Full answer
Server Functions remove the manual work of writing an API route, a fetch call and a JSON contract for each mutation. You write a function in a file with the "use server" directive, import it in a client component, and pass it to a form's action or call it from an event handler or transition. React and the framework handle the serialization and the network request. (The older name Server Actions referred to the form use case. The general feature is now Server Functions.)
What looks like a function call is a public HTTP POST endpoint. The client code is only a convenience. An attacker does not need your UI: they can send a crafted request directly with any arguments. So the security rules are the same as for any API.
Authenticate: read the session on the server inside the function. Never trust a user id, a role or a price sent from the client. Authorize: check that this user may act on this record, not only that they are logged in. Validate: check types, ranges and lengths of everything in FormData or arguments with a schema library or explicit checks, and return field errors. Do not return secrets, internal errors or full database rows. Limit rates for sensitive operations, and protect against forged cross-site requests (frameworks include checks on the Origin header, but verify the defaults for your setup).
Only async functions can be exported from a "use server" file, and arguments and return values must be serializable. Inline functions in a Server Component that are marked "use server" can capture variables, and some frameworks encrypt those captured values for the round trip, but treat them as data from the client anyway.
Keep server packages up to date. In December 2025 a critical remote code execution flaw in the Server Components protocol was fixed in React 19.0.1, 19.1.2 and 19.2.1.
The function below checks the caller, validates the input and returns a result for useActionState. It is run here as a plain async function.
'use server';
const students = new Map([['1', { id: '1', name: 'Asha' }]]);
async function getCurrentUser() {
// Stands in for reading the session cookie on the server.
return { id: 'teacher-1', role: 'teacher' };
}
export async function renameStudent(previousState, formData) {
const user = await getCurrentUser();
if (!user || user.role !== 'teacher') {
return { ok: false, error: 'Not allowed' };
}
const id = String(formData.get('id') ?? '');
const name = String(formData.get('name') ?? '').trim();
if (!students.has(id)) {
return { ok: false, error: 'Unknown student' };
}
if (name.length < 2 || name.length > 50) {
return { ok: false, error: 'Name must be 2 to 50 characters' };
}
students.set(id, { id, name });
return { ok: true, name };
}The function checks who is calling, validates both fields and returns a small result object. In a real project it would be used from a client component through useActionState and a form. This check called it three times as an ordinary async function, which does not test the HTTP layer a framework adds. Output of the run: valid rename: {"ok":true,"name":"Asha Verma"} name too short: {"ok":false,"error":"Name must be 2 to 50 characters"} unknown student: {"ok":false,"error":"Unknown student"}
Key points
- A Server Function is an async "use server" function callable from client code. It is exposed as an HTTP endpoint.
- Never trust arguments. Authenticate, authorize and validate on the server every time.
- Return minimal, serializable results. Do not leak errors or secrets.
- Only async functions can be exported from a "use server" file.
- Keep React and the framework patched, since the protocol has had security fixes.
Common mistakes
- Treating an action as private because only your form calls it.
- Checking that the user is logged in but not that they own the record.
- Trusting hidden form fields such as a role or a price.
- Returning raw database errors or whole records.
- Exporting non-async helpers from a "use server" file.
Follow-ups
- How would you protect against forged cross-site requests for actions?
- How do Server Functions differ from route handlers or API routes in design terms?
- How would you use the returned error object with useActionState in the client component?
Tips
- Open with "it is a public endpoint". Security-minded interviewers look for this sentence.
- List authenticate, authorize, validate in that order.
- Mention that you validate even when the form already has client-side checks.