CCyberwiki

SQL injection

SQL injection
ClassInjection
Also known asSQLi
CWECWE-89: Improper Neutralization of Special Elements used in an SQL Command
OWASP Top 10A05:2025 Injection
MITRE ATT&CKT1190 Exploit Public-Facing Application
Typical severityCritical
AffectsCode that builds SQL text from request data: raw queries in Node.js, Python and PHP, and SQL built at run time inside database functions

If an application pastes a user's input into the text of an SQL query, the database reads any SQL syntax in that input as part of the query's structure. That vulnerability is SQL injection (SQLi),1 one form of injection.2 With it, an attacker can make the database return rows the application never meant to show and can change or delete data. Some database setups also let the attacker run administrative commands.

Description

Applications send requests to a relational database in SQL, a text language. When code assembles a query by joining fixed SQL text with a value taken from a request, such as a URL parameter, a form field or a cookie, the database receives one combined string. It cannot tell which part was written by the developer and which part came from the user, and if the value contains SQL syntax, the database parses it as SQL.

Hand-written queries are where the flaw lives. Query builders and object-relational mappers, such as the Supabase client and Prisma, send values separately by default. In projects built on them, the injectable code sits wherever someone wrote raw SQL: a database function, or a call to a raw-query method marked as unsafe.

Mechanism

A route in a Node.js application reads an order by its number and builds the query by joining strings:

// Node.js with the pg library
const sql = "SELECT * FROM orders WHERE id = " + req.query.id;
const { rows } = await pool.query(sql);

A request to example.com/api/orders?id=1 returns order 1. A request with the value 1 OR 1=1 produces the following query:

SELECT * FROM orders WHERE id = 1 OR 1=1

The condition 1=1 is true for every row, so the database returns every order of every customer. Other inputs can append a UNION SELECT clause to read other tables, or end the statement and start a new one where the database driver accepts several statements in one call.

(a) Input joined into the query textInput1 OR 1=1Query text received by the databaseSELECT * FROM orders WHERE id =1 OR 1=1parsed as SQL: true for every row(b) Input sent as a parameterInput1 OR 1=1Parameter $1 (one value)1 OR 1=1Query text, fixed in the codeSELECT * FROM orders WHERE id = $1The parameter and the query text reach the database separately. The whole input is comparedwith the id column as one value, and the query returns no rows.
Figure 1. The same input sent in two ways. (a) Joined into the query text, it becomes part of the SQL. (b) Sent as a parameter, it reaches the database as a single value and the query text is unchanged.

Impact

What an attacker gets depends on the permissions of the database account the application connects with. With a typical application account, an attacker can read every table that account can read, including user records and password hashes, and can insert, change or delete rows. Where the account has administrative rights, some database systems also allow the attacker to read files on the database server. When the vulnerable route is public, no account in the application is needed, and the attack is the technique of exploiting a public-facing application (T1190).3

Detection

Search the source code for SQL text next to a + operator, inside a template string that contains ${, in a Python f-string or % formatting, and for the Prisma methods $queryRawUnsafe and $executeRawUnsafe. In Postgres functions, search for EXECUTE followed by text joined with ||. A match is a vulnerability when the joined value can come from a request, a form, a cookie or the URL.

Automated testing tools send inputs such as a single quote or 1 OR 1=1 and compare the responses. A database error, or a change in the number of rows returned, shows that the input reaches the query text.

Prevention

Parameterized queries

The primary defence is the parameterized query, also called a prepared statement or bound parameters.4 The query text contains placeholders and the values travel separately, so the database never parses a value as SQL:

// Node.js with the pg library
const { rows } = await pool.query(
  "SELECT * FROM orders WHERE id = $1",
  [req.query.id]
);

With this form, the input 1 OR 1=1 is compared with the id column as one value. It is not a valid ID, so the query returns no rows.

Safe forms by library

LibrarySafe formUnsafe form
Node.js, pgpool.query("... WHERE id = $1", [id])SQL joined with + or ${}
Prismaprisma.$queryRaw`... WHERE id = ${id}`$queryRawUnsafe, $executeRawUnsafe
Supabase JSsupabase.from("orders").select().eq("id", id)Raw SQL inside database functions
Python, psycopgcur.execute("... WHERE id = %s", (id,))f-strings and % on the SQL text
PHP, PDO$st = $pdo->prepare("... WHERE id = ?");
$st->execute([$id]);
$pdo->query() with joined text
Postgres functionEXECUTE format('SELECT * FROM %I WHERE id = $1', tbl) USING idEXECUTE with ||

Identifiers and sort order

Placeholders stand for values only. A table name, a column name or a keyword such as ASC cannot be passed as a parameter.4 When the user chooses one of these, map the choice to a fixed set of values defined in the code and reject anything else:

const SORT = { newest: "created_at DESC", price: "price ASC" };
const order = SORT[req.query.sort] ?? SORT.newest;

In a Postgres function, format() with %I quotes an identifier and %L quotes a literal value; %s inserts text without quoting.

Prompt for a coding agent

Find every database query in this project that is built by joining text with values that come from a request, a form, a URL or a cookie. That includes string concatenation, template strings, f-strings, format() calls, $queryRawUnsafe, $executeRawUnsafe and EXECUTE inside Postgres functions. Rewrite each one to pass the values as query parameters through the database library. Where a column name or sort order comes from input, map it to a fixed list of allowed names. Keep what each query returns the same. List each file and line you changed, and any query you were unsure about.

In SecFlash

SecFlash reports SQL text built from strings as SF-SEC-704 on the Security page, with the file and line, and as SF-SEC-756 when the query is inside a database function. When the attack test changes a query through a live request, it reports SF-SEC-601.

See also

References

  1. back "CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')". Common Weakness Enumeration. MITRE.
  2. back "A05:2025 Injection". OWASP Top 10:2025. OWASP Foundation.
  3. back "Exploit Public-Facing Application, T1190". MITRE ATT&CK.
  4. a b "SQL Injection Prevention Cheat Sheet". OWASP Cheat Sheet Series.