Security: Fix 2 CWE-89 (SQL Injection) vulnerabilities in src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02369.java:65,67#668
Open
appsecai-app[bot] wants to merge 1 commit intomainfrom
Conversation
…nchmarkTest02369.java Replace string concatenation with parameterized PreparedStatement to prevent SQL injection attacks. User-controlled HTTP request parameter names are now binded as typed parameters instead of being concatenated into SQL templates. Fixes: CWE-89 (lines 65, 67)
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
69c734eb(2 instances)src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02369.java:65,67Description: User-controlled input from HTTPServletRequest flows directly into a SQL INSERT statement via string concatenation. Both the SQL template construction (line 65) and statement execution (line 67) lack parameterization, creating a direct SQL injection sink.
Why this matters
Risk if not fixed: An attacker could read, modify, or delete database contents, bypass authentication, execute administrative operations, or in some cases execute operating system commands through database-level functionality.
Risk level: Critical — Direct database access with INSERT-level privileges
Attack surface: HTTP request parameter names are attacker-controlled and flow directly into SQL without sanitization.
Why we're changing it
Status: Confirmed vulnerability
Data Flow Analysis:
The arithmetic in
doSomething()at line 85 is deterministic:(7 * 42) - 106 = 188, which is not > 200, so the ternary always returnsparam— never the hardcoded string. Thereforebar = paramunconditionally.paramis set to the HTTP request parameter name (not value) when a parameter's value equals"BenchmarkTest02369". An attacker controls parameter names, so a request like:sets
param = "malicious'; DROP TABLE users;--", which flows directly into:No sanitization, escaping, or parameterization exists anywhere in this path. The attacker-controlled string is concatenated into the SQL template at line 65 and executed verbatim at line 67.
Evidence:
param = name— assigns HTTP parameter name (attacker-controlled) to parambar = (7 * 42) - num > 200 ? ... : param— evaluates to param always (188 not > 200)String sql = "INSERT INTO users (username, password) VALUES ('foo','" + bar + "')"— direct string concatenation with no escapingstatement.executeUpdate(sql, ...)— executes unsanitized SQL stringHow 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["HTTP Request Parameter Name<br/>(attacker-controlled)"] --> B["Line 53 - param = name"] B --> C["Line 85 - bar = param<br/>(ternary always returns param)"] C --> D["Line 65 - String concatenation<br/>INSERT ... VALUES ... + bar"] D --> E["Line 67 - statement.executeUpdate<br/>(executes raw SQL)"] E --> F["💥 SQL Injection<br/>Attacker breaks string context<br/>and injects arbitrary SQL"] style A fill:#EDE9FE,stroke:#7C3AED style D fill:#FFE5E5,stroke:#F65A5A style E fill:#FFE5E5,stroke:#F65A5A style F fill:#FEF3C7,stroke:#F59E0BManual Verification Steps
src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02369.javaat lines 65 and 67"INSERT INTO users (username, password) VALUES ('foo','" + bar + "')"statement.executeUpdate(sql, ...)with the concatenated stringPreparedStatementis used before the fix?placeholderPreparedStatement.executeUpdate()without a SQL string argumentstatement.setString(1, bar)binds the user input as a typed parameterRunnable Verification Script (click to expand)
Save this script and run with
bash verify_fix.sh:Vulnerable flow: src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02369.java:65
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["Parameterized queries/ORM used"] 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 the HTTP request flows through
doSomething()into thebarvariable, which is then concatenated directly into a SQL INSERT statement string. Thejava.sql.Statement.executeUpdate()call executes this tainted string verbatim, allowing an attacker to break out of the string literal and inject arbitrary SQL commands.Fix approach: The fix replaces string concatenation with a parameterized
PreparedStatement. The SQL template uses a?placeholder for the user-supplied value, andstatement.setString(1, bar)binds the value as a data parameter. The JDBC driver handles all escaping internally, making it structurally impossible for user input to be interpreted as SQL syntax regardless of content.Alternatives considered:
Code Changes
Before (lines 65-67):
After (lines 65-67):
Key changes:
?placeholder instead of string concatenationconn.prepareStatement(sql)returns aPreparedStatementinstead ofStatementstatement.setString(1, bar)binds the user input as a typed parameterstatement.executeUpdate()executes without accepting a SQL string argumentVulnerabilities Addressed
CWE-89
Tainted Sql From Http Request
CWE-89
Jdbc Sqli
How we validated it
PreparedStatementis used for query executionHow to verify
Reviewers can verify this fix by:
'; DROP TABLE users;--to confirm they are safely escapedBefore you merge
Learn more
This fix was generated by AppSecAI. Please review before merging.