Home / Guides / Secret keys in the client

How secret keys leak into the client app

“I did not commit the .env file” is not the same as “users cannot download the key.” If a secret went into a public-prefixed variable, it is in the JavaScript bundle.

Agents are good at wiring environment variables and bad at noticing which ones ship to the browser. I tested this against a throwaway app that looked fine in source: no .env in git, placeholders in .env.example, keys “in env.” The production client bundle still contained sk_test_ and service_role strings, and a leftover /api/debug/env route dumped a key. A search of the built assets caught both. Checking git history alone would have missed them.

This is the usual mechanism, not a rare misconfiguration.

How the leak happens

Frameworks deliberately expose some env vars to client code, and they inline the values at build time:

If an agent (or a tutorial) names a Stripe secret, a Supabase service_role key, a webhook secret, or a database URL with one of those prefixes, the production JavaScript will contain the value. Moving the variable in the host dashboard does not fix a bundle that was already built. You have to rotate the key, then rebuild and redeploy.

The Supabase anon key is designed to be public, with Row Level Security in front of it. The service_role key is a secret and is never used in the browser. Stripe’s publishable pk_ key may go to the client. sk_test_, sk_live_ and whsec_ may not.

Prompt 1: inventory, no secret values

Start with a table, not a cleanup. Ask the agent not to print any secret it finds.

Prompt 1 · read only

Do not edit files and do not print any secret values. From the repo, list every environment variable the app expects, whether it is public or secret, and whether it is required at build time, runtime, or both. Flag any secret that is public-prefixed or imported in client code. Produce a table I can copy into the host’s dashboard.

Then widen the search to strings that already look like keys, including committed files and screenshots of logs if you have them:

Optional extra · pattern search

Search the repo for environment variables, .env* files, and strings that look like keys (sk_, sk-, AKIA, service_role, BEGIN PRIVATE KEY, ghp_, xox, postgres://, mysql://). Produce a table with: name or pattern, file path, whether it appears to be public or secret, and whether it is committed or only in an example file.

Prompt 2: fix the wiring, not the values

Prompt 2 · smallest wiring changes

Using the inventory, make the smallest changes so that: (1) .env, .env.local and similar are in .gitignore; (2) a .env.example exists with placeholder values only; (3) any secret you found in client code is moved to a server-only variable, and the client is broken on purpose until I supply the server path. Do not put real values into any file. Do not print existing values.

Breaking the client on purpose is the point. If a page still works after you remove a secret from a public variable, that secret was not actually required in the browser, or a leftover build is still serving it.

Prompt 3: search the production build

This is the check that catches what source review misses. Run the production build, then search the built client assets. For Next.js that is typically .next/static. Otherwise look in dist, build, or out.

Prompt 3 · search the bundle

Run the production build command from AGENTS.md. Then search the built client assets (for Next.js, typically .next/static; otherwise dist, build, or out) for: sk_live, sk_test, service_role, BEGIN PRIVATE KEY, whsec_, postgres://, and SECRET=. Report only file names and masked snippets if you find a hit (first 3 and last 2 characters). Do not upload this build anywhere.

If you have not checked the client bundle, do not say “no secrets are leaked.” A clean source tree can still leak if an old build is what got deployed. Search the deployed JavaScript too, not only main.

Debug routes are a second leak

Agents often leave a helper such as /api/debug/env, a dev login, or a “reset database” route behind a NODE_ENV !== 'production' gate that does not mean what you think, or behind no gate at all. Ask for a list, then remove or lock those routes so they cannot run in production.

Production hygiene

Search the repo for debug routes, NODE_ENV !== 'production' gates that expose tools, default passwords, and source maps that might be served in production if we configured that. List them. Propose the smallest changes so they cannot run in production. Wait for my yes, then apply.

If a key was already committed or shipped

Do not try to rewrite git history unless you explicitly want to. Rotate the key at the provider. Check GitHub secret scanning, the host’s env, and old preview deployments. After rotation, rebuild and redeploy: frameworks inline public variables at build time, so changing Vercel env and not rebuilding is how old keys stay in the bundle. Search the repo for a short unique snippet of the old value to confirm it is gone, and do not print the full secret.

Preview deployments may use preview env vars. Do not put live Stripe or database credentials on every pull request preview if you can avoid it. Some hosts expose build logs; confirm a failed build did not print .env. If you use Docker, do not COPY .env into the image, and never bake ENV SK_LIVE=... into a public Dockerfile.

How you know you are done

Get the playbook files

The free Debug Loop Breaker sample on GitHub has the Cursor rule, the AGENTS.md section, and the full prompt sequence you can paste into a new chat. The Vibe Launch Pack (£12) adds 15 more playbooks, including the Stripe and secrets checks described in these guides.