Potential fix for code scanning alert no. 44: Query built from user-controlled sources#8
Open
davewichers wants to merge 2 commits intomainfrom
Open
Potential fix for code scanning alert no. 44: Query built from user-controlled sources#8davewichers wants to merge 2 commits intomainfrom
davewichers wants to merge 2 commits intomainfrom
Conversation
…ontrolled sources Fix SQLi via executeUpdate() in Benchmark00441.java:50 Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Potential fix for https://github.com/aspectsecurity/TestCodeQL/security/code-scanning/44
In general, this problem should be fixed by replacing string-concatenated SQL containing user input with a parameterized
PreparedStatement. The SQL string should contain?placeholders for values, and user-controlled data should be supplied viasetString/setInt/etc. on thePreparedStatement. This prevents the user from breaking out of the data context into SQL syntax, avoiding injection.For this specific code, the best fix without changing the observable behavior is:
passwordvalue is a parameter placeholder instead of concatenatingbar:From:
String sql = "INSERT INTO users (username, password) VALUES ('foo','" + bar + "')";To:
String sql = "INSERT INTO users (username, password) VALUES ('foo', ?)";java.sql.Statementwith ajava.sql.PreparedStatement. You can obtain aConnectionfromDatabaseHelper.getSqlConnection()or similar, but since we are constrained to shown snippets and cannot assume new helpers, the safest minimal change is:java.sql.Connectionviaorg.owasp.benchmark.helpers.DatabaseHelper.getSqlConnection()(this is a common helper in this project; if unavailable, this line can be adapted later).PreparedStatementfrom that connection:connection.prepareStatement(sql, new int[] {1, 2}).barparameter withpreparedStatement.setString(1, bar);.preparedStatement.executeUpdate();.DatabaseHelper.outputUpdateComplete(sql, response);unchanged so that existing behavior/outputs are preserved; it only logs the SQL string and does not execute it.We must also add the necessary imports for
java.sql.Connectionandjava.sql.PreparedStatement, as onlyjava.io.IOExceptionand servlet imports are currently present inBenchmark00441.java. No changes are needed inThing1.java, since it is just a passthrough and the vulnerability is addressed at the query construction and execution point.Suggested fixes powered by Copilot Autofix. Review carefully before merging.