Learn / Secret exposure
criticalNext.js · Node · Supabase

An exposed .env file is handing out your keys

Your .env file holds the keys that run your app: the Supabase service_role key, the Stripe secret, API tokens. If that file is committed to a public repo, served by your app, or shipped to the browser, those keys are effectively public.

A leaked key cannot be un-leaked. Anyone who has it can act as you until you rotate it.

Why it matters

The Supabase service_role key bypasses Row Level Security completely, so a leak of that one key means an attacker can read and change every row in your database. A leaked Stripe secret can move money. These are not "low" findings; a single exposed key is usually game over.

How do secrets get exposed?

The common ways, in order:

The fix

Assume any exposed key is already compromised. Rotate it, move secrets to server-only environment variables, keep .env out of git, and purge it from history so it cannot be recovered from old commits.

  1. Rotate the exposed key at the provider now (issue a new one, disable the old).
  2. Store secrets in environment variables, and never prefix a secret with NEXT_PUBLIC_.
  3. Add .env and .env.* to .gitignore.
  4. Purge the secret from git history so old commits do not still contain it.
# 1. Rotate the leaked key at the provider (Supabase / Stripe / OpenAI).
# 2. Keep secrets out of git:
echo '.env*' >> .gitignore
# 3. Remove it from history (rewrites the repo - coordinate with collaborators):
npx git-filter-repo --path .env --invert-paths
# 4. Never ship a secret to the browser: NEXT_PUBLIC_* is public.

FAQ

I deleted the .env file, am I safe?

Not if it was ever committed. The key still lives in your git history and anyone who clones the repo can recover it. You must rotate the key and rewrite history.

Which env vars are safe to expose?

Only values that are genuinely public, like a Supabase anon key or a publishable Stripe key. Anything labelled secret, service_role, or private must stay server-side.