Server Actions with no auth check
A Server Action is a public POST. 'use server' does not mean only your button can call it.
By ShipSecurely Editorial · Updated · How we make these guides
Shows up in: Next.js, Supabase, Vercel
What it is
Next.js Server Actions let a component call a server function without you writing a route. The framework still exposes that function as an HTTP endpoint. Anyone who can talk to your site can invoke it. The button is one client. It is not the only client.
Do Next.js Server Actions need their own auth check?
Yes.
Next.js says to treat Server Actions like public-facing API endpoints: each one is handled as a POST request to the route where it is used, and anyone who gets an action's ID can invoke it with any arguments. Check the session and the user's right to that specific record inside the action itself, because a Proxy (formerly middleware) matcher, a layout redirect, or a hidden button is not the action's check.
"use server";
import { verifySession } from "@/app/lib/dal";
import { db } from "@/app/lib/db";
export async function renameProject(formData: FormData) {
// 1. Who is calling? The user id comes from the session, never from the form.
const session = await verifySession();
if (!session?.userId) throw new Error("Unauthorized");
// 2. Validate the input. Types are not enforced at runtime.
const projectId = formData.get("projectId");
const name = formData.get("name");
if (typeof projectId !== "string" || typeof name !== "string" || name.length > 100) {
throw new Error("Invalid input");
}
// 3. May this user change this project?
const project = await db.project.findFirst({
where: { id: projectId, ownerId: session.userId },
});
if (!project) throw new Error("Not found");
// 4. Only then mutate, and only the field you mean to change.
await db.project.update({ where: { id: project.id }, data: { name } });
}
verifySession() is the Data Access Layer helper from the Next.js authentication guide. In that guide it lives in app/lib/dal.ts and redirects to /login when there is no session. Swap in your auth library's session call, and keep the check in the action even if it redirects. db stands in for your database client. A schema library such as Zod can do step 2.
"use server" marks the function as server code. It does not check who the caller is. An action that updates a row using an id from its arguments, and never looks at the session, is an open write. Combined with the secret key, it also bypasses Supabase RLS.
Closures do not save you. The arguments the client passes are untrusted. A hidden input with userId is untrusted. Read the user from the session inside the action.
How to check in 5 minutes
Search for
"use server". Open every exported async function.For each, find the session lookup. If the first
awaitis a database call, the action is open.Check arguments. If the function trusts
userId,role,price, orisAdminfrom the caller, that is the bug even when a session exists.Sign out and trigger the action's endpoint if you know it, or temporarily call the action from a logged-out page on a staging deploy you own. A mutation that succeeds logged out is confirmed.
How to fix
Next.js
Use the action example in the full guide: session first, then input validation, then an ownership check on the id, then the write. An action that takes arguments instead of FormData needs the same four steps.
Validate name for length and type. Do not spread the form object into data. That is how role sneaks in. See the client sets the price or role.
Middleware or proxy can be an extra filter. Keep the check in the action anyway. A matcher that skips the page never runs for that call, because a Server Function is a POST to the page's route. See Auth in middleware that skips API routes.
Supabase
Inside the action, create the client from the user's cookies so RLS runs, or use the secret key only after the session check and add user_id to the query yourself. Do not create a module-level secret client and call it from every action by habit.
Vercel
Server Actions work on Vercel the same way. Deployment protection on a preview does not authorize production actions.
Firebase
If the action uses the Admin SDK, rules will not stop it. The session check in the action is the control.
Copy the fix prompt
Paste this into Cursor, Lovable, Bolt, or Claude Code. Read the diff before you merge it.
You are authorizing Server Actions. Do not add features. Find every "use server" function that reads or writes data. 1. Load the current user inside the function. If there is no user, throw or return an error before any write. 2. Ignore userId, role, price, and isAdmin arguments. Take identity from the session. 3. Scope updates and deletes to rows that user owns. 4. Do not pass the raw form object into a database update. Assign the fields you intend. 5. Leave the UI button as it is. The button is not the check. List each action and the check you added.
Questions
Do Next.js Server Actions need their own auth check?
Yes. The Next.js authentication guide says to treat Server Actions with the same security considerations as public-facing API endpoints and to verify the user is allowed to perform the mutation. The Proxy reference says to verify authentication and authorization inside each Server Function rather than relying on Proxy alone. Read the user from the session, validate the input, and check the user may act on that record before the write.
Are Server Actions CSRF-safe because Next.js checks the origin?
Next.js has protections so a random site cannot trivially submit an action with the user's cookies. That is not authorization. A request that passes the framework's origin check can still be from a logged-out caller or from the wrong user. Check the session and the owner.
Can I rely on the layout having redirected?
No. The action does not re-run your layout's redirect as its authorization. Check inside the function.
Sources
Related
Guides
Need someone to fix it for you? See /help.
The weekly fix
The weekly fix: 3 real security mistakes in vibe-coded apps and how to fix them, every Tuesday.