The Client Component prop leak
A file marked "use client" can't magically read server-only variables — but it can still leak one, if a Server Component hands it over as a prop. The leak isn't in the client code; it's in the handoff.
Demo A — reading process.env directly (safe by design)
ClientSafeDemo is a Client Component that reads process.env.STRIPE_SECRET_KEY itself, with no help from a parent. Since that variable was never prefixed with NEXT_PUBLIC_, Next.js never inlined a value for it in this bundle.
'use client'
export default function ClientSafeDemo() {
const value = process.env.STRIPE_SECRET_KEY
// → undefined in the browser, every time, regardless of the
// real value on the server. Next.js only inlines NEXT_PUBLIC_*.
return <div>{String(value)}</div>
}Demo B — the actual leak: passing it down as a prop
This page (a Server Component) reads process.env.STRIPE_SECRET_KEY and passes it straight into ClientLeakDemo as a prop, the way you might pass a value down to render it nicely. Watch what happens.
import ClientLeakDemo from './ClientLeakDemo'
export default function Page() {
// DANGER — this value now has to cross the server/client boundary,
// which means React serializes it into the page's initial payload.
return <ClientLeakDemo secret={process.env.STRIPE_SECRET_KEY} />
}- 1View source on this page and search for
sk_test_DEMO. You'll find the full secret key sitting in the HTML, inside the RSC payload React streamed down forClientLeakDemo. - 2Open DevTools → Network, reload the page, and inspect the document response for this URL. The same string is right there in the response body — this is not something that requires reading minified JS to find.
- 3Now search the same response for
STRIPE_SECRET_KEYwithout a prefix mismatch — you'll only find it near the leaked component, not anywhere near Demo A, which never touched it.
A Client Component can read a server-only variable if it imports process.env itself.
Passing a server-only variable as a prop into a Client Component can leak it to the browser.
The safest fix is to keep passing the secret down, but rename the prop to something less obvious.