✳ The reference desk / In practice

SQL injection

A flaw in which untrusted input is joined into the text of a database query, so the database reads part of the input as SQL. A parameterized query removes it by keeping input as a value.

What it means

SQL is the language applications use to ask a database for data. SQL injection happens when code builds a query by joining text the developer wrote with text a user sent. The database receives one string and cannot tell the two apart. A quote in the input can end the developer's string, and whatever follows is read as query logic. An attacker can then read other people's rows, change data, or bypass a login. The fix is a parameterized query. The query's shape is sent first with placeholders, and the input is sent separately as a value that can never become SQL.

See the idea / Applications

Joined into the query text, input becomes SQL.

Send the same input to two versions of the booking search. One joins it onto the query text. The other binds it as a value.

Meridian’s booking search takes a tenant filter from the user and returns that tenant’s bookings. The table holds three bookings: P-0412 and P-0413 for tenant test-7, and P-0502 for tenant test-9. Vera types x' OR '1'='1 into the filter. Compare a query built by joining strings with a parameterized query.

SQL injection: Send the same input to two versions of the booking search. One joins it onto the query text. The other binds it as a value. The chapter text below describes every state.Booking search · tenant filterThree bookings: two test-7, one test-9Input from Verax' OR '1'='1Untrusted text from the formQuery textNot built yetWhat the database is givenHow the database reads itNot parsed yetThe parser reads the whole textRows returnedNo query runA test-7 search should see test-7 rowsINPUT RECEIVED

01 / 04

Receive

Vera types a filter into the booking search. It contains single quotes and the word OR. So far it is only text.

More about this step

The input is x' OR '1'='1. A normal filter, test-7, finds that tenant’s two bookings. Nothing is wrong with a user typing quotes. What happens next depends on how the code hands this text to the database.

Keep in mindOne query, one input and three rows. The test behind this page runs both queries in SQLite. The lesson’s database is PostgreSQL, and both read this input the same way. The lesson’s filter, input and tenant name test-7 are used here. The three bookings are this example’s. Input validation, a web application firewall and a low-privilege database account are useful extra layers and are not shown. Code that joins strings inside a stored procedure is still open to injection.

Read the visual, step by step

Joined string

  1. Receive. Vera types a filter into the booking search. It contains single quotes and the word OR. So far it is only text. The input is x' OR '1'='1. A normal filter, test-7, finds that tenant’s two bookings. Nothing is wrong with a user typing quotes. What happens next depends on how the code hands this text to the database.
  2. Join. The code joins the filter onto the query text. The quote after the x closes the value that the developer opened. The developer wrote SELECT id FROM bookings WHERE tenant = ' and then a closing quote. With the input in the middle, the text becomes SELECT id FROM bookings WHERE tenant = 'x' OR '1'='1'. It is still one valid query, but not the one the developer meant.
  3. Parse. The database parses the whole text. It reads OR '1'='1' as query logic, a condition that is true for every row. The parser cannot tell which characters the developer wrote and which Vera sent. The condition is now: tenant is x, or '1' equals '1'. The second half is always true.
  4. Return. The query returns all three bookings. Two belong to tenant test-7. One, P-0502, belongs to tenant test-9. The tenant filter was the only thing keeping one tenant’s search away from the others’ data, and the input removed it. A search for one tenant has become a read of the whole table.

Parameterized query

  1. Receive. Vera types a filter into the booking search. It contains single quotes and the word OR. So far it is only text. The input is x' OR '1'='1. A normal filter, test-7, finds that tenant’s two bookings. Nothing is wrong with a user typing quotes. What happens next depends on how the code hands this text to the database.
  2. Prepare. The code uses a query with a placeholder where the tenant filter goes. Its shape is fixed, whatever the input says. The text is SELECT id FROM bookings WHERE tenant = $1. It has one condition and one placeholder, and no later value can add another. This is the lesson’s PostgreSQL form. SQLite, which the test uses, writes ? instead.
  3. Bind. The input is bound to the placeholder as one value. Its quotes and its OR are characters in a string, not SQL. With server-side prepared statements the value travels separately from the query text. Some drivers quote the value on the client instead. Either way, the value cannot change the query’s shape. The database compares the tenant column with the whole 12-character string, quotes included.
  4. Return. No booking belongs to a tenant named x' OR '1'='1, so the query returns no rows. The same input that exposed the table is now a search term that matches nothing. A normal filter such as test-7 still returns its two rows.

One query, one input and three rows. The test behind this page runs both queries in SQLite. The lesson’s database is PostgreSQL, and both read this input the same way. The lesson’s filter, input and tenant name test-7 are used here. The three bookings are this example’s. Input validation, a web application firewall and a low-privilege database account are useful extra layers and are not shown. Code that joins strings inside a stored procedure is still open to injection.

Flow references: OWASP: SQL Injection Prevention Cheat Sheet

AN ILLUSTRATIVE SCENARIO

A search that returns another tenant's booking

Meridian's booking search should return the bookings of one tenant. A normal filter, test-7, finds two. The code joins the filter onto the query text. Vera types x' OR '1'='1 into the filter. The finished condition reads tenant = 'x' OR '1'='1'. The database reads the OR as logic that is true for every row and returns all three bookings, including one that belongs to tenant test-9. With a parameterized query, tenant = $1, the same input matches no tenant and returns no rows.

Put it to work

  1. Search the code for queries built by joining or formatting strings, and replace each one with a parameterized query or a safe query API.
  2. Where a table or column name must vary, choose it from a fixed list in the code and never take it from the request.
  3. Give the application's database account only the rights it needs, and add a build check that fails on newly joined query text.

How to check your work

Send a single quote, then a probe such as x' OR '1'='1, to each input that reaches a query in a test environment. No error text should come back, and the result must never be wider than a normal search.

Connect the ideas

  • XSS

    A flaw in which a page includes untrusted data without encoding it, so the browser runs it as script inside the site's own origin. The script can then act as the user who is viewing the page.

  • WAF

    A control that inspects web application traffic and applies rules to detect or block unwanted requests, often including known attack patterns.

  • SAST

    Analysis of source or bytecode for vulnerability patterns without running the program.

  • Vulnerability

    A weakness in a system, process, or person that makes a loss easier to cause.

Explore a field lesson

Find your next idea.

Tip: press / to open search. Escape closes this window.