Skip to main content

Every Next.js Server Action is a public endpoint. Audit the ones Cursor or v0 wrote for you

Next.js says every exported action is reachable by direct POST. The four patterns AI tools get wrong, a safer action, and a 20-minute audit using the Next.js checklist.

Updated 6 min readMarkdown

If your Next.js app was written by Cursor, v0, Claude Code or Bolt, it probably has files that start with 'use server'. Those are Server Actions: the functions behind your forms and buttons that update a profile, delete a project, change a plan or invite a teammate.

They feel like private functions. They aren't. The Next.js docs say it plainly: "when a Server Action is created and exported, it is reachable via a direct POST request, not just through your application's UI."

That one sentence explains a whole class of bugs in AI-written apps. Here's what Next.js protects for you, what it doesn't, and a 20-minute audit you can run on your own code.

What Next.js does for you

The data security guide lists two built-in protections:

  • Secure action IDs. The client calls an action by an encrypted, non-deterministic ID. The IDs are cached for at most 14 days and regenerated on a new build.
  • Dead code elimination. Actions your app never uses are removed at build time, so they don't become endpoints.

There's also a CSRF guard. Server Actions only accept POST, and Next.js compares the Origin header with the Host header (or X-Forwarded-Host). If they don't match, the request is aborted.

Next.js calls the ID scheme a way to reduce risk "in cases where an authentication layer is missing." Then it adds the part that matters: "you should still treat Server Actions as reachable via direct POST requests and verify authentication and authorization inside each one." An ID is not a password. Anyone who can use your app can see the IDs in their own browser's network tab.

The four patterns that go wrong

1. The page checks auth, the action doesn't. AI tools often guard a page: if there's no session, redirect to /login. The action defined on that page has no check of its own. Next.js is explicit: "A page-level authentication check does not extend to the Server Actions defined within it. Always re-verify inside the action."

2. The proxy (middleware) is the only lock. Many generated apps put auth in middleware.ts, which Next.js 16 renamed to proxy.ts. That's fine as a first pass, but not as the only check. In March 2025, CVE-2025-29927 let attackers skip middleware entirely on self-hosted Next.js by sending an internal x-middleware-subrequest header. It was patched in 15.2.3, 14.2.25, 13.5.9 and 12.3.5. In its postmortem, the Next.js team wrote: "We do not recommend Middleware to be the sole method of protecting routes in your application."

3. Signed in, but not the owner. The action checks that someone is logged in, then trusts an ID from the form:

'use server'
export async function deleteProject(formData: FormData) {
  const session = await auth()
  if (!session) throw new Error('Unauthorized')
  await db.project.delete({ where: { id: formData.get('projectId') as string } })
}

Any signed-in user can delete any project by changing projectId. The Next.js audit checklist asks exactly this: "Does the action check ownership of the resource (authorization, not just authentication)?"

4. The form becomes the update. Code like db.user.update({ data: Object.fromEntries(formData) }) writes whatever fields the request includes, including role, plan or credits if those columns exist. The docs warn to "always validate input from client, as they can be easily modified."

A fifth one is quieter: actions that return the whole database row to the client. Next.js recommends returning only what the client needs.

A safer action

'use server'
import 'server-only'
import { z } from 'zod'
import { auth } from '@/lib/auth'
import { db } from '@/lib/db'

const Input = z.object({ projectId: z.string().uuid() })

export async function deleteProject(formData: FormData) {
  const session = await auth()
  if (!session?.user) throw new Error('Unauthorized')

  const { projectId } = Input.parse({ projectId: formData.get('projectId') })

  // ownership: the where clause includes the caller
  const result = await db.project.deleteMany({
    where: { id: projectId, ownerId: session.user.id },
  })
  if (result.count === 0) throw new Error('Not found')

  return { ok: true } // nothing else
}

Three things changed. The action checks the session itself. Input is parsed against a strict schema. And ownership is enforced in the query, so a guessed ID simply matches nothing. If you're on Supabase, an RLS policy keyed to auth.uid() does the same job at the database layer. Use both.

The 20-minute audit

1. List every action.

grep -rln "use server" app src lib 2>/dev/null

Then list the exported async functions in each file. Each one is a POST endpoint while your app uses it.

2. For each action, answer the Next.js audit questions. These come straight from the "Auditing" section of the data security guide:

  • Are the arguments validated inside the action or in the data access layer?
  • Is the user re-authorized inside the action?
  • Does it check ownership, not just sign-in?
  • Are return values filtered to only what the client needs?
  • Does database access go through a server-only data layer?

3. Check route handlers and the proxy too. The same guide singles out proxy.ts and route.ts: they "have a lot of power," so audit them with extra care. Your app/api/**/route.ts files are public URLs in the same way.

4. Replay one request against your own app. Sign in, trigger an action, and find the request in DevTools > Network. It's a POST to the page URL with a Next-Action header. Right-click it and choose Copy as cURL. Run it once with the cookie header removed, and once signed in as a second test user against the first user's record. Both should fail. Keep the Origin header, or the CSRF check will reject the request before your code runs, and you'll learn nothing. Do this only against apps you own.

5. Stay patched. Middleware bypass and later React Server Components advisories were all fixed in point releases. Check next in package.json against the Next.js security advisories on GitHub.

A rule file for your coding agent

Paste this into .cursor/rules, CLAUDE.md or your builder's knowledge file so the next generated action starts out right:

Every exported function in a 'use server' file is a public POST endpoint. In each one: get the session and throw if missing; validate all inputs with a zod schema; enforce ownership in the database query (where id AND ownerId = session user); never spread form data into an update; return only the fields the UI needs. Never rely on proxy.ts or middleware.ts as the only auth check.

What an outside scan can and can't see

An outside-in scan can't tell whether your Server Actions check ownership. That takes your code and a signed-in test user, which is what the audit above is for. It can see what your live deployment exposes to anyone: secrets compiled into the bundle, source maps, exposed files and missing headers. ProofForge's Cursor-built app checklist is here: https://proofforge.live/guides/is-my-cursor-built-app-secure. The free read-only scan is at https://proofforge.live

Sources, checked December 1, 2026:

  • Next.js docs, "How to think about data security in Next.js," nextjs.org/docs/app/guides/data-security
  • Next.js docs, "proxy.js" file convention (middleware renamed in v16), nextjs.org/docs/app/api-reference/file-conventions/proxy
  • Next.js blog, "Postmortem on Next.js Middleware bypass" (CVE-2025-29927), nextjs.org/blog/cve-2025-29927
  • Next.js security advisories, github.com/vercel/next.js/security/advisories

No comments yet. Be the first to comment.