env.inspector
05 / 10Danger pattern"use server"action return valuesform submissions

Server Actions

A Server Action ("use server") is a function that runs on the server even though you call it like a normal function from client code. It's a Route Handler wearing a function signature — the same rule applies: whatever it returns is sent to the browser.

Run both actions

Both buttons call a "use server" function that reads the same STRIPE_SECRET_KEY. Compare the two results.

// action result will appear here
// action result will appear here
actions.js
'use server'

// DANGER — returns the raw secret to whoever called this action
export async function chargeCardLeaky() {
  return { ok: true, usedKey: process.env.STRIPE_SECRET_KEY }
}

// SAFE — returns only a derived, non-sensitive result
export async function chargeCardSafe() {
  const key = process.env.STRIPE_SECRET_KEY || ''
  return { ok: true, chargedWithKeyEndingIn: key.slice(-4) }
}
✕Why this one catches people off guard
Server Actions feel local — you import a function and call it, no fetch(), no URL, no visible network request in your own code. Under the hood, Next.js still sends an HTTP request and serializes the return value back to the client. If it "feels like calling a function," it's easy to forget that the return value is just as public as a Route Handler's JSON.
Verify it yourself
  1. 1
    Open DevTools → Networkbefore clicking either button. You'll see a POST request fire for each action, even though your code never wrote a fetch call.
  2. 2
    Inspect the response body of the leaky action's request. The secret is right there in the serialized action result, same shape as a Route Handler response.
  3. 3
    Compare against the safe action's request — same server-side capability, but the response never contains anything an attacker could reuse.
Self-check0/3 answered
  • Calling a Server Action from a Client Component looks like a normal function call, but Next.js still sends it over the network.

  • Because Server Actions run on the server, any value they return is safe to send to the client.

  • The fix is the same pattern as Route Handlers: return derived, non-sensitive data instead of the raw secret.