Security rules

NGB073: SQL injection in server code

Traces request data (Express / Node query, params, body, cookies, headers) into SQL text executed by server code — SSR servers, BFFs and API routes in the workspace: query() / execute() / raw() … on clients of pg, mysql / mysql2, sqlite3 / better-sqlite3, mssql, knex, sequelize, typeorm and Prisma's $queryRawUnsafe() / $executeRawUnsafe().

Property Value
Rule ID NGB073
Category Angular SSR security
Default severity critical
CWE CWE-89
Built-in presets recommended: error, strict: error

Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.

What it checks

Traces request data (Express / Node query, params, body, cookies, headers) into SQL text executed by server code — SSR servers, BFFs and API routes in the workspace: query() / execute() / raw() … on clients of pg, mysql / mysql2, sqlite3 / better-sqlite3, mssql, knex, sequelize, typeorm and Prisma's $queryRawUnsafe() / $executeRawUnsafe(). Only the SQL string argument is checked; bound parameters are safe.

Why it matters

Concatenating or interpolating request values into SQL lets an attacker change the statement: read other users' rows, bypass login checks, modify or drop data. Parameterized queries keep data out of the SQL grammar and are supported by every driver.

Detection behavior and supported patterns

  • db.query(SELECT … ${req.query.id}), pool.query('… ' + req.body.name), connection.execute(sql) where sql is built from request data (receivers typed as an SQL package, or named db / pool / client / connection / knex … in files that import one)
  • knex.raw(…), .whereRaw(…), .orderByRaw(…), sequelize.query(…), dataSource.query(…), prisma.$queryRawUnsafe(…), sql.unsafe(…) with request data in the SQL text
  • Not reported: placeholders with bound values (query("… WHERE id = $1", [id])), tagged templates (sql…, `prisma.$queryRaw`…), values passed through an escaping helper (mysql.escape(), pg-format) or numeric conversion

Example

TypeScript
await pool.query(`SELECT * FROM orders WHERE customer = '${req.query.customer}'`);

Safer alternative

TypeScript
await pool.query('SELECT * FROM orders WHERE customer = $1', [req.query.customer]);

Remediation

Use placeholders and pass values separately ($1 / ? with a values array, Prisma $queryRaw tagged templates, knex bindings). Choose dynamic identifiers (sort column, direction) from a fixed map instead of inserting the request value.

False-positive considerations

  • A column name chosen from request data but checked against an allowlist in a separate function is reported; inline the check or document an exception.

Known limitations

  • ORM query builders with object conditions are not analyzed (they parameterize values).
  • SQL assembled in helper functions is followed only as far as the taint engine follows the flow; dynamic identifiers (table / column names) validated by an allowlist are still reported.

References

Run it

Shell
ngbastion explain NGB073