Next.js Server Actions Security: CSRF, Multipart Uploads and Pessimistic UI

Server Actions make mutations feel almost too easy. You write an async function, mark it with "use server", wire it to a <form>, and the framework handles the rest. That convenience hides a fact that matters for security: every Server Action is a public HTTP endpoint. Anyone who can reach your app can try to call it, with any input, in any order, from any tool.

This guide explains what Next.js already does to protect your actions, where those protections stop, and what you need to add yourself. We cover three areas that cause the most production trouble:

  1. CSRF hardening: what the built-in Origin check covers, how to configure it behind proxies, and when to add explicit tokens.
  2. Multipart file uploads: body size limits, server-side validation, and when to bypass Server Actions for large files.
  3. Pessimistic UI state: showing the real server result before updating the interface, using useActionState, useFormStatus and idempotency keys.

The examples use the Next.js App Router with React 19 APIs and TypeScript. Where behavior depends on a version, we say so. Always confirm details against the official documentation linked throughout.

How Server Actions Work Under the Hood