SQL Injection and Parameterised Queries
What goes wrong when data is pasted into a query string, and the one fix that actually works.
SQL Injection and Parameterised Queries
The same lookup, four ways
What goes wrong when data is pasted into a query string, and the one fix that actually works.
The same lookup, four ways
If an application builds SQL by concatenating user text into a query, special characters can change what the database executes. An attacker may bypass a login check, read data, or alter rows depending on the query and permissions. Escaping strings by hand is error-prone because quoting rules differ across drivers and contexts.
If an application builds SQL by concatenating user text into a query, special characters can change what the database executes. An attacker may bypass a login check, read data, or alter rows depending on the query and permissions. Escaping strings by hand is error-prone because quoting rules differ across drivers and contexts.
Use the database driver's parameterized-query API. The SQL statement contains placeholders, and the driver sends user values separately as data.
The placeholder syntax varies by driver. Bind the supplied email through that API instead of inserting it into the SQL string. Parameterization also lets the database reuse query plans in some systems, but the security property is the separation of values from SQL code.
Parameters represent values, not SQL identifiers or keywords. If a user can choose a sort column or table, map that choice through a strict allowlist of known identifiers and construct the query from that trusted mapping. Never treat arbitrary input as a column name.
Parameterization prevents a class of injection, but it does not replace authorization, validation, or database permissions. Give the application account only the access it needs, avoid exposing detailed database errors to users, and keep dependencies current. See DML for safe row changes.
For a sort option, map a small public choice such as “newest” to a fixed SQL fragment such as created_at DESC. Do not bind the string as though a value placeholder could stand for the column name, and do not concatenate an arbitrary request parameter. Bind the actual page size and filter values separately. Review every query-building path, including search, exports, and administrative endpoints; one safe login query does not protect the rest of the application.
A useful security test supplies quotes, comment markers, wildcard characters, and Unicode in fields that are meant to be ordinary text. The value should remain data and must not change the query's meaning. Parameterization does not decide whether that value is authorized or valid, so enforce length, format, and ownership rules separately. Review logs and error paths so a rejected input does not disclose schema details. Treat each database driver’s placeholder syntax as part of its API and avoid mixing string formatting with bound values.
Answer without scrolling back up.
Why does a parameterised query prevent injection?
It is a structural guarantee rather than a filter: there is no input for which a bound parameter turns into query structure.
Why is escaping an inadequate defence?
WHERE id = 1 OR 1=1 contains no special characters at all. Parameterisation avoids the whole class by never putting the value in the query text.
How should a user-supplied ORDER BY column be handled?
Placeholders stand for values, not identifiers. Identifiers must be chosen from a fixed set rather than sanitised.
Build a query with string concatenation, inject a payload that drops a table, then switch to a parameterised query that blocks it completely.