Development
How do you fix SQL injection in Java, Python, JavaScript, PHP and C#?


Technical writer
7 min
To fix SQL injection, pass user input to the query as a bound parameter, never as part of the SQL string. In the Fluid Attacks Database, weakness 146 (CWE-89) has 35 detection rules and code fixes in 11 languages. The same weakness names cases that a parameter alone does not cover: table names, column names, sort order and LIKE wildcards.
SQL injection still ranks near the top. CWE-89, the weakness ID that MITRE gives SQL injection, is #2 in the 2025 CWE Top 25, one place higher than in 2024. OWASP counts more than 14,000 CVEs for SQL injection in A05:2025 Injection, a category of the OWASP Top 10 2025. This post shows the fix in five languages, with code from our database, and the cases that need more than a placeholder.
What does the Fluid Attacks Database say about SQL injection?
The Fluid Attacks Database links each weakness to the requirement that prevents it, the code that fixes it, the rules that detect it and the standards that ask for it. For SQL injection, that chain starts at weakness 146.
Link in the chain | What the database lists for SQL injection |
|---|---|
Weakness | 146. SQL injection, expected remediation time of 30 minutes |
Requirements | 169. Use parameterized queries and 173. Discard unsafe inputs |
Standards | 30 standard references, linked through requirement 169. Among them: CWE-89, PCI DSS 6.2.4 and CERT IDS00-J |
Detection rules | 35: 34 static application security testing (SAST) rules and 1 SaaS security posture management (SSPM) rule |
Code fixes | 11 languages: C#, Elixir, Go, Java, JavaScript/TypeScript, Kotlin, PHP, Python, Ruby, Rust and Scala |
We took these figures from the Fluid Attacks Database on November 6, 2026. The database is updated constantly, so the counts can be different when you read this post.
A SAST rule reads source code and flags a pattern without running the application. The one SSPM rule of weakness 146 checks n8n workflows, as the section on workflow tools shows.
The description of weakness 146 does not stop at string concatenation. It also names ORM query languages such as JPQL and HQL, expression bindings such as SpEL and dynamic identifiers such as table names, column names and sort order. Its recommendations add a strict allowlist for identifiers, an escape function for LIKE wildcards and a database account with least privilege. They also warn that a stored procedure that concatenates its parameters, for example with sp_executesql or EXEC, is as injectable as inline concatenation.
Requirement 169 is shared by seven weaknesses, among them time-based (154), error-based (438) and blind (450) SQL injection. All seven point to the same control: parameterized queries.

Weakness 146: SQL injection in the Fluid Attacks Database.
How do you fix SQL injection in Java, Python, JavaScript, PHP and C#?
In each language, the fix moves user input out of the SQL string and into a parameter that the driver or the ORM handles as data. Each section describes the vulnerable pattern, shows a short example with a text input, links to the fix page of weakness 146 for that language and names the SAST rules that weakness 146 lists for it.
Java: bind values with PreparedStatement
In the vulnerable servlet, the query concatenates the id request parameter into a Statement. An input such as 1 OR 1=1 returns every row. The Java fix uses a PreparedStatement with a ? placeholder. The code below uses a text input, so only the placeholder stops the attack. An input such as ' OR '1'='1 reaches the database as one literal value:
Rules: SAST-PIOFS (Java API SQL injection) and SAST-STISE (CallableStatement SQL injection). SAST-STISE checks Java code that builds a CallableStatement call, the call to a stored procedure, by concatenation.
Python: let the ORM build the query
In Django, the ORM builds the query and passes the values as parameters. Raw SQL is also safe when the values go to cursor.execute() as a separate argument, never inside the string, as the Django documentation recommends. The injectable form puts the input inside the SQL text, for example with string formatting. The code below uses the ORM and looks up a client by username, a text value:
For more detail, see the Python fix page of weakness 146. Rules: SAST-ECCEM (SQLAlchemy), SAST-THN9T (Python database drivers) and SAST-SDOAA (Django raw SQL with unvalidated input).
JavaScript and TypeScript: use prepared statements
The vulnerable Express route builds the query with a template literal that puts username inside the SQL text. The input ' OR '1'='1 returns all users. The mysql package, with a ? placeholder, does not send a prepared statement: it escapes the value on the client and turns an object into key = 'val' pairs. If the HTTP parser accepts nested objects, a username object can change the query. The code below checks that username is a string and uses execute() from MySQL2, which prepares the statement:
For more detail, see the JavaScript/TypeScript fix page of weakness 146. Rules: SAST-IJPI2 (JavaScript), SAST-IE2EU and SAST-PU6IE (TypeScript raw query with user input).
PHP: bind values with the query builder
The vulnerable case is SQL that interpolates the input, such as DB::select("SELECT * FROM users WHERE id = $id"). Raw SQL by itself is not the problem; the interpolated input is. The query builder binds the value as data. Raw SQL with the values as the second argument of DB::select() is also valid. The code below filters by username, a text value:
For more detail, see the PHP fix page of weakness 146. Rules: SAST-CAN7I and SAST-SHWRT (raw SQL injection).
C#: add SqlCommand parameters
The vulnerable ASP.NET Core controller interpolates username into the query text of a SqlCommand. The C# fix writes the query with a @Username placeholder. It also rejects a null, empty or whitespace username before the query runs. The code below sets the parameter type and size explicitly with Parameters.Add:
Rules: SAST-AE1AS (String.Format SQL injection), SAST-P1IEL and SAST-WLSEE (raw SQL with user input).
When is a parameterized query not enough?
A placeholder protects a value. It cannot hold an identifier, and it does not stop LIKE from reading wildcards in the value. Weakness 146 names both cases, and it also covers ORM queries that skip the parameter API.
Table names, column names and ORDER BY
A placeholder accepts data, not identifiers or keywords. A query that sorts by a column the user selects cannot put that column in a ?, and concatenation brings the injection back. Weakness 146 asks for a strict allowlist: the application compares the input with a fixed list of column names and ASC or DESC, and rejects all other values. The OWASP SQL Injection Prevention Cheat Sheet gives the same advice. A blocklist does not replace it, as our post on bypassing SQLi filters manually shows.
LIKE wildcards
In a LIKE clause, % matches any string of zero or more characters and _ matches any single character, as the SQL Server LIKE reference shows. A parameter keeps these characters out of the SQL syntax, but the database still reads them as wildcards. A search value of % matches every row that the query can reach. Weakness 146 states that parameterization does not prevent wildcard injection and asks the developer to sanitize the wildcards explicitly, for example with an escape(String) function. The database detects the Java case with rule SAST-1SC0E (Java LIKE clause unescaped input).
Raw queries inside an ORM
An object-relational mapper (ORM) maps database tables to classes and builds the SQL for the developer. Weakness 146 includes ORM query languages such as JPQL and HQL when they are built from untrusted input. Its rules include checks for TypeORM (SAST-IRLSS), Sequelize (SAST-NLIIS), GORM (SAST-IGFRL), Ecto (SAST-QTTFL) and Diesel or SQLx (SAST-ENDNI). The fix is the same as in plain SQL: bind the value through the parameter API of the ORM.

Weakness 146 has 34 SAST rules and 1 SSPM rule.
Does SQL injection reach workflow automation tools?
Yes. Two n8n advisories from 2026 show the same flaw in database nodes, where the value of a {{ ... }} expression goes straight into the query text.
Advisory | Node | Severity | Fixed in |
|---|---|---|---|
GHSA-hwmj-qg4v-cvg9 (CVE-2026-59257), June 2026 | MySQL v1, executeQuery | CVSS v4.0 5.3 | 1.123.61, 2.27.4, 2.28.1 |
GHSA-5qpp-pqww-h7fp, September 2026 | Microsoft SQL v1 | CVSS v4.0 7.0 | 2.41.4, 2.42.1 |
Both advisories describe workflows that pass outside input, for example from a webhook, into the query field. The fix is to update n8n. As interim steps, the advisories point to node versions that use query parameters: the MySQL v2 node, and version 1.1 or later of the Microsoft SQL node, with the dynamic values moved to the Query Parameters option. The Fluid Attacks Database checks this pattern with rule SSPM-1U1KZ (n8n interpolated SQL query), the one SSPM rule of weakness 146.
What should you check after the fix?
Weakness 146 gives an expected remediation time of 30 minutes. Other queries in the same code base can follow the same pattern, so after the first fix, look for it in the rest of the code:
Find every call that runs SQL from a
string: Statement,cursor.executewith string formatting,connection.querywith a template literal, andDB::selectorSqlCommandwith interpolated text.Check each
ORDER BY, table name and column name that comes from a request against an allowlist.Check each
LIKEclause for escaped wildcards.Check the raw query methods of your ORM and the query fields of your workflow tools.
For the habits that keep these patterns out of new code, see our guide to secure coding practices.
A placeholder fixes the value, not the query around it. Weakness 146 reaches beyond the WHERE clause: its recommendations cover sort columns and LIKE searches, and its 35 rules include ORM queries and n8n workflows. Treat the first finding as one instance of a pattern. Use the fix page for your language as the reference, and check the rest of your code against the same rules.
* Thanks to Camilo Vera, Staff Engineer at Fluid Attacks, for reviewing this article and providing technical support.
Try SAST from Fluid Attacks and catch SQLi before deploy
Tags:
code
web
vulnerability
Subscribe to our newsletter
Stay updated on our upcoming events and latest blog posts, advisories and other engaging resources.
Other posts























