An SQL statement is built from a template or concatenation with values
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.
From the rule's source, sql_string_building.rs.
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; 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:
[rules]
"tx0/ts:sql-string-building" = "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-string-building] reason="..."tx0 explain tx0/ts:sql-string-building shows the same in the terminal. Found a false positive? Report it.