Define a Server Function authorization boundary
Treat a Server Function as a server-side operation
A Server Function is an asynchronous function that a compatible RSC integration can invoke from client code using a transported reference. Mark it with a use server directive at the function or module level as supported; for a direct client import use a module-level directive. The integration handles the request and response. A Server Action is a Server Function used as an Action, for example by a form. A form is not an access-control boundary. Any client-supplied identifier, hidden value or bound argument must be treated as untrusted. Every mutation must authenticate the caller using trusted server context, authorize the requested resource and validate its input. The following three source files are a partial integration fixture: the request/session and persistence adapter is intentionally not supplied. Do not expose this fixture as a real endpoint.
export function planRename(actor,record,input){ if(!actor || typeof actor.id!=='string' || !actor.id) return {ok:false,code:'AUTH_REQUIRED'}; if(!record || record.ownerId!==actor.id) return {ok:false,code:'NOT_FOUND'}; if(typeof input!=='string') return {ok:false,code:'INVALID_TITLE'}; const title=input.trim(); if(title.length<1 || title.length>60) return {ok:false,code:'INVALID_TITLE'}; return {ok:true,change:{id:record.id,title}};}The pure function makes a decision, not a write. A missing actor receives AUTH_REQUIRED. A missing or unauthorized record returns the same NOT_FOUND result to avoid revealing whether another user owns it. A valid owner with a trimmed title between 1 and 60 JavaScript UTF-16 code units receives a minimal change. This toy policy has no administrator role. The caller must obtain actor and record from trusted storage, not from the incoming form. Actual persistence must enforce the same ownership and consistency conditions atomically; a check followed by an unguarded write is insufficient.
Call the policy inside the trusted request boundary
'use server';import {planRename} from './c40-l02-s03.mjs';import {currentActor,renameOwnedRecord} from './approved-server-adapter.js';export async function renameBook(previous,formData){ const actor=await currentActor(); if(!actor)return {message:'Sign in first.'}; const id=formData.get('id'); if(typeof id!=='string'||!/^b[0-9]{1,8}$/.test(id))return {message:'Invalid book.'}; // Adapter loads the record and applies the callback in one guarded transaction. const result=await renameOwnedRecord(actor,id,record=>planRename(actor,record,formData.get('title'))); return {message:result.ok?'Title saved.':'Unable to save title.'};}The file exports an async Server Function. The missing adapter is an explicit prerequisite: currentActor must verify a session on every call; renameOwnedRecord must enforce ownership at read/write time, validate the change and commit atomically or fail. The model callback is server-internal, not passed by a browser. The adapter must fail closed, never return raw private records, and handle conflicts and storage failures. The integration must also enforce applicable origin/CSRF protections, request size and rate limits. Previous action state and FormData are untrusted. No real identity, credential, storage or endpoint is used. The function returns a small status object; logging and revalidation depend on the integration.
Use a form to submit the operation
'use client';import React,{useActionState} from 'react';import {renameBook} from './c40-l02-s06.mjs';export default function RenameForm({bookId}){ const [state,action,pending]=useActionState(renameBook,{message:''}); return <form action={action}><input type="hidden" name="id" value={bookId}/><label>New title <input name="title" required maxLength={60}/></label><button type="submit" disabled={pending}>{pending?'Saving...':'Save title'}</button><p role="status">{state.message}</p></form>;}The form starts with an empty status. A valid authenticated owner should see Title saved after a successful request; other callers should not change the record. The disabled button and input length limit improve usability but do not enforce security. The hidden book ID is not secret and may be changed by a caller. useActionState integrates the result and pending state; the form does not display the new stored record unless the surrounding application updates it. A successful action can reset uncontrolled fields; define preservation behavior separately if needed. Errors thrown by the action require an appropriate error boundary or framework handling. Do not assume server cache refresh occurs universally. Practice: test the pure policy with allowed and denied cases, then write the adapter contract and acceptance tests before selecting an actual integration.