SQL injection has been on the OWASP Top 10 for over two decades, and it refuses to leave. Not because developers don’t know about it — every framework ships parameterized queries as the default, every tutorial mentions it — but because the query that escapes the parameter is always the one nobody reviewed: the dynamic ORDER BY, the identifier interpolated into a report generator, the raw-SQL escape hatch inside a well-behaved ORM, the search filter assembled from a legacy string template. Injection in 2026 is rarely a concatenated login form. It’s a gap between “we use an ORM” and “we understand what parameterization actually protects.”
This post is about that gap. We’ll look at why parameterization works at the protocol level — the part that makes it a structural defense rather than an escaping trick — where its protection genuinely ends, and how to audit for the paths that slip past it.
The Vulnerability Is a Parsing Confusion
Every injection bug starts the same way: data becomes code. The database receives one string and must decide which parts are query structure and which parts are values. When you build SQL by concatenation, you’ve delegated that decision to whatever the user typed.
# Vulnerable: the user's input is parsed as SQL
email = request.args["email"]
query = "SELECT id, name FROM users WHERE email = '" + email + "'"
cur.execute(query)
An input of ' OR 1=1 -- terminates the string literal, injects a predicate, and comments out the rest. The classic exploits go beyond reading one table: stacked statements where the driver allows them, UNION SELECT to read other tables through the same channel, boolean- and time-based blind injection that exfiltrates data one bit per request even when errors are suppressed. Second-order injection is the sneakiest variant — the payload is stored safely (it was parameterized on insert), then interpreted as SQL later when a different code path builds a query from stored values.
Notice what all of these have in common: the payload crosses the code/data boundary inside the database’s parser. That’s the boundary parameterization moves.
Why Parameterization Works: Parse Time and Bind Time
A parameterized query sends the query text and the parameters as separate things. With PostgreSQL’s extended query protocol, the flow is Parse → Bind → Execute: the server parses the query with placeholders ($1, $2) and produces a prepared statement; parameters are then bound to those placeholders and can only ever be read as values. By the time your user’s input arrives, parsing is already finished. There is no input that can change the shape of the query, because the input is never part of the thing being parsed.
# Safe: parameters travel separately from the query text
cur.execute(
"SELECT id, name FROM users WHERE email = %s AND active = %s",
(email, True),
)
This is why parameterization beats escaping. Escaping is a transformation that tries to make hostile data safe by rewriting it correctly for one grammar; parameterization doesn’t rewrite anything — it removes the data from the grammar entirely. That’s also why drivers that look like they’re “just escaping for you” under the hood still deserve scrutiny: correctness depends on the escaping being right for every value, every encoding, every edge case.
Not All Parameterized Queries Are Executed the Same Way
Here’s a nuance most developers miss: two drivers can both accept a parameters list and behave differently on the wire. Python’s PostgreSQL drivers are the textbook case.
psycopg2 merges query and parameters on the client: it adapts each Python value to a SQL literal, quotes it, splices it into the query string, and sends one final string to the server. The quoting logic is mature and safe for values, but the server receives a monolithic string — the code/data separation exists only in the driver’s string manipulation. psycopg3 changed this: it uses the extended query protocol natively and sends the query and parameters separately via PQexecParams, with real server-side binding. Same calling convention, different security and performance profile — the server sees identical query text across executions, which improves plan caching and makes pg_stat_statements aggregates far more useful.
The same separation exists in other ecosystems. Go’s database/sql passes placeholders through to the driver, and the PostgreSQL driver uses the extended protocol:
rows, err := db.Query(
"SELECT id, name FROM users WHERE email = $1 AND active = $2",
email, true,
)
The audit point is not “your driver is broken” — it’s that the safety guarantee lives in the driver’s implementation, not in your code. Know which protocol your driver uses, and treat driver major versions as security-relevant upgrades.
Where Parameterization Stops
Parameterization covers values. Four places values aren’t enough, and every one of them shows up in real incidents:
- Identifiers. Table names, column names, and schema names can’t be bound parameters — the parser needs them at parse time. If a sort column or report table name comes from user input, use the driver’s identifier-quoting facility (psycopg’s
sql.Identifier) plus a whitelist of allowed names. Whitelisting is the real control; quoting is defense in depth. - Query structure.
ORDER BYdirection, which tables get joined, which predicate branch runs — all parse-time decisions. Asort=price;DROP TABLE xin a dynamic clause isn’t defused by binding the rest of the query. - LIMIT/OFFSET and numbers. Some drivers refuse to bind numeric literals in positions the grammar reserves; the fix is casting to int in application code before interpolation — never trusting the raw string.
- LIKE patterns. Binding protects against injection, not against wildcard abuse: a bound
%in aLIKEpattern can turn a targeted lookup into a full-table scan or a data-mining endpoint. Escape wildcards in the pattern itself for user-facing search.
from psycopg import sql
ALLOWED_SORT = {"created_at", "name", "total"}
sort = request.args.get("sort", "created_at")
query = sql.SQL("SELECT id, total FROM orders ORDER BY {} DESC").format(
sql.Identifier(sort) if sort in ALLOWED_SORT else sql.Identifier("created_at")
)
cur.execute(query)
ORMs deserve a specific warning: their default query builders are parameterized, but every major ORM ships a raw-query escape hatch, and those escape hatches are where the injection bugs concentrate. A Session.execute(text("... f-string ...")) or a fragment interpolated into extra(where=...) bypasses the builder entirely. Auditing rule: grep your codebase for the raw-query APIs and review every hit as if it were direct concatenation, because that’s what it is.
Defense in Depth Below the Query
Parameterization is the primary control, not the whole strategy. When it fails — one raw query, one dynamic identifier — the blast radius is decided by the layers under it:
- Least-privilege database roles. The application user should have exactly the DML it needs on exactly the tables it needs. No
superuser, no DDL in normal operation, no access to tables it never reads. - Disable multi-statement execution. Most drivers execute one statement per call by default; keep it that way. Stacked queries are what turn a read bug into a drop-table bug.
- Errors stay in logs, not responses. Verbose database errors reveal schema, query text, and driver versions — reconnaissance that makes blind injection dramatically cheaper.
- Validate early, in typed form. Parse user input into typed values (UUIDs, ints, enums) at the boundary. Everything downstream that receives a typed value can’t carry a payload.
A Practical Audit Checklist
- Grep for string formatting into queries:
f"nearSELECT/INSERT/UPDATE/DELETE,.format(,%interpolation, and+concatenation in query-building code. - Review every raw-query call in your ORM, including stored procedures with dynamic SQL inside them.
- Check every user-controlled identifier (sort keys, column choices, dynamic table routing) for a whitelist.
- Confirm your driver uses server-side binding, and record which one — this is exactly the kind of fact that changes silently during a library migration.
- Verify the application’s database role can’t do anything a successful injection shouldn’t accomplish.
The OWASP injection entry and the PostgreSQL extended query protocol documentation are worth reading in full — the protocol page in particular makes it obvious why binding after parsing is a structural guarantee rather than a hygiene habit.
Injection persists because the vulnerable path is always the one that felt special enough to build by hand. Treat “the query is assembled dynamically” as the smell, parameterization as the default, and whitelisting as the boundary where values stop being enough — and the OWASP list gets to keep its famous stale entry a while longer, on someone else’s incident report.