312-50 ยท SQL injection
SQL injection countermeasures
Parameterized queries, stored procedures, input validation, least-privilege database accounts, WAF, error handling, and detection.
Key points
- Primary fix: parameterized queries (prepared statements) keep code and data separate.
- Safe stored procedures work too, but dynamic SQL inside them is injectable.
- Allow-list validation for values that cannot be bound (table, column names).
- Least-privilege database accounts.
- Generic user errors, detailed server-side logs.
- WAF as a compensating layer; monitoring to detect attacks.
Source: https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html
Practice questions
Which measure is the preferred primary defence against SQL injection?
- Hiding database errors from users
- Parameterized queries (prepared statements)
- Blocking the apostrophe character in a client-side script
- Renaming database tables
Show answer
Answer: B. Parameterized queries (prepared statements)
Parameterized queries ensure input is always treated as data. A WAF or error hiding only reduces exposure.
Which controls reduce SQL injection risk?
- Prepared statements with bound parameters
- A least-privilege database account
- Allow-list validation of values like sort column names
- Showing full database errors to users
- Relying only on browser-side validation
Show answer
Answer: A. Prepared statements with bound parameters, B. A least-privilege database account, C. Allow-list validation of values like sort column names
Parameterization, least privilege and allow-list validation all help. Showing full errors leaks information, and trusting client-side checks alone is easily bypassed.
Any stored procedure is automatically safe from SQL injection.
Show answer
Answer: False
False. A procedure that builds dynamic SQL by concatenating its arguments is still injectable; only safe, parameterized procedures protect.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
