QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › SQL in QA work

What is SQL injection and how do you test for it?

SQL injection happens when a query is built as a string and user input becomes part of its text, so it is parsed as code. Parameterised queries prevent it: the value is passed separately and never parsed as SQL. A parameter does not cover column names or sort direction, which need an allowlist. QA checks every input with a quote and a "1=1" condition; destructive probes run only on an authorised environment.

SQL · QA practice

A single quote typed into the search field returned a database error instead of results. Is that already a bug?

A tester types O'Brien into search and sees a 500 error with the text syntax error at or near. This is not only a failure on a surname with an apostrophe: the input reached the query as code, so with another string the query can be changed. This lesson is about why it happens, how a parameter differs from concatenation and how to check such places without harming the environment.

How it works and why

SQL injection happens when a query is built as a string and user input becomes part of the query text. The database server cannot tell where the developer's code ends and foreign characters begin, so a quote closes a string literal and the rest of the input is read as SQL. In a parameterised query the query text and the values are sent separately: the query is parsed first with the placeholder $1, and the value is supplied afterwards as data and is never parsed as SQL. So a quote in a value is just a quote.

// vulnerable: the input becomes part of the query text
await client.query(
  "SELECT * FROM users WHERE email = '" + email + "'",
);

// safe: the value is passed separately
await client.query('SELECT * FROM users WHERE email = $1', [email]);

Example: what one string changes

The table has three users. The input ' OR '1'='1 in vulnerable code gives the condition email = '' OR '1'='1', true for every row, so the query returns everyone. A parameterised query looks for a user whose email literally equals this string and finds nobody.

The same input in two implementations

Concatenation
idemail
1anna@test.com
2ivan@test.com
3olga@test.com
Parameter $1
idemail
Figure 1. On the left all users came back, on the right nothing: the input was data.

How QA checks it. The input does not have to be harmful, probes that reveal the behaviour are enough:

InputWhat we observe
'a 500 error or database error text means the quote is not handled
' OR '1'='1more results than there should be
anna@test.com' AND '1'='1 and anna@test.com' AND '1'='2different results when only the truth changes: the condition is executed
1 OR 1=1a numeric parameter without quotes

The expected correct behaviour: the input is handled as plain text ("nothing found" or a 400 validation error), the response has no SQL errors or table names, the response shape is the same. Check all places where input reaches the database: search, filters, sorting parameters, JSON fields, headers, login. Destructive probes such as ; DROP TABLE are not run on a shared environment, and probes with a delay (pg_sleep) and automatic scanners are used only on an environment where it is permitted.

Limits and alternatives

A parameter protects values but not column and table names, keywords and sort direction. A placeholder does not work for ORDER BY ${sort}, an allowlist of permitted values is needed here. An ORM does not guarantee safety: raw queries with string interpolation are just as vulnerable. Manual escaping is unreliable, and the rights of a database user with minimal privileges only reduce the damage but do not remove the vulnerability. Manual probes reveal the obvious, and a systematic check is done with automated tools with the system owner's permission.

Common mistakes

  • Closing the "500 on a quote" bug with a filter that strips quotes: this does not remove the cause and breaks surnames like O'Brien.
  • Checking only the search field and forgetting sorting, filters and headers.
  • Assuming an ORM rules out injection: a raw query with interpolation is vulnerable.
  • Running destructive probes on a shared environment.
  • Not noticing that database error text in a response is already an information leak.

Check yourself

Search on ' returns 500 and the body has syntax error at or near. Which defects are here?

Answer

Two. The input reaches the query as code (a potential injection), and the response reveals internal database details. File them as separate bugs, not one.

A sort parameter is put into ORDER BY. Why does the placeholder $1 not help here and what to do?

Answer

A placeholder replaces a value, not an identifier or a keyword, so ORDER BY $1 sorts by a constant string. Accept only values from a list of allowed columns and directions, and reject the rest with a 400.

In an interview. Junior: SQL injection is when user input reaches the query as code. The defence is parameterised queries. I check search fields and filters with basic strings containing a quote and the "one equals one" condition and expect them to be handled as plain text, with no database errors in the response. Middle/Senior: I understand the mechanism: parameters are sent separately from the query text, so a value is never parsed as SQL. I know the boundary: identifiers and sorting cannot be protected by a parameter, an allowlist is needed. I check all inputs, not only search, tell a handling error from a leak, and run destructive probes and scanners only with permission.

More articles in this chapter