Question presented to candidate: "Our login page builds its query by gluing the username from the form into a SQL string. A security review flagged it as SQL injection. What does that mean, why is it dangerous, and how do parameterized queries fix it?"
What a strong answer should cover:
- Definition: untrusted input is pasted into the SQL text, so the parser reads part of the input as SQL instead of as a value
- Why escaping by hand is fragile, and why a bound parameter is different: the SQL text and the values travel separately, so a value can never change the shape of the query
- Where parameters cannot help: table names, column names,
ORDER BYdirection and keywords cannot be placeholders, so they need an allowlist (orquote_ident/format('%I')in PostgreSQL) - Defence in depth: least-privilege database roles, ORM raw-query APIs used safely (for example Prisma
$queryRawtagged template vs$queryRawUnsafe), input validation as a second layer, not the fix - Second-order injection: data stored safely can still be unsafe later if another piece of code concatenates it into dynamic SQL
Code / implementation expected: Yes β show a concatenated query going wrong and the same lookup written with a placeholder.
Schema used by the examples: