SKY-D281 · Security in code
SQL injection in a Next.js Server Action
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
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
"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
"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
"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
"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
- Runs with
--dangeron TypeScript and JavaScript files that contain"use server"and a call toquery,exec,execute,raw,sql,$queryRaw,$executeRawor theirUnsafeforms. - 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. - Follows that input through variables,
String(),+, template literals, local helper functions and callbacks inside the action. - 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 asdb,pool,conn,prismaorsql. Loggers,consoleand regular expressions are skipped. - Skips query text that carries no input (fixed strings with placeholders, constants) and Prisma
$queryRawand$executeRawtagged templates on a proven Prisma client. - 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,raworunsafe. 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.
"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--baselineaccept today's findings and report only new ones.