Skip to main content

SKY-D281 · Security in code

SQL injection in a Next.js Server Action

Critical

A Next.js Server Action is a server function marked with "use server" that a form or a button in the browser can call directly. SKY-D281 reports a Server Action that pastes its input into the text of a SQL query, for example in a template literal passed to pool.query() or prisma.$queryRawUnsafe().

Languages
TypeScript/JavaScript
Quality gate
counted toward the gate limits; a CRITICAL finding fails the gate on its own (full and diff scans)
Finding type
review
Catalog name
Next.js server action SQL injection
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

Every Server Action is a public endpoint: the browser sends its arguments and anyone can send other ones. A title such as x' WHERE 1=1; -- turns an update of one row into an update of every row, and other inputs can read or delete whole tables.

What Skylos flags

app/posts/actions.ts · TypeScriptReported on line 10
"use server"; import { revalidatePath } from "next/cache"; import { pool } from "@/lib/db"; export async function renamePost(formData: FormData) {  const id = String(formData.get("id"));  const title = String(formData.get("title"));  await pool.query(`UPDATE posts SET title = '${title}' WHERE id = ${id}`);  revalidatePath("/posts");}

What Skylos leaves alone

app/posts/actions.ts · TypeScript
"use server"; import { revalidatePath } from "next/cache"; import { pool } from "@/lib/db"; export async function renamePost(formData: FormData) {  const id = String(formData.get("id"));  const title = String(formData.get("title"));  await pool.query("UPDATE posts SET title = $1 WHERE id = $2", [title, id]);  revalidatePath("/posts");}

Why Skylos leaves it alone

The same action, but the SQL text is a fixed string with $1 and $2 placeholders, and the form values travel separately in the array. No input reaches the SQL text, so nothing is reported.

More languages

JavaScript

Flagged

app/users/actions.js · JavaScriptReported on line 9
"use server"; import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient(); export async function findUser(formData) {  const email = String(formData.get("email") ?? "");  return prisma.$queryRawUnsafe(`SELECT id, name FROM users WHERE email = '${email}'`);}

Not flagged

app/users/actions.js · JavaScript
"use server"; import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient(); export async function findUser(formData) {  const email = String(formData.get("email") ?? "");  return prisma.$queryRaw`SELECT id, name FROM users WHERE email = ${email}`;}

Why Skylos leaves it alone

It looks like the same template, but $queryRaw is a tagged template on a client made with new PrismaClient() from @prisma/client. Prisma turns each ${…} into a bound parameter, so Skylos treats it as safe. $queryRawUnsafe with a template literal pastes the text in.

How Skylos decides

  1. Runs with --danger on TypeScript and JavaScript files that contain "use server" and a call to query, exec, execute, raw, sql, $queryRaw, $executeRaw or their Unsafe forms.
  2. Treats as Server Actions the exported functions of a file that starts with "use server", and any function whose body starts with it. Every parameter of such a function is untrusted input.
  3. Follows that input through variables, String(), +, template literals, local helper functions and callbacks inside the action.
  4. Checks that the receiver is a database: a value imported from or made by a SQL library (pg, mysql, @prisma/client, knex, drizzle-orm, sequelize, typeorm, SQLite drivers and others), or a name such as db, pool, conn, prisma or sql. Loggers, console and regular expressions are skipped.
  5. Skips query text that carries no input (fixed strings with placeholders, constants) and Prisma $queryRaw and $executeRaw tagged templates on a proven Prisma client.
  6. Reports CRITICAL on the SQL text when the input reaches it and the text contains a SQL keyword, or is the input itself, or the method is $queryRawUnsafe, $executeRawUnsafe, raw or unsafe. The general SQL injection finding (SKY-D211) for the same call is dropped.

How to fix it

Keep the SQL text fixed and pass each value as a parameter: $1, $2 with an array for pg, ? for MySQL and SQLite, or Prisma's $queryRaw tagged template. Check in the action that the caller may change the record, since Server Actions are public.

app/posts/actions.ts · TypeScript · the fix, against the flagged file
"use server"; import { revalidatePath } from "next/cache"; import { pool } from "@/lib/db"; export async function renamePost(formData: FormData) {Removed:   const id = String(formData.get("id"));Added:   const id = Number(formData.get("id"));  const title = String(formData.get("title"));Removed:   await pool.query(`UPDATE posts SET title = '${title}' WHERE id = ${id}`);Added:   await pool.query("UPDATE posts SET title = $1 WHERE id = $2", [title, id]);  revalidatePath("/posts");}

When it can be wrong

  • A value checked against an allowlist before it is pasted in, such as a sort column, is still reported: the check does not recognise allowlists.
  • Missed: SQL built in a helper imported from another file, and query calls on a receiver whose name and origin do not look like a database. SQL in ordinary route files and other code is SKY-D211.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Mark the line with the SQL text and say why the value is safe.

    TypeScript
    await db.query(`SELECT * FROM t ORDER BY ${col}`); // skylos: ignore[SKY-D281] allowlist
  • [tool.skylos] ignore = ["SKY-D281"] turns the rule off for the whole project, which hides real injections too.

  • skylos baseline . and then --baseline accept today's findings and report only new ones.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.