qdw1987 / WebGoat

WebGoat is a deliberately insecure application
https://owasp.org/www-project-webgoat/
Other
0 stars 0 forks source link

fix the sql injection SqlInjectionLesson10.java #15

Open qdw1987 opened 8 months ago

qdw1987 commented 8 months ago
Checklist - [X] Modify `src/main/java/org/owasp/webgoat/lessons/sql_injection/introduction/SqlInjectionLesson10.java` ✓ https://github.com/qdw1987/WebGoat/commit/70ee60bfb32c7bb76b2a6daf106c73f1ad1b199c [Edit](https://github.com/qdw1987/WebGoat/edit/sweep/fix_the_sql_injection_sqlinjectionlesson/src/main/java/org/owasp/webgoat/lessons/sql_injection/introduction/SqlInjectionLesson10.java) - [X] Running GitHub Actions for `src/main/java/org/owasp/webgoat/lessons/sql_injection/introduction/SqlInjectionLesson10.java` ✓ [Edit](https://github.com/qdw1987/WebGoat/edit/sweep/fix_the_sql_injection_sqlinjectionlesson/src/main/java/org/owasp/webgoat/lessons/sql_injection/introduction/SqlInjectionLesson10.java)
sweep-ai[bot] commented 8 months ago

🚀 Here's the PR! #16

See Sweep's progress at the progress dashboard!
Sweep Basic Tier: I'm using GPT-4. You have 5 GPT-4 tickets left for the month and 3 for the day. (tracking ID: 5417749826)

For more GPT-4 tickets, visit our payment portal. For a one week free trial, try Sweep Pro (unlimited GPT-4 tickets).
Install Sweep Configs: Pull Request

[!TIP] I can email you next time I complete a pull request if you set up your email here!


Actions (click)

Sandbox Execution ✓

Here are the sandbox execution logs prior to making any changes:

Sandbox logs for aeb481e
Checking src/main/java/org/owasp/webgoat/lessons/sql_injection/introduction/SqlInjectionLesson10.java for syntax errors... ✅ src/main/java/org/owasp/webgoat/lessons/sql_injection/introduction/SqlInjectionLesson10.java has no syntax errors! 1/1 ✓
Checking src/main/java/org/owasp/webgoat/lessons/sql_injection/introduction/SqlInjectionLesson10.java for syntax errors...
✅ src/main/java/org/owasp/webgoat/lessons/sql_injection/introduction/SqlInjectionLesson10.java has no syntax errors!

Sandbox passed on the latest develop, so sandbox checks will be enabled for this issue.


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/qdw1987/WebGoat/blob/aeb481e561e4a379a58ce84b26544b0c28e547d0/src/it/java/org/owasp/webgoat/SeleniumIntegrationTest.java#L58-L95 https://github.com/qdw1987/WebGoat/blob/aeb481e561e4a379a58ce84b26544b0c28e547d0/src/main/resources/lessons/sql_injection/documentation/SqlInjection_introduction_content5_before.adoc#L1-L15 https://github.com/qdw1987/WebGoat/blob/aeb481e561e4a379a58ce84b26544b0c28e547d0/src/it/java/org/owasp/webgoat/SeleniumIntegrationTest.java#L95-L110 https://github.com/qdw1987/WebGoat/blob/aeb481e561e4a379a58ce84b26544b0c28e547d0/src/main/java/org/owasp/webgoat/lessons/sql_injection/introduction/SqlInjectionLesson3.java#L45-L59 https://github.com/qdw1987/WebGoat/blob/aeb481e561e4a379a58ce84b26544b0c28e547d0/src/main/java/org/owasp/webgoat/lessons/sql_injection/introduction/SqlInjectionLesson8.java#L43-L57 https://github.com/qdw1987/WebGoat/blob/aeb481e561e4a379a58ce84b26544b0c28e547d0/src/main/java/org/owasp/webgoat/lessons/sql_injection/introduction/SqlInjectionLesson9.java#L44-L58 https://github.com/qdw1987/WebGoat/blob/aeb481e561e4a379a58ce84b26544b0c28e547d0/src/main/java/org/owasp/webgoat/lessons/sql_injection/advanced/SqlInjectionLesson6a.java#L40-L55

Step 2: ⌨️ Coding

--- 
+++ 
@@ -55,12 +55,15 @@

     protected AttackResult injectableQueryAvailability(String action) {
         StringBuffer output = new StringBuffer();
-        String query = "SELECT * FROM access_log WHERE action LIKE '%" + action + "%'";
+        String query = "SELECT * FROM access_log WHERE action LIKE ?";// The query is now parameterized

         try (Connection connection = dataSource.getConnection()) {
+            PreparedStatement statement = null;
+            ResultSet results = null;
             try {
-                Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
-                ResultSet results = statement.executeQuery(query);
+                statement = connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
+                statement.setString(1, '%' + action + '%');
+                results = statement.executeQuery();

                 if (results.getStatement() != null) {
                     results.first();
@@ -79,8 +82,14 @@
                 } else {
                     return success(this).feedback("sql-injection.10.success").build();
                 }
+            } finally {
+                if (results != null) {
+                    results.close();
+                }
+                if (statement != null) {
+                    statement.close();
+                }
             }
-
         } catch (Exception e) {
             return failed(this).output("").build();
         }

Ran GitHub Actions for 70ee60bfb32c7bb76b2a6daf106c73f1ad1b199c:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/fix_the_sql_injection_sqlinjectionlesson.


🎉 Latest improvements to Sweep:


💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request. Join Our Discord