On this page
Why Do .env Files Keep Leaking Secrets?
.env files leak because they're easy to commit by mistake and AI assistants have no convention against it. Learn the failure and how to stop it.
Quick answer
.envfiles leak because they’re plain text, easy to commit accidentally, and not gitignored by default.- AI assistants routinely commit them or hardcode their values, since no convention tells them otherwise.
- The fix is layered: gitignore
.env, scan for secrets, and treat any committed secret as leaked.
Why it keeps happening
The .env pattern is deceptively fragile. A .env file holds every secret in one convenient place, as plain text. It’s exactly the file you don’t want in version control — and exactly the file that’s easy to commit: a git add . that includes it, a .gitignore rule that was never written, or a template .env.example renamed to .env and committed. One command, and the secrets are in history forever.
The AI-specific twist
AI assistants make the problem worse because they have no convention to follow unless you give them one. Asked to “wire up the API key,” an assistant will paste the key into the code or a committed config file — the shortest path to working. It will happily create and commit a .env if nothing stops it. The leak isn’t malice; it’s the absence of the rule “secrets never go in the repo.”
Why a committed secret is so bad
Once a secret is in git, it’s effectively public if the repo is public, and still exposed if the repo is private but shared. Deleting the file doesn’t remove it — it lives in history. And because secrets are often long-lived and shared, a single leak can expose many systems. This is why the response to a committed secret is rotation, not deletion.
Where this bites vibecoders
This is one of the most common, most avoidable incidents in AI-assisted development, and it’s nearly invisible until it’s not. The fix is cheap and should be day-one:
.envin.gitignore, secrets read from the environment, and a secret scanner in CI. A convention enforced by automation beats a convention you intend to remember.
Where AI coding assistants get this wrong
- Committing
.envfiles and real keys by default. - Hardcoding secrets in code or committed config when no rule says otherwise.
- Generating
.env.examplewith real-looking values that get copied into production. - “Fixing” a leak by deleting the file without rotating the secret.
Checklist
- Add
.envand key files to.gitignorebefore the first commit. - Read secrets from the environment, never from committed files.
- Scan for secrets in CI to catch leaks before merge.
- Rotate any secret that was ever committed — assume it’s leaked.
- Keep a
.env.examplewith names and dummy values only.
FAQ
Does deleting a committed secret fix the leak?
No. The secret remains in git history, accessible to anyone who can read the repository. The only real fix is rotating (revoking and replacing) the secret, then optionally cleaning history. See How to Automate API Key Rotation.
How do I stop AI assistants from committing secrets?
Give them the convention explicitly: “read secrets from environment variables, never commit .env or keys,” and enforce it with .gitignore and CI secret scanning. The automation, not the instruction, is what reliably prevents it.
Are .env files bad practice?
No — they’re a fine local-development tool. The problem is committing them. Used correctly (local only, gitignored, real secrets in a manager in production), .env is a reasonable pattern. See How to Manage Secrets and Environment Variables Properly.
Related topics
- How to Manage Secrets and Environment Variables Properly
- How to Scan Your Codebase for Hardcoded Secrets
- What Is a Non-Human Identity (NHI)?