SQL injection prevention: parameters, allow-lists and access control
Learn where parameterized queries help, why dynamic sort fields need allow-lists and what to review in ORMs and stored procedures.
SQL injection occurs when untrusted data becomes part of a query's SQL syntax and changes its meaning. The main defence is to keep the query structure separate from its values: write the SQL, then bind values through the database library. For structural choices that cannot be parameters, such as a sort column, use a fixed allow-list controlled by the application.
That separation is the core recommendation in the OWASP SQL Injection Prevention Cheat Sheet. It applies to values arriving through forms, APIs, imported files and even data already stored in the database. "It came from our database" is not a reason to concatenate it into a later query.
Why concatenation is the problem
Consider this illustrative query-building pattern:
UNSAFE: SQL text = "SELECT balance FROM accounts WHERE name = '" + supplied_name + "'"
The program mixes the supplied name into the SQL command itself. The database cannot reliably distinguish the intended value from additional SQL syntax introduced through that value.
The safer pattern is:
SQL text = "SELECT balance FROM accounts WHERE name = ?"
Execute SQL with supplied_name bound separately
The placeholder syntax depends on the language, database and driver. These are explanatory patterns, not executable code for every library. Use the binding API documented for your actual driver. OWASP's parameterization examples show several language-specific implementations.
The important detail is where the separation happens. Concatenating values into a string and then passing that completed string to a method named prepare does not restore the boundary.
Parameters do not replace permission checks
A correctly bound query can still return an account the caller should not see. Parameter binding stops the supplied value from changing SQL syntax; it does not decide which rows the caller is allowed to access.
For an account screen, the review therefore needs two separate questions:
- Is the supplied identifier bound as data?
- Is the caller authorized to access the account identified by that value?
Do not close an access-control bug merely because the query uses placeholders. Likewise, a permission check does not make string-built SQL safe.
How to handle dynamic sorting and column names
A parameter normally represents a value. It does not stand in for an arbitrary table name, column name or SQL keyword such as ASC.
If a catalogue lets visitors choose a sort order, translate the submitted option into a fixed expression:
| Visitor option | Application-owned SQL choice |
|---|---|
| newest | created_at DESC |
| price-low | price ASC |
| price-high | price DESC |
| anything else | Reject it or apply a documented default |
This table is a suggested design, not a universal schema. The application chooses the allowed columns and directions. It must not simply return the incoming string after checking that it contains ordinary letters. Continue binding filter values separately.
Quoting an identifier is also different from deciding whether a user should be able to select that table or column. Syntax protection is not authorization.
ORMs and stored procedures still need review
An ORM can provide safe binding features, but raw query strings and concatenated ORM query languages can reintroduce injection. Review the actual query-construction path, including reporting screens and administrative tools, rather than assuming the framework handles every case.
Stored procedures have a similar boundary. Binding the arguments protects the procedure call. If the procedure constructs another SQL command internally by concatenation, that internal command needs its own review and parameter handling.
A practical audit should follow a value from entry point to final execution. Stopping at the first prepared call can miss the vulnerable step later in the chain.
Validation and least privilege are additional layers
OWASP strongly discourages manually escaping values as the main defence. Escaping rules differ by SQL dialect and configuration. Validating a value's type, length or format is useful, but it does not replace binding it.
Limit database privileges as well. An application account should receive only the access its work requires, not broad administrator rights. Separate applications may need separate database accounts. Views can limit exposed columns, subject to the database's own privilege rules. Queries against views still need parameterization.
These layers reduce exposure and potential damage. None makes it acceptable to leave an injection path in place.
What to put in a code-review checklist
Use this as a starting point for a project-specific review:
- Find query execution sites, including raw ORM queries and stored procedures.
- Check that untrusted values are bound separately from SQL text.
- Trace dynamic identifiers and sort directions back to fixed application-owned choices.
- Review internal dynamic SQL, not only the outer procedure call.
- Verify row-level authorization independently of query binding.
- Check the database account's actual permissions.
- Run appropriate security tests in an authorized environment and record what was covered.
A passing review of one endpoint is not evidence that the whole application is safe. This article explains prevention patterns; it is not a security assessment, a complete testing guide or a guarantee against compromise.
By Omnistu. Technical guidance reviewed 16 November 2026. Examples are illustrative and have not been executed against a project database.