SQL INJECTION

User input becomes database commands

L215 MINSECURITY
PREDICT

The server (PostgreSQL or SQLite) runs: SELECT * FROM users WHERE name = '<name>' AND pass = '<pass>'. You type name = admin' -- and password = x. What happens?

The server builds a query by pasting the input between quotes: WHERE name = ‘ + name + ’. The database parses the whole string as SQL. A quote in the input ends the literal early, and whatever follows is parsed as code: a comment that deletes the password check, or an OR that matches every row.

The fix is to keep code and data apart. Send fixed SQL with ? placeholders and pass the values separately. The database parses the query first, then binds the values. A value cannot become syntax, whatever it contains.

▶ SIMULATION
LOGIN FORM
name: —
password: —
waiting
DATABASE · users (3 rows)
1 admin pass=Zx9!vault
2 alice pass=correct horse
3 o'brien pass=b3ar
passwords: plaintext (second bug: store a salted slow hash)
SERVER
query built by: string concatenation
logged in as: —
logins that returned someone else: 0
✓ a login returns only the user whose name AND password were typed
SCHEDULE
01[SERVER]server builds the SQL by pasting the inputs into quotes
02[USER]alice: name = alice, password = correct horse
03[ATTACKER]attacker: name = ' OR 1=1 --, password = x
04[ATTACKER]attacker: name = admin' --, password = x
05[SERVER]report
Start of string concatenation scenario. 5 steps.
# The input becomes part of the SQL text
sql = ("SELECT * FROM users WHERE name = '" + name +
       "' AND pass = '" + password + "'")
row = db.execute(sql).fetchone()

# name = admin' --
# SELECT * FROM users WHERE name = 'admin' --' AND pass = 'x'
#                                          └─ the rest is a comment
# (PostgreSQL, SQLite, SQL Server. MySQL needs whitespace after --,
#  so there the attacker types "admin' -- " or "admin'#".)

Watch the parsed WHERE row. Under concatenation the attacker’s input changes the shape of the tree. Under bound parameters it stays (name = $1 AND pass = $2) for every input, the attacks return 0 rows, and o’brien logs in.

BREAK IT NOT YET

Three ordinary logins against the concatenating server. (o'brien gets a 500: the apostrophe already breaks the query.) Add one attacker login that gets in as a user whose password the attacker never typed.

GOAL: a login returns a user whose password was not typed

FIX IT NOT YET

Make buildLogin(name, pass) return \{ sql, params \} with ? placeholders and the inputs in params. The grader runs it on the same SQL engine as the sim, with legitimate users (including o'brien) and injection payloads, and checks the SQL text never changes with the input. (Plaintext passwords stay to keep the query readable; see the code tab for the real comparison.)

Mental model:

Injection happens whenever data is pasted into a string that something will parse as code. SQL, HTML and shell commands are all the same bug. The fix is never cleverer quoting. It is an interface that keeps them in separate channels: the query’s structure is fixed before any user value arrives.

IN PRODUCTION

Any query built with string formatting or +: login forms, search boxes, ORDER BY from a query string, raw SQL escape hatches in ORMs. The first sign is often a 500 for names with apostrophes. Drivers and ORMs bind parameters by default; identifiers (table, column, sort direction) cannot be bound and need an allowlist.

IN INTERVIEWS

“What is SQL injection and how do you prevent it?” Show the payload and the query it produces. Say parameterized queries, and why: the statement is parsed before values are bound. Mention least-privilege database accounts and the identifier allowlist, and if a login query is involved, point out the plaintext password too.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: XSS →