noUnsafeSqliteInterpolation (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/noUnsafeSqliteInterpolation - This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noUnsafeSqliteInterpolation": "error" } } }}Description
Section titled “Description”Disallow inserting values into SQL strings passed to the exec() and prepare() methods of node:sqlite databases.
The DatabaseSync class of the built-in node:sqlite module runs SQL code with its exec() and prepare() methods.
When a template literal inserts a value with ${}, the value becomes part of the SQL code itself.
A value that contains SQL, such as text typed by a user, can then change what the query does.
This is called SQL injection, and it can leak or destroy data.
Even harmless values can break the query, for example a name that contains a quote.
The rule also reports tagged templates, such as String.raw`SELECT 1` , passed to these methods.
The tag function turns the template into a plain string before the method receives it,
so the inserted values are never sent separately from the SQL code.
Instead, write a ? placeholder in the SQL string and pass the values when you run the prepared statement:
import { DatabaseSync } from "node:sqlite";
const database = new DatabaseSync(":memory:");const query = database.prepare("SELECT * FROM users WHERE id = ?");query.get(id);Starting from Node.js 24.9.0, you can also use the tagged templates of createTagStore().
They send each inserted value separately from the SQL code:
import { DatabaseSync } from "node:sqlite";
const database = new DatabaseSync(":memory:");const sql = database.createTagStore();sql.get`SELECT * FROM users WHERE id = ${id}`;The rule only checks databases created with new from the DatabaseSync class imported from node:sqlite with an import statement.
It follows variables declared with const, such as const sql = ...; database.exec(sql),
but not variables declared with let or var, values returned by functions, or modules loaded with require().
It doesn’t report SQL strings joined with +.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”import { DatabaseSync } from "node:sqlite";
const database = new DatabaseSync(":memory:");database.exec(`SELECT * FROM users WHERE id = ${id}`);code-block.js:4:15 lint/nursery/noUnsafeSqliteInterpolation ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This SQL string passed to exec() inserts values with ${}.
3 │ const database = new DatabaseSync(":memory:");
> 4 │ database.exec(`SELECT * FROM users WHERE id = ${id}`);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 │
ℹ Inserted values become part of the SQL code, so a value that contains SQL can change what the query does. This is called SQL injection.
ℹ Write a ? placeholder in the SQL string, and pass the values when you run the prepared statement, for example database.prepare("SELECT * FROM users WHERE id = ?").get(id).
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
import { DatabaseSync } from "node:sqlite";
const database = new DatabaseSync(":memory:");const sql = `SELECT * FROM users WHERE id = ${id}`;database.prepare(sql);code-block.js:5:18 lint/nursery/noUnsafeSqliteInterpolation ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This SQL string passed to prepare() inserts values with ${}.
3 │ const database = new DatabaseSync(":memory:");
4 │ const sql = `SELECT * FROM users WHERE id = ${id}`;
> 5 │ database.prepare(sql);
│ ^^^
6 │
ℹ The SQL string is built here.
3 │ const database = new DatabaseSync(":memory:");
> 4 │ const sql = `SELECT * FROM users WHERE id = ${id}`;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 │ database.prepare(sql);
6 │
ℹ Inserted values become part of the SQL code, so a value that contains SQL can change what the query does. This is called SQL injection.
ℹ Write a ? placeholder in the SQL string, and pass the values when you run the prepared statement, for example database.prepare("SELECT * FROM users WHERE id = ?").get(id).
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
import { DatabaseSync } from "node:sqlite";
const database = new DatabaseSync(":memory:");database.prepare(String.raw`SELECT * FROM users WHERE id = ${id}`);code-block.js:4:18 lint/nursery/noUnsafeSqliteInterpolation ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This SQL string passed to prepare() is built by a tagged template.
3 │ const database = new DatabaseSync(":memory:");
> 4 │ database.prepare(String.raw`SELECT * FROM users WHERE id = ${id}`);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 │
ℹ The tag function turns the template into a plain string before the database receives it, so the inserted values become part of the SQL code. A value that contains SQL can change what the query does. This is called SQL injection.
ℹ Write a ? placeholder in the SQL string, and pass the values when you run the prepared statement, for example database.prepare("SELECT * FROM users WHERE id = ?").get(id).
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
import { DatabaseSync } from "node:sqlite";
const database = new DatabaseSync(":memory:");database.exec("CREATE TABLE users (id INTEGER)");database.exec(`SELECT 1`);database.prepare("SELECT * FROM users WHERE id = ?").get(id);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.