Action and handler access checks
What you need to know
A Server Action and a Route Handler can be invoked independently of the visible page. Both must authenticate and authorize the operation at the server boundary, including when a layout or Proxy has already checked the session.
By the end, you can: reject unauthorized direct requests; test role mismatch.
Map a DAL decision to an HTTP response
type Decision = 'ALLOW'|'UNAUTHENTICATED'|'NOT_FOUND'|'FORBIDDEN';function responseFor(decision:Decision) { const status={ALLOW:200,UNAUTHENTICATED:401,NOT_FOUND:404,FORBIDDEN:403}[decision]; return Response.json({status:decision},{status,headers:{'Cache-Control':'private, no-store'}});}console.log(responseFor('FORBIDDEN').status);Expected behavior: 403; the response body contains the policy result, not a draft. This is a pure HTTP mapping example.
How it works
A Route Handler exposes an HTTP contract; a Server Function can be invoked from the UI through a framework-managed request. Neither is private just because it is only referenced from a private page. Read the session in the operation, validate external input, run the shared DAL and return a deliberately limited result. Map anonymous API requests to 401, known same-tenant permission failures to 403 and concealed missing or foreign records to 404 in this fictional service. Use 409 for a stale revision only after the caller is allowed to see the resource. Do not send internal exception messages or distinguish inaccessible from missing in a way that leaks existence. A Server Action may return a typed result for expected failures rather than expose HTTP details as its UI contract. Proxy and layouts can improve navigation and early rejection; they cannot be the only enforcement. Real cookie-authenticated mutations also need the framework's applicable origin/CSRF protections and a reviewed session library.
Recheck identity in a Server Action
// app/drafts/actions.ts — integration fragment'use server';import {readVerifiedPrincipal} from '@/lib/session';import {saveDraft} from '@/lib/dal';export async function renameDraft(_:unknown, form:FormData) { const principal = await readVerifiedPrincipal(); const id=form.get('id'); const title=form.get('title'); const rev=form.get('revision'); if (!principal) return {kind:'unauthenticated'}; if (['id','title','revision'].some(key=>form.getAll(key).length!==1) || typeof id!=='string' || !id.trim() || typeof title!=='string' || typeof rev!=='string' || !/^\d{1,8}$/.test(rev) || title.trim().length<3 || title.trim().length>80) return {kind:'invalid'}; return saveDraft(principal,id,title.trim(),Number(rev));}Expected behavior: The action rejects missing identity or malformed form data before calling a DAL that must enforce ownership, role, tenant, draft state and revision atomically. The imported session and DAL functions are integration placeholders, not a complete authentication implementation.
Common mistake
An encrypted action identifier, hidden input or disabled button is not a permission rule. Never trust a submitted role or tenant claim. A same-tenant user with reader permissions must be denied even if the request shape is valid.
Practice and verification
Design a direct request test for an anonymous caller, a reader, a foreign admin and an authorized editor. Assert both status/result and unchanged stored state for each denied case. Run only against the synthetic local fixture, not a live site.
Scope: App Router, proposed Next.js 16.x and React 19, Cache Components disabled. Framework snippets are staged, not executed; local policy, SQLite and HTTP transport evidence is separate. Exact patched dependencies and platform contracts are pending.