Security: Fix CWE-89 (SQL Injection) vulnerability in src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02177.java:50#676
Open
appsecai-app[bot] wants to merge 1 commit intomainfrom
Conversation
…hmarkTest02177.java Replace string concatenation with parameterized SQL query using Spring JdbcTemplate PreparedStatementSetter to bind user input as data, eliminating SQL injection vector.
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.
What we found
69c734ebsrc/main/java/org/owasp/benchmark/testcode/BenchmarkTest02177.java:50Description: User-controlled input from HTTPServletRequest flows directly into a SQL query via string concatenation with no parameterization or sanitization. An attacker can inject arbitrary SQL commands into the PASSWORD clause of an authentication query, potentially bypassing authentication or accessing unauthorized data.
Why this matters
Risk if not fixed: SQL injection on an authentication query creates a critical attack surface. An attacker could:
' OR '1'='1)Risk level: Direct database access with no input validation layer
Why we're changing it
The vulnerability stems from a deterministic data flow:
param = request.getParameter("BenchmarkTest02177")— attacker-controlled HTTP parameterguess = "ABC"; switchTarget = guess.charAt(2)— evaluates to'C'at compile timecase 'C': bar = param— user input assigned tobarunconditionallysql = "SELECT * from USERS where USERNAME='foo' and PASSWORD='" + bar + "'"— raw string concatenation with no escapingJDBCtemplate.query(sql, ...)— unparameterized SQL executed against the databaseThe switch statement's compile-time constant ensures
baralways receives the raw HTTP parameter value, which is then concatenated directly into the SQL string. No ESAPI encoding, PreparedStatement, or bind variable usage protects the query construction.How we confirmed
Vulnerability Flow Diagram
%%{init: {'theme':'base','themeVariables':{'fontFamily':'ui-sans-serif, Inter, system-ui, sans-serif','primaryColor':'#EDE9FE','primaryTextColor':'#1A1A2E','primaryBorderColor':'#7C3AED','lineColor':'#5B21B6','secondaryColor':'#FEF3C7','tertiaryColor':'#DCFCE7'}}}%% flowchart TD A["HTTPServletRequest.getParameter<br/>BenchmarkTest02177"] -->|attacker input| B["param variable"] B -->|switchTarget='C'<br/>case 'C': bar=param| C["bar variable<br/>unvalidated"] C -->|string concatenation| D["SQL: SELECT * FROM USERS<br/>WHERE PASSWORD='" + bar + "'"] D -->|JDBCtemplate.query<br/>unparameterized| E["❌ SQL Injection<br/>Attacker can modify query logic"] style A fill:#EDE9FE,stroke:#7C3AED style B fill:#EDE9FE,stroke:#7C3AED style C fill:#FFE5E5,stroke:#F65A5A style D fill:#FFE5E5,stroke:#F65A5A style E fill:#FEF3C7,stroke:#F59E0BManual Verification Steps
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02177.javaat line 48-52?placeholder instead of string concatenationJDBCtemplate.query()uses aPreparedStatementSetterto bind thebarvariable viaps.setString(1, bar)Runnable Verification Script (click to expand)
Save this script and run with
bash verify_fix.sh:Vulnerable flow: src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02177.java:50
SQL Injection
%%{init: {'theme':'base','themeVariables':{'fontFamily':'ui-sans-serif, Inter, system-ui, sans-serif','primaryColor':'#EDE9FE','primaryTextColor':'#1A1A2E','primaryBorderColor':'#7C3AED','lineColor':'#5B21B6','secondaryColor':'#FEF3C7','tertiaryColor':'#DCFCE7'}}}%% flowchart TD subgraph Vulnerable["❌ Vulnerable Flow"] direction LR A1["Project"] --> A2["SQL string concatenation with user input"] A2 --> A3["💥 Database Compromised"] end Vulnerable ~~~ Fixed subgraph Fixed["✅ Fixed Flow"] direction LR B1["Project"] --> B2["The fix is in place"] B3["🛡️ Attack Blocked"] B2 --> B3 end style A2 fill:#FFE5E5,color:#000 style A3 fill:#ffa94d,color:#000 style B2 fill:#74c0fc,color:#000 style B3 fill:#DCFCE7,color:#000How we fixed it
Fix Description
Root cause: User-controlled input from HTTPServletRequest flows through the
doSomething()method into thebarvariable (the switch statement's compile-time constantswitchTargetevaluates to'C', assigningparamdirectly tobar). This attacker-controlled value was previously passed into a SQL query via string concatenation, allowing arbitrary SQL to be injected into the PASSWORD clause.Fix approach: The fix replaces string concatenation with Spring JdbcTemplate's parameterized query mechanism. The SQL string now contains a
?placeholder, and an explicitPreparedStatementSetterbinds thebarvariable viaps.setString(1, bar). The JDBC driver treats the bound value strictly as data, never as executable SQL syntax, completely eliminating the injection vector regardless of input content.PreparedStatementSettermakes the parameterization unambiguous to both static analysis tools and human reviewers.Alternatives considered:
JDBCtemplate.query(sql, new Object[]{bar}, rowMapper)) — functionally correct but deprecated in Spring 5.3+ and potentially misread by SAST tools as tainted data flowing alongside the SQL stringVulnerabilities Addressed
CWE-89
Tainted Sql From Http Request
How we validated it
The fix was validated by:
?placeholder instead of string concatenationPreparedStatementSetterexplicitly binds the user input as a data parameterHow to verify
Reviewers can verify the fix by:
BenchmarkTest02177.javato confirm the parameterized query structuremvn test -Dtest=BenchmarkTest02177Before you merge
Learn more
This fix was generated by AppSecAI. Please review before merging.