Skip to main content

Security

What is SQL Injection?

Definition

SQL injection is a vulnerability where attacker-supplied input is concatenated into a SQL statement, letting the attacker change what that statement does.

Also known as: SQLi

SQL Injection in more detail

The bug appears whenever a query is assembled as a string. The database receives one blob of text and has no way to know which characters the developer intended as code and which arrived from a form field, so an input containing a quote and a boolean clause becomes part of the query's logic. From there an attacker can bypass a login, read tables they have no business reading, modify or delete rows, and on some configurations read files or run commands on the host.

It is not always visible. Blind SQL injection extracts data one bit at a time from differences the attacker can observe — a page that renders differently for a true condition than a false one, or a time-based variant that injects a sleep and measures the response. Automated tools exploit both quickly, which is why 'the error message does not show anything' is no defence at all.

The fix is parameterised queries, also called prepared statements: the SQL text and the values travel separately, so the database parses the statement first and the input can never become syntax. This is different from escaping, which depends on getting every character class and encoding right and fails in edge cases. Identifiers such as table and column names cannot be parameterised, so when those are dynamic they must be checked against an allow-list. Least-privilege database accounts and stored-procedure discipline limit the damage; they do not prevent the bug.

SQL Injection example

Vulnerable — string concatenation

  query = "SELECT * FROM users WHERE email = '" + email + "'"

  email = anything' OR '1'='1
  -> SELECT * FROM users WHERE email = 'anything' OR '1'='1'
     every row returned; a login check like this passes

Safe — parameterised, values never parsed as SQL

  # Python (DB-API)
  cur.execute("SELECT * FROM users WHERE email = %s", (email,))

  # Node (pg)
  db.query('SELECT * FROM users WHERE email = $1', [email])

  # Java (JDBC)
  ps = conn.prepareStatement("SELECT * FROM users WHERE email = ?");
  ps.setString(1, email);

Dynamic identifiers cannot be parameterised — allow-list them
  sort = ALLOWED[sort] or "created_at"

SQL Injection — common questions

Do ORMs prevent SQL injection?
Mostly, because they parameterise by default. The gap is raw SQL fragments and dynamically built order-by or table names, which many ORMs pass through untouched. An ORM reduces the surface; it does not remove the responsibility.
Is escaping input good enough?
No. Escaping depends on the exact database, character set and context, and it has failed repeatedly in the past. Parameterised queries separate code from data structurally, which is why they are the recommended fix rather than a stricter escaping routine.
What is blind SQL injection?
Injection where the results are never displayed, so the attacker infers data from observable side effects — whether the page changes, whether an error occurs, or how long the response takes. It is slower to exploit but just as effective.

Keep reading

Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.

Browse the A-Z glossary