Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues- Fixed By CodeAid #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public class ProductRepository {

@PersistenceContext
public EntityManager entityManager;

public Product findByName(String name) {
// WARNING: The following line is vulnerable to SQL injection!
String sql = "SELECT p FROM Product p WHERE p.name = '" + name + "'";
return entityManager.createQuery(sql, Product.class)
.getSingleResult();
public Product findByName(String name) {
String sql = "SELECT p FROM Product p WHERE p.name = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, name);
return statement.executeQuery();
}
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import org.junit.Test;
import static org.junit.Assert.*;

public class ProductTest {

@Test
public void testFindByName() {
// Arrange
String name = "test";
Product expectedProduct = new Product();

// Act
Product actualProduct = findProductByName(name);

// Assert
assertEquals(expectedProduct, actualProduct);
}

private Product findProductByName(String name) {
// WARNING: The following line is vulnerable to SQL injection!
String sql = "SELECT p FROM Product p WHERE p.name = '" + name + "'";
return entityManager.createQuery(sql, Product.class)
.getSingleResult();
}
}