Exposed .env File Next.js Fix: A Developer's Complete Guide
OverMCP Team
Quick answer
If you’ve accidentally exposed your .env file in a Next.js app, immediately rotate all secrets (API keys, database passwords, tokens) and remove the .env file from version control history. For ongoing protection, add .env to .gitignore, use environment variables in Vercel/Netlify, and run a secret leak scanner to catch any lingering exposures.
What to check first
Before you panic, run through this checklist to assess the damage:
https://yourdomain.com/.env. If you see your secrets, they are exposed.git log --diff-filter=A -- .env to see if the file was ever committed.sk-, AKIA, or SG..NEXT_PUBLIC_ are exposed to the browser. Non-public keys must only be used in server-side code (API routes, getServerSideProps, middleware)..env is compromised.Step-by-step fix
1. Remove `.env` from version control
If you accidentally committed .env, remove it from Git history using git filter-branch or the simpler git rm:
git rm --cached .env
echo ".env" >> .gitignore
git add .gitignore
git commit -m "Remove .env from repo and add to .gitignore"To completely purge it from history (use with caution if you have collaborators):
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch .env" \
--prune-empty --tag-name-filter cat -- --allThen force push to rewrite history:
git push origin --force --all2. Rotate all secrets
Assume every secret in the exposed file is compromised. Generate new keys:
service_role key.Update your deployment environment variables with the new values.
3. Use Next.js environment variables correctly
Create two files – one for local development and one for production secrets:
# .env.local (local only, never committed)
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
OPENAI_API_KEY=sk-...
STRIPE_SECRET_KEY=sk_live_...
# .env.example (committed, shows required vars without values)
DATABASE_URL=
OPENAI_API_KEY=
STRIPE_SECRET_KEY=In next.config.js, ensure you’re not accidentally exposing server-only variables:
// next.config.js
module.exports = {
env: {
// Only put values that are safe for the client here
NEXT_PUBLIC_ANALYTICS_ID: process.env.NEXT_PUBLIC_ANALYTICS_ID,
},
};Best practice: Use NEXT_PUBLIC_ prefix only for values that must be in the browser (e.g., public API keys for Firebase). All other secrets should be accessed only in server-side code via process.env.SECRET.
4. Add a `.gitignore` entry
Ensure .env is in your .gitignore from the start:
# .gitignore
.env
.env.local
.env.production
.env.*.local5. Scan for remaining leaks
Use a free AI app security scanner to check your entire Git history and current deployment for exposed secrets. This will catch:
6. Set up continuous monitoring
Enable continuous security monitoring on your repository so you’re alerted immediately if any new secrets are accidentally pushed.
Common mistakes
.env, .env.local, .env.development, etc. but only .env.local is ignored by default in some setups. Always use .env.local for local development keys..env to .env.example and forget to blank out the secrets. Use placeholder values like your-api-key-here.NEXT_PUBLIC_ is inlined into the client bundle. Never store API keys, database passwords, or other secrets there..env.production locally but deploy via Git, that file could be committed. Use .env.production.local for local overrides.How to prevent future exposures
husky with lint-staged can run a secret scanner before each commit.FAQ
How do I check if my `.env` file is publicly accessible?
Try visiting https://yourdomain.com/.env in a browser. If you see the file contents, it’s exposed. Also check /.env, /.env.local, and /.env.production. Use a secret leak scanner to detect leaks automatically.
Can I recover a committed `.env` file without rewriting Git history?
Yes, you can remove the file from the current commit and add it to .gitignore, but the file will still exist in previous commits. For a full removal, you must rewrite history using git filter-branch or BFG Repo-Cleaner, then force push.
What should I do if my `.env` file was exposed but only for a short time?
Immediately rotate all secrets in the file. Even a brief exposure can be scraped by automated bots. Change API keys, database passwords, and any tokens. Then scan your repo and deployment for any copies of the old keys.
---
Protect your Next.js app from secret leaks. Scan your repo for free with [OverMCP](https://www.overmcp.com/).