/ THE IDEA
The problem is mixing two roles in the same text: the trusted instruction and the untrusted value it operates on. Quotation marks have meaning to the query reader, so merely surrounding a supplied name with quotes does not reliably preserve that boundary. A parameterized query sends a fixed instruction shape with separate data values. A placeholder marks where a value belongs. The database interface binds the supplied value there as data, rather than reparsing its characters as extra query structure. This is different from checking whether a name contains suspicious punctuation. Real names legitimately contain punctuation, and rules designed to guess every dangerous string are fragile. Keep the code/data separation even after checking value type and length for the application’s ordinary requirements.
THE FORMAL IDEA
SELECT id FROM customers WHERE surname = ?
| SELECT asks for data; id is the requested field; customers is the fixed table. | | WHERE restricts results; surname is the fixed field to compare; ? is a parameter placeholder. Exact placeholder notation depends on the database interface. |
|
RUN THE TINY EXAMPLE
Change one thing. Predict what happens.
Bind the value Smith → search for the surname Smith. Change only the value to O'Neil, containing the straight quote used by SQL → search for the complete surname O'Neil. The instruction still contains one equality comparison and one value. Do not paste the supplied name into the instruction text before sending it.
|
The changed input changes which value is matched, not what the database has been instructed to do. This distinction is the security mechanism. The example uses an ordinary name to show that safe handling should also preserve correct everyday behaviour.
/ SO WHAT?
Parameters usually represent values, not arbitrary table names or sorting keywords. If a screen offers newest-first or oldest-first, map that choice to a small set of fixed instructions controlled by the application. A database account should also have only the access the feature needs, limiting consequences if another defect appears.
ONE CAVEAT |
| Parameterization does not decide whether the caller may view a returned record. Dynamically constructed queries inside a stored procedure can still be unsafe, so trace where the final instruction is built. |
KEEP THIS
Keep query structure fixed and bind changing values separately so user data cannot acquire the role of database instructions.
|
NEXT: What keeps a nuclear chain reaction going?
|