nextjs-server-action-validation
Next.js Server Action Validation
Concept of the skill
What it is: The validation and authorization discipline for Next.js Server Actions that accept client-originated input. Mental model: A Server Action is a public POST endpoint wearing function-call syntax. Why it exists: Convenience syntax can hide the HTTP boundary, leading developers to skip auth, input validation, or org scoping. What it is NOT: It is not GET route handling, Server Component data fetching, or Stripe webhook verification. Adjacent concepts: Zod schemas, auth checks, org-scoped mutations, controlled error surfaces. One-line analogy: It is the security checkpoint every form submission crosses before touching the database. Common misconception: Only the project UI can call the action; any HTTP client can call the generated endpoint.
Coverage
- The public endpoint reality —
'use server'functions are exposed as POST endpoints that any HTTP client can call directly; the Next.js call graph is not a security boundary - Validation order — auth check BEFORE Zod parse, Zod parse BEFORE database access; any other order produces an exploitable window
- Zod schema design for actions — using
.safeParse()over.parse()to return structured errors rather than throwing; returning{ error: ... }shapes that the calling Client Component can render - Org-scoping after authentication — verifying that
input.orgIdmatches the session's org, not just that the user is logged in - Error surface control — how
try/catcharound database calls prevents internal errors from propagating to the client response