SQL injection check
Goal
Remove every path where outside text can change the structure of a database query.
A good fix uses parameters for values and allow lists for identifiers, and is covered by a test.
When to use
- Code uses raw SQL, query builders or ORM escape hatches.
- A security scanner flagged a query.
- The app accepts sort fields, filters or search text from users.
- For validating input generally, use
use_skillwithinput-validation-audit.
Steps
- Find the database layer with
rgforSELECT,INSERT,UPDATE,DELETE,execute(,query(,raw(,$queryRawUnsafe,text(and.format(near SQL. - List every site that builds a query from variables: template strings,
+concatenation,%formatting,fstrings. - Read each site with
read_fileand trace backwards: where does each variable come from. Mark ones that can come from a request, file, queue or another user's stored data. - Rewrite values as bound parameters using the driver's placeholder style (
?,$1,:name,%sas a parameter marker, not formatting). Useedit_file. - Handle identifiers (column names, sort order, table names), which cannot be parameters: map the user's choice through a fixed allow list to a known column.
- Handle
LIKE: parameterise the whole pattern and escape the wildcard characters if literal matching is intended. - Handle
INlists by generating the right number of placeholders, not by joining values into the text. - Check ORM raw helpers. Tagged template helpers that parameterise are safe. Their string-taking
unsafevariants are not. - Check NoSQL and other query languages: filters built from parsed request JSON can inject operators such as
$ne. Cast values to expected types before use. - Check least privilege for the database user: no schema changes, no access to other databases, limited to needed tables.
- Write tests that send a quote, a comment marker and a tautology as input and confirm results are unchanged.
- Report each site as fixed, safe with reason, or needs the owner.
Gotchas
- Escaping functions are brittle and depend on character sets. Parameters keep the data out of the query text entirely.
- Stored values are untrusted too. Data saved earlier and then concatenated later is second order injection.
- ORMs are not immune: raw fragments,
order_bywith user strings andwherewith string arguments inject. - A numeric id read from the URL is still a string until cast. Casting and parameters should both happen.
- Error messages that show the SQL help an attacker. Log them server side and return a generic error.
- Stored procedures that build dynamic SQL inside reintroduce the problem under a different name.
Output
- A table of query sites: location, source of input, status, fix.
- The edits made and tests added.
- Sites that could not be traced, with why.
