Skip to content

Commit c31ac46

Browse files
committed
updated test to account for new sql parameterization behavior
1 parent 6b78b32 commit c31ac46

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

core-codemods/src/test/java/io/codemodder/codemods/DefectDojoSqlInjectionCodemodTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class DefectDojoSqlInjectionCodemodTest {
1313
renameTestFile =
1414
"src/main/java/org/owasp/webgoat/lessons/sqlinjection/advanced/SqlInjectionChallenge.java",
1515
dependencies = {})
16-
final class DefectDojoSqlInjectionChallengeCodemodTestMixin implements CodemodTestMixin {}
16+
final class WebGoatSqlInjectionChallengeTest implements CodemodTestMixin {}
1717

1818
@Nested
1919
@Metadata(
@@ -22,5 +22,5 @@ final class DefectDojoSqlInjectionChallengeCodemodTestMixin implements CodemodTe
2222
renameTestFile =
2323
"src/main/java/org/owasp/webgoat/lessons/sqlinjection/introduction/SqlInjectionLesson8.java",
2424
dependencies = {})
25-
final class DefectDojoSqlInjectionLesson8CodemodTestMixin implements CodemodTestMixin {}
25+
final class WebGoatSqlInjectionLesson8Test implements CodemodTestMixin {}
2626
}

core-codemods/src/test/resources/defectdojo-sql-injection/SqlInjectionChallenge/SqlInjectionChallenge.java.after

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class SqlInjectionChallenge extends AssignmentEndpoint {
6565

6666
try (Connection connection = dataSource.getConnection()) {
6767
String checkUserQuery =
68-
"select userid from sql_challenge_users where userid = ?";
68+
"select userid from sql_challenge_users where userid = ?" + "" + "";
6969
PreparedStatement statement = connection.prepareStatement(checkUserQuery);
7070
statement.setString(1, username_reg);
7171
ResultSet resultSet = statement.execute();

core-codemods/src/test/resources/defectdojo-sql-injection/SqlInjectionLesson8/SqlInjectionLesson8.java.after

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,21 @@ public class SqlInjectionLesson8 extends AssignmentEndpoint {
6464
protected AttackResult injectableQueryConfidentiality(String name, String auth_tan) {
6565
StringBuilder output = new StringBuilder();
6666
String query =
67-
"SELECT * FROM employees WHERE last_name = '"
68-
+ name
69-
+ "' AND auth_tan = '"
70-
+ auth_tan
71-
+ "'";
67+
"SELECT * FROM employees WHERE last_name = ?"
68+
+ ""
69+
+ " AND auth_tan = ?"
70+
+ ""
71+
+ "";
7272

7373
try (Connection connection = dataSource.getConnection()) {
7474
try {
75-
Statement statement =
76-
connection.createStatement(
77-
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
75+
PreparedStatement statement =
76+
connection.prepareStatement(
77+
query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
7878
log(connection, query);
79-
ResultSet results = statement.executeQuery(query);
79+
statement.setString(1, name);
80+
statement.setString(2, auth_tan);
81+
ResultSet results = statement.execute();
8082

8183
if (results.getStatement() != null) {
8284
if (results.first()) {
@@ -149,9 +151,10 @@ public class SqlInjectionLesson8 extends AssignmentEndpoint {
149151
action = action.replace('\'', '"');
150152
Calendar cal = Calendar.getInstance();
151153
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
154+
String time = sdf.format(cal.getTime());
152155

153156
String logQuery =
154-
"INSERT INTO access_log (time, action) VALUES (?" + ", ?" + ")";
157+
"INSERT INTO access_log (time, action) VALUES (?" + "" + ", ?" + "" + ")";
155158

156159
try {
157160
PreparedStatement statement = connection.prepareStatement(logQuery, TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE);

framework/codemodder-testutils/src/main/java/io/codemodder/testutils/CodemodTestMixin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ default void verifyTransformedCode(final Path before, final Path expected, final
251251
throws IOException {
252252
String expectedCode = Files.readString(expected);
253253
String transformedJavaCode = Files.readString(after);
254+
System.out.println(transformedJavaCode);
254255
assertThat(transformedJavaCode, equalTo(expectedCode));
255256
}
256257

0 commit comments

Comments
 (0)