Use this when user input reaches SQL queries.
SQL Injection
SQL injection inserts SQL through user input to read, change or delete database data. Parameterized queries or prepared statements prevent it; escaping alone is not enough.
1. SQL injection: untrusted input changes SQL queries.
2. Use parameterised queries/ORMs.
3. Allowlist dynamic identifiers.
4. Least-privilege DB accounts.
5. WAF as backup.
input → parameter binding → query (no injection)
Context & applicabilityCommon mistakes · platform/version boundaries
- Escaping instead of parameterising.
- Parameterising values but building table or column names from input — use allow-lists.
- ORMs used with raw query functions (
raw(),query()) and string concatenation. - Stored procedures that build dynamic SQL internally.
Operational reference
Fast context before you go deeper.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Use prepared statements.
- Escaping alone is not enough.
- Use least-privilege database accounts.
sqlmap -u '<url>?id=1' --batch --level=1 (authorised test only)— detect SQLi- Code review: parameterised queries — prepared statements
- DB account privileges — least privilege
ReferenceWhy it matters1 section · depth on demand
Example and fix
Vulnerable:
query = "SELECT * FROM users WHERE name = '" + name + "'"
Input ' OR '1'='1 returns every user.
Fixed (parameterised):
cur.execute("SELECT * FROM users WHERE name = %s", (name,))
Also: least-privilege database accounts, no detailed SQL errors to users, and an allowlist for identifiers (table/column names) that cannot be parameterised.
ReferenceImportant terms & boundaries2 sections · depth on demand
Detection and testing
sqlmap -u "https://test/item?id=1" --batch(only on systems you are authorised to test).- Error messages with SQL syntax, or behaviour changes with
'and' OR '1'='1. - WAF/database logs showing
UNION SELECT,SLEEP(,xp_cmdshell.
The database account should have least privilege, so a successful injection cannot drop tables or read other schemas.
In depth
SQL injection occurs when user input is concatenated into a database query, letting an attacker change the query's meaning — reading, modifying or deleting data, or bypassing authentication. It remains one of the most damaging web vulnerabilities.
Prevention:
| Control | Notes |
|---|---|
| Parameterised queries / prepared statements | The primary fix — input is always data, never code |
| ORM query APIs | Safe when used without raw string building |
| Allow-list validation | For values that cannot be parameterised (sort columns) |
| Least-privilege DB accounts | Limit damage |
| WAF | Additional layer, not a fix |
cur.execute("SELECT * FROM devices WHERE site = %s", (site,)) # safe
# never: cur.execute(f"SELECT * FROM devices WHERE site = '{site}'")
Detect in testing with SAST/DAST tools and code review of any raw SQL.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Web Security → SQL Injection. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://owasp.org/www-project-application-security-verification-standard/ · confidence 94%