Server Action Validation
Server actions using formData without Zod/schema validation
criticalSecurity
next-server-action-validationWhy this matters
Server actions receive untrusted input from the client. Using formData.get() without validation is the same as trusting user input — injection, type confusion, and data corruption follow.
Example
app/actions/posts.ts"use server";
export async function createPost(formData: FormData) {
const title = formData.get("title") as string;
const body = formData.get("body") as string;
await db.post.create({ data: { title, body } });
}app/actions/posts.ts"use server";
import { z } from "zod";
const schema = z.object({
title: z.string().min(1).max(200),
body: z.string().min(1).max(10000),
});
export async function createPost(formData: FormData) {
const data = schema.parse({
title: formData.get("title"),
body: formData.get("body"),
});
await db.post.create({ data });
}How to fix
Validate all formData fields with Zod or a similar schema library inside the server action before using the values.
Reference
- In the output
- Findings carry
"ruleId": "next-server-action-validation"in--jsonand the same id in SARIF and in the terminal. - Silence one line
// prodlint-disable-next-line next-server-action-validation- Silence a whole file
// prodlint-disable next-server-action-validationat the top of the file- How it works
- Matches text patterns. Source on GitHub