What is SQL injection, and how do you fix it?
Updated 2026-11-17
Short answer
SQL injection is a flaw where text supplied by a user becomes part of a database query and changes what the query does. The fix is to stop building queries by joining strings and pass the values as parameters instead.
How it happens
The code takes a value from a request and concatenates it into a query string. If the value contains SQL, the database runs it. An attacker can then read data they should not see, change it or delete it.
How to spot it in code
- A query built with string concatenation or string formatting.
- A value that comes from a request, a file or another system and reaches that query.
- Dynamic table or column names taken from input.
How to fix it
- Use parameterized queries or prepared statements. The value is sent separately from the query text.
- If you use an ORM, use its query methods and avoid its raw-query escape hatch.
- For table or column names, choose from a fixed list instead of using the input.
- Give the database account only the permissions it needs.
How scancodebase finds it
The scanner follows the value from where it enters the program to the query, across functions and files, and shows you that path with a proposed fix.