Skip to content

QuizNot attempted

Fixing SQL injection

What is the correct fix for this query?
db.query(`SELECT * FROM users WHERE id = '${req.params.id}'`);
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Use a parameterized query: db.query('SELECT * FROM users WHERE id = ?', [id])
  • Parameterized queries send the SQL and the values separately, so the driver never interprets input as SQL, whatever characters it contains.
  • Not stripping quotes: blacklists miss encodings, numeric contexts and other metacharacters.
  • Not frontend validation: an attacker calls the API directly and skips it.
  • Not try/catch: it only hides errors
  • the injected query still runs.