An SQL statement is built from a template or concatenation with values

tx0/ts:sql-built-from-stringsrecommendedwarningsecurity/injectiondepth fastsecurity, cwe-89

What it finds

An SQL statement assembled from strings and values, as in ` db.query(SELECT * FROM users WHERE id = ${id}) `, lets a value that contains quotes change the statement: SQL injection. Placeholders with separate parameters, or a tagged template that the driver escapes, keep values out of the statement text.

Reported is the first argument of query, execute, exec, raw, $queryRawUnsafe and $executeRawUnsafe when it is a template with substitutions or a + concatenation of literals and other values, other than constants written in upper case, and its literal parts read as SQL: they start with SELECT, INSERT, UPDATE, DELETE, REPLACE, MERGE, CREATE, ALTER or DROP followed by a clause that belongs to it. Statements whose values are all fixed when the code is written are left alone: literals, numbers, .length, identifiers quoted by the driver (escapeId) and lists of placeholders such as ids.map(() => "?").join(", ").

From the rule's source, sql_built_from_strings.rs.

Example from the rule's tests
export async function find(db: { query(q: string, p?: unknown[]): Promise<unknown> }, id: string, table: string) {  await db.query(`SELECT * FROM users WHERE id = ${id}`);tx0 reports: values are written into this SQL statement  await db.query("DELETE FROM sessions WHERE user = '" + id + "'");tx0 reports: values are written into this SQL statement  await db.query("SELECT * FROM users WHERE id = $1", [id]);  await db.query(`select ${table}`);  await db.query(`Hello ${id}, see the FROM field`);  await db.query(`SELECT * FROM t LIMIT ${MAX_ROWS}`);}declare const MAX_ROWS: number;declare function escapeId(name: string): string;export async function fixed(db: { query(q: string, p?: unknown[]): Promise<unknown> }, ids: string[], table: string, page: number) {  await db.query(`SELECT * FROM t WHERE id IN (${ids.map(() => "?").join(", ")})`, ids);  await db.query(`SELECT * FROM ${escapeId(table)} WHERE id = ?`, [ids[0]]);  await db.query("SELECT * FROM t LIMIT 20 OFFSET " + page * 20);  await db.query(`SELECT * FROM t WHERE id IN (${ids.join(", ")})`);tx0 reports: values are written into this SQL statement}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:sql-built-from-strings" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:sql-built-from-strings] reason="..."

tx0 explain tx0/ts:sql-built-from-strings shows the same in the terminal. Found a false positive? Report it.