Validation and Security
Server Actions are server entry points and should be treated as externally reachable mutation boundaries. Never assume that because a button is hidden in the UI, the action cannot be invoked.
Validate input on the server. Validate types, ranges, formats, ownership, and business rules. Libraries such as Zod can provide reusable schemas.
Authentication answers 'who are you?' Authorization answers 'are you allowed to perform this operation?' Both matter.
A disabled button, hidden field, or client-side validation is not a security boundary. Every server mutation must validate authorization and input.
'use server';
import { z } from 'zod';
const schema = z.object({
name: z.string().min(2).max(100)
});
export async function updateProfile(formData: FormData) {
const result = schema.safeParse({
name: formData.get('name')
});
if (!result.success) {
return { error: 'Invalid name' };
}
const user = await requireUser();
await updateUser(user.id, result.data);
}The action validates untrusted input before using it and also establishes the current user.
Validate all mutation input on the server.
Check authorization for the specific resource and operation.
Treat Server Actions as security-sensitive entry points.