Sub topic 14
Security: what the payload exposes
This chapter assumes you know Server Components, the "use client" boundary, serializable props, and Server Functions from the earlier chapters. It covers what leaves the server, and what an attacker can send back.
The payload is data the browser can read
Server Components run only on the server, so their code and secrets never ship to the browser. The payload is different. It is sent to the client, and anyone using the browser's network tab can read it.
Anything you pass as a prop to a Client Component is serialized into the payload. So is every value a Server Component renders into an element. If a value is in the tree, it is in the payload.
// app/page.js (Server Component)
import Profile from './Profile'; // a Client Component
async function getUser(id) {
// returns { id, name, email, passwordHash, apiKey }
return db.users.find(id);
}
export default async function Page() {
const user = await getUser(1);
// Bad: the whole record, including passwordHash and apiKey,
// is serialized into the payload.
return <Profile user={user} />;
}The fix is to pick only the fields the client needs.
export default async function Page() {
const user = await getUser(1);
return <Profile name={user.name} />;
}Where leaks happen
| Source | Why it leaks | Fix |
|---|---|---|
| Whole objects as props | Every field is serialized | Pass only the needed fields |
| Secrets in rendered output | Output is part of the payload | Keep secrets out of the tree |
| Server-only code imported by a Client Component | The bundler includes it in the client bundle | Mark modules as server-only |
| Server Function arguments | The request comes from the network | Validate and authorize each call |
Keeping server code out of the client
A Client Component can import a module that reads secrets. The bundler then puts that module in the browser bundle. To catch this at build time, many setups use the server-only package.
// app/data.js
import 'server-only';
export async function getUser(id) {
// uses process.env.DB_PASSWORD
}Importing this file from a Client Component now causes a build error. Check your framework's documentation for how it handles environment variables. Many frameworks expose only variables with a specific prefix to the client.
Server Functions are public endpoints
A function in a "use server" module is reachable over the network. The client calls it by reference, but nothing stops someone from sending the request by hand with any arguments. Treat each Server Function like an HTTP endpoint.
// app/actions.js
'use server';
import { getSession } from './auth';
export async function deletePost(formData) {
const session = await getSession();
if (!session) {
throw new Error('Not signed in');
}
const id = formData.get('id');
if (typeof id !== 'string' || !/^\d+$/.test(id)) {
throw new Error('Invalid id');
}
// Check that session.userId owns post `id`, then delete it.
}Hiding a button in the UI is not authorization. The function must check who is calling and whether they may do this action.
The payload itself is untrusted input in one direction
Sending data to the client is safe only if you chose what to send. Data coming back, such as form values and Server Function arguments, is attacker-controlled. Keep both rules together: send the minimum, and trust nothing that returns.
Exercise
A Server Component fetches an order record that includes a customerEmail and an internal costPrice, then renders <OrderView order={order} />, where OrderView is a Client Component that shows only the order total. Does costPrice reach the browser, and what should you change?
Show answer
Yes. The whole order object is serialized into the RSC payload as a prop, so costPrice and customerEmail are visible in the network response even though OrderView never displays them. Pass only what is needed, for example <OrderView total={order.total} />.
Exercise
A Server Function deleteComment(formData) is only shown to admins in the UI. Why is that not enough, and what must the function do?
Show answer
The function can be called directly over the network with any arguments, so hiding the button does not restrict access. The function must verify the caller's session, check that the caller is allowed to delete that comment, and validate the id it receives.