Skip to content

Minimize What Leaves the Server

The Course
•
18 min study

The decision

Authorization and output shaping solve different problems. A caller who may read a review still does not need internal audit fields, session tokens or other people’s identifiers.

Read, predict, run

This projection returns the fields needed by the public review card. Add an internal field to the input and observe that it stays out. Test the exact output shape, not only the existence of title, because extra fields can be the vulnerability:

JavaScript
export function run(row) {
  return { id: row.id, title: row.title, version: row.version };
}

Before pressing Run, predict what run returns for {"id": 1, "title": "Review", "version": 2, "sessionToken": "fixture"}, from the code above. Write your answers down first: after Run, the case table shows the expected values beside the actual ones.

Minimize What Leaves the Server Next.js · 5 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <strong>Review Queue · Practice lab</strong> </header> {children} </body> </html> ); } import Cases from "./Cases.jsx"; export default function Page() { return ( <main> <h1>Minimize What Leaves the Server</h1> <p> Read the input and predict the result before running. Change lib/contract.js for the exercise. </p> <Cases /> </main> ); } import { run, examples } from "@/lib/contract.js"; const text = (value) => JSON.stringify(value) ?? "undefined"; export default async function Cases() { const results = await Promise.all( examples.map(async ([input, expected]) => { // Record the input before running, to show whether run changed it. const before = text(input); const actual = await run(input); return { input: before, expected, actual, kept: text(input) === before, pass: text(actual) === text(expected), }; }), ); return ( <section> <h2>Executable cases</h2> <p> Checks: {results.filter((row) => row.pass).length}/{results.length}{" "} passed </p> <table> <thead> <tr> <th>Input</th> <th>Expected</th> <th>Actual</th> <th>Input kept</th> </tr> </thead> <tbody> {results.map((row, i) => ( <tr key={i}> <td> <code>{row.input}</code> </td> <td> <code>{text(row.expected)}</code> </td> <td className={row.pass ? "pass" : "fail"}> <code>{text(row.actual)}</code> </td> <td className={row.kept ? "pass" : "fail"}> {row.kept ? "yes" : "no: run changed it"} </td> </tr> ))} </tbody> </table> </section> ); } body { font: 16px/1.5 system-ui; margin: 0; color: #182b3c; background: #f4f8fc; } header, main { max-width: 900px; margin: auto; padding: 20px; } header { border-bottom: 1px solid #cbd5e1; } a { color: #075985; } button, input, textarea, select { font: inherit; padding: 8px; margin: 4px; } button { cursor: pointer; } table { border-collapse: collapse; width: 100%; } th, td { padding: 10px; border: 1px solid #cbd5e1; text-align: left; vertical-align: top; } pre, code { white-space: pre-wrap; overflow-wrap: anywhere; } .pass { color: #166534; } .fail, [role="alert"] { color: #a01818; } section { margin: 16px 0; padding: 12px; background: white; border-radius: 8px; } nav { display: flex; gap: 16px; } export function run(row) { return { id: row.id, title: row.title, version: row.version }; } export const examples = [ [ { id: 1, title: "Review", version: 2, sessionToken: "fixture" }, { id: 1, title: "Review", version: 2 }, ], ];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Check your prediction
  • Input {"id": 1, "title": "Review", "version": 2, "sessionToken": "fixture"} → {"id": 1, "title": "Review", "version": 2}.

The case table computes these results on the server side of the simulation. It compares values; it does not prove a production deployment or database works.

The failure to avoid

Passing an entire database row to a Client Component exposes it even when JSX does not display every field. The same applies to JSON responses and metadata.

Compare two projections of the row {"id": 1, "title": "Review", "version": 2, "ownerId": "ada", "auditNote": "flagged"}. A denylist that removes only auditNote still sends ownerId, and next month's new column too. The allowlist above sends exactly id, title and version. Write the exact JSON each puts on the wire.

Exercise

Extend the contract

Include a canEdit capability computed from a trusted actorId and the row owner. Do not include ownerId itself. Here actorId stands for the id the server took from the session and joined to the row, never a value the client sent. Deny by default: a missing or empty actorId can't edit anything, even a row with no owner.

Design the contract first: add one case of your own where actorId is an empty string and ownerId is empty too, and decide its result before running.

Edit lib/contract.js, then rerun the original cases. If the requested rule changes an earlier result, update that expected value deliberately. Keep the other cases. Add the acceptance cases to examples so the preview checks them too.

  • Input {"id": 1, "title": "Review", "version": 2, "actorId": "ada", "ownerId": "ada"} must return {"id": 1, "title": "Review", "version": 2, "canEdit": true}.
  • Input {"id": 1, "title": "Review", "version": 2} must return {"id": 1, "title": "Review", "version": 2, "canEdit": false}.
Your working space Next.js · 5 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <strong>Review Queue · Practice lab</strong> </header> {children} </body> </html> ); } import Cases from "./Cases.jsx"; export default function Page() { return ( <main> <h1>Minimize What Leaves the Server</h1> <p> Read the input and predict the result before running. Change lib/contract.js for the exercise. </p> <Cases /> </main> ); } import { run, examples } from "@/lib/contract.js"; const text = (value) => JSON.stringify(value) ?? "undefined"; export default async function Cases() { const results = await Promise.all( examples.map(async ([input, expected]) => { // Record the input before running, to show whether run changed it. const before = text(input); const actual = await run(input); return { input: before, expected, actual, kept: text(input) === before, pass: text(actual) === text(expected), }; }), ); return ( <section> <h2>Executable cases</h2> <p> Checks: {results.filter((row) => row.pass).length}/{results.length}{" "} passed </p> <table> <thead> <tr> <th>Input</th> <th>Expected</th> <th>Actual</th> <th>Input kept</th> </tr> </thead> <tbody> {results.map((row, i) => ( <tr key={i}> <td> <code>{row.input}</code> </td> <td> <code>{text(row.expected)}</code> </td> <td className={row.pass ? "pass" : "fail"}> <code>{text(row.actual)}</code> </td> <td className={row.kept ? "pass" : "fail"}> {row.kept ? "yes" : "no: run changed it"} </td> </tr> ))} </tbody> </table> </section> ); } body { font: 16px/1.5 system-ui; margin: 0; color: #182b3c; background: #f4f8fc; } header, main { max-width: 900px; margin: auto; padding: 20px; } header { border-bottom: 1px solid #cbd5e1; } a { color: #075985; } button, input, textarea, select { font: inherit; padding: 8px; margin: 4px; } button { cursor: pointer; } table { border-collapse: collapse; width: 100%; } th, td { padding: 10px; border: 1px solid #cbd5e1; text-align: left; vertical-align: top; } pre, code { white-space: pre-wrap; overflow-wrap: anywhere; } .pass { color: #166534; } .fail, [role="alert"] { color: #a01818; } section { margin: 16px 0; padding: 12px; background: white; border-radius: 8px; } nav { display: flex; gap: 16px; } export function run(row) { return { id: row.id, title: row.title, version: row.version }; } export const examples = [ [ { id: 1, title: "Review", version: 2, sessionToken: "fixture" }, { id: 1, title: "Review", version: 2 }, ], ];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Hint 1: the idea

The client needs to know what it may do, not why. A capability is a fact the server works out from private data and sends as a boolean, so the private data itself stays behind.

Hint 2: a first step

Compute the boolean server-side and keep the explicit output allowlist.

Show one solution

Replace the exported run function and any constants shown here; keep the examples export underneath it. Compare the result with your own approach.

JavaScript
export function run(row) {
  return {
    id: row.id,
    title: row.title,
    version: row.version,
    canEdit:
      typeof row.actorId === "string" &&
      row.actorId !== "" &&
      row.actorId === row.ownerId,
  };
}

row.actorId === row.ownerId alone would be true when both are missing (undefined === undefined), granting edit to an anonymous caller on an unowned row. Checking for a real id first keeps the capability deny-by-default.

canEdit is computed on the server and leaves as a plain boolean, so the browser learns the answer but not whose row it is. The presence checks come first: without them, a row with no owner and a request with no actor would compare undefined === undefined and allow editing. It's a display hint only; the action that performs the edit must still check on the server.

Explain it without the code

List what a review card's JSON would reveal if it included ownerId, and one attack that information helps.

Going further

Read the Next.js guide for the framework boundary around this rule. The preview runs code-playpad's Next.js simulation. Real caching, hydration, authentication and persistence require the real-server checks described on Day 7.

Was this article helpful?

Your response is saved on this device.