Stack

How to security test a Next.js app before it ships

Updated

Test Next.js at the three places authorization is enforced separately: middleware.ts, each route.ts, and every "use server" action. A matcher that skips one path, or an action that trusts the form body's own ID, is the bug in almost every reported case.

Where this class of bug actually lives in Next.js

  • Middleware matchers. The config.matcher regex is the only thing standing between an unauthenticated request and a page, and a matcher written as /dashboard/:path* does not cover /api/* unless you list it.
  • Server actions. A "use server" function is a public HTTP endpoint. It receives whatever the client posts, including fields the UI never shows — an orgId, a userId, a role.
  • RSC and route payloads. Data fetched in a server component is serialized into the page. A component that selects * ships every column to anyone who views source, whatever the UI renders.
  • Route handlers under /app/api. Each one is its own entry point with its own auth check, and the one added last is usually the one missing it.

What to try first

  1. Request every matcher-excluded path with no cookie at all: /api/<anything>, /_next/data/, and the action endpoint IDs you can scrape out of the built JS.
  2. Take a server action invocation from the browser network tab and replay it with orgId changed to another tenant's. The action will happily process it if it reads the ID from the body.
  3. Search the RSC payload for fields the page does not render — stripeCustomerId, internalNotes, other users' emails — with view-source on a logged-in page.
  4. Sign in as the lowest role and hit every route handler the UI hides from that role. Hiding a link is not an access check.

A reproduction you can keep

The server action replay is the one that finds real bugs. Invoke the action with the victim's org id and keep the response:

Replay a server action as the wrong tenant
# Next-Action id comes from the JS bundle or the network tab
curl -sS -i 'https://staging.example.com/dashboard' \
  -X POST \
  -H 'Next-Action: 7f9c2ab1e4d3' \
  -H 'Content-Type: text/plain;charset=UTF-8' \
  -H 'Cookie: session=***' \
  --data-raw '[{"orgId":"org_other_tenant","plan":"enterprise"}]'

# vulnerable: 200 and the other org's data in the RSC payload
# fixed: 403, or a redirect to /login, or an error with no record in it

What "fixed" has to look like

The action must derive the org from the session server-side and ignore any orgId in the body, which means the fixed response is a 403 or an error that contains no record. A 200 whose payload is missing the other tenant's fields is not fixed if the row was still read — check whether the response time and the error message changed with a valid versus invalid id.

Where people get this wrong

  • middleware.ts does not run for every route in every deployment target. Do not treat it as the only gate.
  • A client-side useSession check renders the page then hides it. The data already shipped.
  • Route caching can serve one tenant's response to another if the handler does not vary on the session cookie.

What Offense does here

Offense runs an agent swarm against your app in a disposable sandbox, keeps the request, the identity and the vulnerable response as evidence, and replays the same attack after the fix so the retest is a control comparison rather than a green check. It does not read your source and it does not patch your code: the finding goes to your coding agent with the reproduction attached. The hosted workspace is invite-only — join the waitlist — and the free dependency audit in /install needs no account.

Questions this page answers

Does a Next.js middleware auth check protect API routes?

Only the routes its config.matcher actually matches. Request each /api path with no cookie and confirm you get a redirect rather than data.

Can a server action be called directly?

Yes. A server action is a POST endpoint. It has to authenticate and authorize the caller itself, exactly like a route handler.

Written by Offense Research, reviewed December 2, 2026. Correct something? Email imran@offenseresearch.com.