On this page
- Why .env files became the default
- Five ways a .env file leaks
- 1. It gets committed
- 2. It gets baked into a Docker image
- 3. It ends up in your frontend bundle
- 4. It gets printed
- 5. Your AI coding agent reads it
- A safer setup, step by step
- Keep only development values in .env
- Ignore it everywhere
- Load production secrets at runtime
- Scope what each process can see
- Rotate on a schedule
- A quick checklist
- Where Keysquid fits
- FAQ
A .env file is fine for development keys on your own laptop, and a liability for production keys anywhere. The file itself isn't the problem: plain text on disk is. Every tool that touches your project folder (git, Docker, your bundler, your backup software, your AI coding agent) can pick it up, and most of the leaks we see start there. The safer pattern is to keep only low-risk dev values in .env and load real secrets into the process at runtime, so they never sit in the folder at all.
Why .env files became the default
The twelve-factor app methodology argued for keeping config in environment variables, separate from code, so the same build can run in any environment (12factor.net: Config). .env files grew up as the convenient local stand-in: a dotenv library reads the file at startup and copies each line into process.env. Node.js now does it natively with node --env-file=.env app.js.
That's a good idea about where config comes from. It says nothing about how to store a secret, and a text file in your repo folder is one of the weakest places to keep one.
Five ways a .env file leaks
1. It gets committed
The classic. .env was added before .gitignore existed, or someone ran git add -A in a subfolder with its own .env. Adding it to .gitignore afterwards doesn't help: git keeps the file in history, and every clone and fork has a copy. Public repos are scanned for keys within minutes of a push.
If a key has ever been committed, treat it as leaked. Follow the first 10 minutes after a leaked API key, then stop tracking the file:
git rm --cached .env
echo ".env" >> .gitignore
Our free GitHub secret scanner checks a public repo's history for exactly this.
2. It gets baked into a Docker image
COPY . . copies everything in the build context, including .env, unless a .dockerignore excludes it (Docker: build context). The file then lives in an image layer, and anyone who can pull the image can extract it, even if a later step deletes it. Add .env* to .dockerignore in every project, and pass secrets at runtime or as build secrets instead.
3. It ends up in your frontend bundle
Bundlers inline some variables into the JavaScript they ship to browsers. Next.js inlines every variable prefixed NEXT_PUBLIC_ (Next.js docs); Vite exposes every variable prefixed VITE_ (Vite docs). A secret key that gets one of those prefixes "to make the error go away" is published to every visitor. Keys that must stay secret belong behind a server route, never in client code.
4. It gets printed
Debug endpoints that dump process.env, crash reporters that attach the environment, CI steps that run env or printenv, and verbose logging of config objects all copy secrets into places with much wider access than the server itself. Once a key is in a log aggregator, it's readable by everyone who can search logs, often for months.
5. Your AI coding agent reads it
Coding agents read files in the project to understand it, and .env is a file in the project. A key the agent has read is now in its context, can be quoted back in a session, and may be sent to the model provider. We covered how to block this in Claude Code in keeping API keys away from Claude Code; the simplest fix is not having production keys in the folder at all.
A safer setup, step by step
Keep only development values in .env
Development keys should be separate keys, with the smallest permissions and spend limits the provider offers: a test-mode Stripe key, an OpenAI project key with a low budget. If one leaks, the damage is small. Commit a .env.example with names and blank values so new developers know what to set, and never put a real value in it.
Ignore it everywhere
Add .env* (with an exception for .env.example) to .gitignore, .dockerignore, and any packaging or upload config such as .npmignore or .vercelignore. Our .gitignore generator includes the right patterns for most stacks. A pre-commit hook adds a second line of defence; block API keys before they reach GitHub shows a one-line setup.
Load production secrets at runtime
The OWASP Secrets Management Cheat Sheet recommends centralising secrets in a dedicated store, with access control, auditing and rotation, rather than scattering them across config files (OWASP). In practice that means your process gets its secrets when it starts, from the platform or a secrets manager, and nothing is written to the project folder:
- Hosting platforms (Vercel, Railway, Render, Fly) store variables encrypted and inject them at runtime. Fine for small setups, but each platform holds its own copy, which makes rotation a chore.
- A secrets manager keeps one copy and syncs or injects it everywhere. With Keysquid,
keysquid run --project backend --env prod -- npm startstarts your app with secrets in its environment, read from the agent you run, so the values never touch disk.
Scope what each process can see
A process should get the secrets it uses, not the whole team's .env. Separate projects and environments mean a compromised worker exposes the worker's keys, not the payment keys.
Rotate on a schedule
Even with all of the above, keys leak through channels you didn't think of. Rotation caps how long a leaked key stays useful. If rotating is a manual, risky job, it won't happen; Keysquid rotates keys automatically for OpenAI, Stripe, AWS and many more, and pushes the new value everywhere it's used.
A quick checklist
| Check | Why |
|---|---|
.env* in .gitignore and .dockerignore | Stops commits and image leaks |
No secret has a NEXT_PUBLIC_ or VITE_ prefix | Stops browser exposure |
.env holds dev keys only, with low limits | Limits damage from any leak |
| Production secrets injected at runtime | Nothing on disk to copy |
| No code or CI step prints the environment | Keeps keys out of logs |
| Keys rotate on a schedule | A leaked key stops working |
Where Keysquid fits
Keysquid stores secrets so that only an agent you run in your own infrastructure can decrypt them, and injects them into processes at runtime, so production keys never need to live in a .env file. It can also import an existing .env file to get you started, then rotate those keys so the copies that were sitting on disk stop working. You can start a free trial and move one project in a few minutes.
Frequently asked questions
Is it safe to keep API keys in a .env file?
It's acceptable for development keys on your own machine, as long as the file is in .gitignore and .dockerignore. It's risky for production keys, because a plain-text file is copied, backed up and read by tools you don't control. Load production secrets at runtime from a secrets manager instead.
Does adding .env to .gitignore remove it from git history?
No. .gitignore only stops untracked files from being added. If .env was committed before, it stays in history and on every clone. Treat any key that was ever committed as leaked: rotate it, then remove the file from tracking with git rm --cached .env.
Are environment variables more secure than a .env file?
Slightly. Variables injected at runtime never sit on disk in your project folder, so they can't be committed or copied into an image. They are still visible to the process, its child processes and anyone who can read that process's environment, so keep them scoped and short-lived.
Why does my frontend have my API key in it?
Frontend build tools inline some environment variables into the JavaScript they ship. In Next.js that's every variable starting with NEXT_PUBLIC_, in Vite every variable starting with VITE_. Anything with those prefixes is public. Never give a secret key one of them.