# Server Actions Security

> Why you can never trust the Client payload.

Subject: Next.js | Lesson 37 of 65 | 4 min read
Interactive lesson: https://codewithanimation.in/nextjs/actions-security
Previous: [Error Handling (Actions)](https://codewithanimation.in/nextjs/actions-error-handling.md) | Next: [Client Routing (useRouter)](https://codewithanimation.in/nextjs/use-router.md)

## Public Endpoints

It is a common misconception that Server Actions are 'internal' functions safely hidden away on the server.

In reality, Next.js compiles every Server Action into a public HTTP POST endpoint. Anyone with the URL can trigger it from outside your application.

## Zero Trust Architecture

Because they are public endpoints, you must treat them with the exact same paranoia as a traditional REST API route.

You cannot trust the data being sent from the client, and you must verify that the user making the request is authorized to perform the action.

## Internal Validation

Always retrieve the user's session securely *inside* the body of the Server Action.

Never accept sensitive identifiers (like `userId` or `role`) as arguments passed from the client, because a malicious user can simply change the arguments in their network request.

## Common mistake: The Prop Trap

A common mistake is fetching the user session in a Server Component, and then passing the `userId` down to a Client Component's action handler.

If the action relies on that passed `userId` to delete a post, a hacker can easily intercept the request, change the `userId` in the JSON payload, and delete posts belonging to other people.

## Tip: Zod Validation

Just like API routes, you should validate the shape and type of the incoming `FormData` using a schema validation library like Zod.

Never blindly trust that the client sent the correct fields.

## Try it: The Payload Forgery

Hover over the 'Bad Practice' and 'Good Practice' tabs to see how an attacker can manipulate arguments passed from the client, and how to prevent it by reading the session securely on the server.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/actions-security

## Check yourself

### Question 1

What are Server Actions compiled into under the hood?

- A. Secure WebSocket connections.
- B. Encrypted RPC binaries.
- C. Public HTTP POST endpoints.
- D. Private Node.js internal methods.

Correct answer: C

Next.js exposes Server Actions as standard POST endpoints. Anyone can hit them if they know the URL and the Action ID.

- Why not A: They use standard HTTP.
- Why not B: They are standard web endpoints.
- Why not D: They are exposed to the internet.

### Question 2

Why is it dangerous to pass `userId` as an argument from the Client to a Server Action?

- A. Because the payload can be manipulated by malicious users.
- B. Because `userId` is usually not serializable.
- C. Because it breaks React Server Components.
- D. Because it prevents Next.js from caching the route.

Correct answer: A

If you trust arguments passed from the Client, a user can modify the HTTP request payload and forge actions on behalf of other users. Always read the secure session cookie inside the action itself.

- Why not B: Strings are perfectly serializable.
- Why not C: It works, it's just wildly insecure.
- Why not D: Mutations aren't cached anyway.

## Remember this

- Server Actions are exposed as public HTTP POST endpoints.
- Treat them with the same security paranoia as traditional API routes.
- Never trust sensitive identifiers (`userId`, `role`) passed in as arguments.
- Always validate the user's session and authorization INSIDE the action body.
- Use libraries like Zod to validate the incoming data payload.
