Pcornel
Browse topics
Options
Recent
Favorites
Security · Technical reference

SQL Injection

Direct answer

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.

When to use this

Use this when user input reaches SQL queries.

First 30 seconds

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.

Operational verification

input → parameter binding → query (no injection)

Context & applicabilityCommon mistakes · platform/version boundaries
Common mistakes
Common mistakes
  • 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.
Platform / version applicability
Security architecture and operations
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformSecurity architecture and operations
Scope / prerequisiteUse this when user input reaches SQL queries.
What next?2 suggested next steps
Evidence1
On this pageJump to a specific section
Evidence references

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.

observation · 1
Key points
  • Use prepared statements.
  • Escaping alone is not enough.
  • Use least-privilege database accounts.
Key checks
  • 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:

ControlNotes
Parameterised queries / prepared statementsThe primary fix — input is always data, never code
ORM query APIsSafe when used without raw string building
Allow-list validationFor values that cannot be parameterised (sort columns)
Least-privilege DB accountsLimit damage
WAFAdditional 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docSecurity architecture and operations current

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%
Confirmation

Confirm action

Continue?