Next.js security checklist for AI-written apps
The Next.js security issues AI assistants introduce most: open server actions, secrets in NEXT_PUBLIC variables, middleware-only auth and cache leaks.
For an AI-written Next.js app, check four things first: no private value is exposed through a NEXT_PUBLIC variable, every server action and route handler authorises the caller and validates input, authorisation is enforced in the handlers and not only in middleware, and nothing user-specific is cached where another user can receive it. Then confirm server-only modules never reach client bundles.
- Server actions are public HTTP endpoints, even though they look like local functions.
- Middleware is a convenience layer, not the only security boundary.
- Caching mistakes in Next.js can serve one user's data to another.
Next.js is the most common framework we see in AI-written startup codebases, and assistants are good at it. They are also consistent in where they cut corners. This checklist covers the framework-specific items from the OverKnights checklist, with the shape each one takes in generated code.
NX-01: Secrets in NEXT_PUBLIC variables
Any variable with the NEXT_PUBLIC_ prefix is inlined into the JavaScript sent to every visitor. Assistants add the prefix when a value is undefined in a client component, because that makes the error go away.
Check:
- No
NEXT_PUBLIC_variable holds a secret, service role key, private API key or webhook secret. - Client components that need privileged data call a server action or route handler instead.
Details and a CI rule are in secrets in NEXT_PUBLIC variables.
NX-02: Server actions without auth and validation
A server action is an HTTP endpoint. The "use server" function looks like an ordinary import, which is exactly why assistants forget that anyone can call it with any arguments.
"use server"
import { z } from "zod"
const Input = z.object({ projectId: z.string().uuid(), name: z.string().min(1).max(120) })
export async function renameProject(raw: unknown) {
const user = await requireUser()
const { projectId, name } = Input.parse(raw)
await requireRole(user, projectId, "editor")
await db.project.update({ where: { id: projectId }, data: { name } })
}
Check every action for three lines: who is calling, is the input valid, may they touch this record.
NX-04: Middleware-only authorisation
Assistants often protect a section of the app with a middleware matcher and stop there. Route handlers and server actions under a different path, or called directly, stay open. Middleware can also be skipped by misconfigured matchers.
Check that every data access path authorises on its own. See middleware auth bypass in Next.js.
NX-03: User data in shared caches
Next.js caches aggressively and the rules have changed between versions. Generated code sometimes caches a function that reads the current user, or fetches personalised data in a context that is shared across requests.
Check:
- Any cached function that returns user-specific data takes the user ID as an argument and is keyed by it, or is not cached.
- Personalised pages are rendered per request.
- Nothing reads cookies or headers inside a shared cache scope.
SEC-05: Validation on every boundary
Types on the client are not validation. Parse inputs in route handlers, server actions and webhook handlers with a schema library. Reject on failure; do not coerce.
PERF-03: Server code in client bundles
Importing a database client or a module that reads secrets into a client component either breaks the build or, worse, ships logic and constants to the browser.
Add import "server-only" to modules that must never reach the client. It turns a silent leak into a build error.
A quick audit you can run today
- Grep for
NEXT_PUBLIC_and read every value. - List every file with
"use server"and everyroute.ts. Confirm each calls your auth helper. - Grep for your cache directives and functions and check what they return.
- Add
server-onlyto your database and secrets modules.
If you would rather have this checked every night as the codebase changes, that is the service we run.
Frequently asked questions
Are Next.js server actions secure by default?
They are protected against some cross-site attacks, but any client can call them with any arguments. Authorisation and validation are your job in every action.
Is middleware enough to protect routes?
No. Use it for redirects and coarse checks, and enforce authorisation again in each route handler, server action and data access function.
How do I keep server code out of the browser?
Import the server-only package in modules that must never be bundled for the client, and keep secrets in variables without the public prefix.
Want to know what your repo would show?
Send one public repo. We reply with three verified findings and the prompts to fix them. No call needed.