Action authorization
What you need to know
Server Actions must establish the caller’s identity and check permission for the specific resource on every invocation. Rendering an edit button only for its owner is a useful interface choice, not a security boundary.
By the end, you can: recheck identity on each invocation; validate mutation target.
Model resource authorization without real sessions
type Principal = { id: string } | null;type Note = { id: string; ownerId: string } | null;export function decide(principal: Principal, note: Note) { if (!principal) return 'UNAUTHENTICATED'; if (!note || note.ownerId !== principal.id) return 'NOT_FOUND'; return 'ALLOW';}Expected behavior (not framework-tested): Anonymous input is unauthenticated; a foreign or missing note yields NOT_FOUND; the owner is allowed. Principal input is synthetic in this exercise only.
How it works
Use a trusted session mechanism to obtain the principal on the server. Then parse and validate the requested resource identifier and fields, verify the resource belongs to the caller or their authorized scope, and apply the write atomically where possible. Do not accept ownerId, role or tenantId from the submitted form as identity. Even an ID captured by a Server Function closure must be checked against the current principal when invoked. A database write scoped by both resource and owner helps avoid check-then-write races; the affected-row count provides evidence of success or denial. Real session and database integration is deliberately not included in this stage.
A pure policy function can teach allowed, forbidden and missing cases without impersonating a real authentication system. Choose whether to conceal the existence of unauthorized resources; this lab’s model returns a generic not-found outcome for missing and foreign records. Distinguish unauthenticated from authenticated-but-denied internally. A hidden form field, client condition, or framework action ID is not a permission grant.
Scope a database update (design only)
-- PostgreSQL-style parameters; not an executable V6 database lab.UPDATE reading_notesSET title = $1WHERE id = $2 AND owner_id = $3RETURNING id;Expected behavior (not framework-tested): The application must bind a validated title, validated note ID and verified session principal. No returned row means no authorized matching record was updated.
Common mistake
Never use formData.get("ownerId") as the caller identity or trust a previously rendered authorization decision. Permission can change between display and invocation. The fixture does not implement a real session, so it must not expose a working private write endpoint.
Practice and verification
Build a table with anonymous, owner, other authenticated user and missing note. Add a case where ownership changes after rendering the form but before writing. State which outcomes may disclose existence and which must not. Specify the actual session and database tests still needed.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.
Further reading: Data Security · Auth · Mutating