Skip to content
>_ITDITDWeb Security Platform

Glossary

What is .env — what happens when an environment file leaks

.env gathers an app's secrets (DB credentials, API keys, encryption keys) in one file. Why it's the most dangerous file, what each leaked secret unlocks, where to place it, and how to protect it in git and via rotation — with a self-check.

Published 2026-06-07 Updated 2026-10-06 8 min read

.env is extremely handy and almost every modern framework uses it. But put it in the wrong place once and every secret in it can be read by anyone. Here's what it is and how to protect it, from zero.

A bit more detail

Apps have "values that change per environment" and "values you don't want anyone to see": the DB password, external API keys, the key that encrypts sessions. Hard-code these into source and the secrets leak the moment you share the code.

So you collect them into one .env file and read them as "environment variables" from code. Next.js, Laravel, Rails, and many frameworks work this way.

The contents are plain text, one NAME=value per line (the values below are placeholders).

APP_ENV=production
DB_HOST=127.0.0.1
DB_PASSWORD=your-db-password-here
PAYMENT_API_KEY=your-payment-api-key-here

Keeping config outside the code was popularized by the web-app design guide "The Twelve-Factor App". Its test for whether config is properly separated is whether the codebase could be made open source at any moment without compromising any credentials. .env is a convenient container for that idea during development.

How it differs from similar things

Here are the things most often confused with .env. Only some of them are a safe place for secrets.

NameWhat it isOK for secrets?
Environment variablesValues the OS or container passes to a running processYes (the norm in production)
.envEnvironment variables collected in a fileYes, but never in a public directory or Git
.env.exampleA sample listing the names with empty valuesNo (it's meant to be committed)
NEXT_PUBLIC_* / VITE_*Variables embedded into browser JavaScriptNo (anyone can read them)
Secrets managerA cloud service for storing secret valuesYes; managed with permissions and audit logs

The fourth row is the one people miss. Next.js embeds the values of variables starting with NEXT_PUBLIC_, and Vite those starting with VITE_, into the JavaScript shipped to the browser at build time. Even if it lives in .env, a variable with one of these prefixes becomes public.

Why it's the "most dangerous file"

Because multiple keys sit in one file, a single exposure spreads damage fast. Listing "what each leaked value unlocks" makes the severity clear.

Leaked valueWhat happens
DB credentialsread / alter / wipe data
External API keysimpersonation use, fraudulent billing
Encryption key (e.g. Laravel APP_KEY, signing keys)session forgery, impersonated login
OAuth secretstakeover of linked apps

Indeed, exposing .env is a classic incident. → The .env exposed to the whole world

Real cases: exposed .env files being targeted

  • A large-scale extortion operation (2024): a security company's research team reported activity that targeted 110,000 domains, collected .env files exposed on web servers, used the cloud credentials inside to take data, and demanded ransom. Over 90,000 variables were collected, about 7,000 of them for cloud services and about 1,500 for social media accounts. The cause was not a cloud-provider vulnerability but misconfiguration by the organizations that exposed their .env files
  • Vercel (April 2026): unauthorized access to internal systems led to some customers' environment variables being read — those stored in the category whose values can be displayed again → full write-up
  • Public repositories (a 2026 study): more than 540,000 credentials published on GitHub were still valid at the time of the study. Deleting the file or the history doesn't help unless the credential is revoked → full write-up

The first case shows that exposed .env files aren't found by people browsing — they're collected automatically and at scale. Don't assume a small site won't be found.

The typical leak = a placement mistake

The biggest cause is "putting the whole app in the public directory (docroot)". Where only public/ should be served, exposing its parent makes .env reachable too.

✗ Dangerous (app at the public root)

public_html/
├─ .env   ← readable!
├─ app/
└─ public/

✓ Safe (app outside, only public served)

app-root/
├─ .env   ← unreachable
├─ app/
└─ public_html/ → public/
Keep the app outside the docroot and serve only public/. That alone makes .env unreachable.

Besides placement, it also leaks through these routes:

  • Committing it to Git (instantly public in a public repo; visible to everyone with access in a private one)
  • Leaving backups such as .env.bak or .env.old in the public directory
  • Shipping a container image or a distribution zip with .env still inside
  • Putting secrets in NEXT_PUBLIC_ / VITE_ variables, as described above

How to protect it

1

Don't put it in a public directory

Keep the app outside the docroot; serve only public/. Concrete steps for shared hosting: this guide.

2

Don't commit it to git

Add .env* to .gitignore and share only a .env.example with empty values.

3

Rotate everything if it leaks

Not just the one confirmed key — replace every key that was in .env. Act as if all were seen.

4

Self-check regularly

Build the habit of checking that /.env can't be opened on your own site.

How to check your site and repository

Self-check (only against domains you own)

# A 200 with a body means it's exposed. 403/404 is okay for now.
curl -sI https://yourdomain/.env | head -1
curl -sI https://yourdomain/.git/config | head -1

Checking this on every deploy catches placement mistakes early.

Check both the outside of the site and the inside of the repository.

  1. For a site whose ownership you've verified, the site security audit checks /.env, /.env.local, /.env.bak and similar paths in one go
  2. In the repository, run git log --all --oneline -- .env to see whether it was ever committed. If anything shows up, revoke the keys it contained
  3. Paste config or code into the secret leak scanner to spot hardcoded API keys or private keys (it runs in your browser)
  4. Review the list of NEXT_PUBLIC_ / VITE_ variables to make sure no secret ends up in the built JavaScript

This site's view: decide where .env lives and how to revoke its keys, together

Most .env incidents begin with one file in the wrong place. What makes the damage spread afterward is not having a set procedure for replacing the keys. On this site we recommend that if you use .env, you not only keep it outside the public directory and out of Git, but also write down in advance how to revoke and reissue each key inside it. In production, moving to a cloud secrets manager or to environment variables whose values can't be displayed again cuts the leak routes further.

Sources (primary)

  • The Twelve-Factor App, "III. Config" — 12factor.net (store config in environment variables; the open-source litmus test)
  • Next.js docs, "Environment Variables" — nextjs.org (NEXT_PUBLIC_ is inlined into JavaScript at build time; don't commit .env files)
  • Vite docs, "Env Variables and Modes" — vite.dev (VITE_ variables are exposed to client code)
  • Unit 42 (Palo Alto Networks), "Leaked Environment Variables Allow Large-Scale Extortion Operation in Cloud Environments" (August 15, 2024) — unit42.paloaltonetworks.com

Classification

CWE classifies the types of weakness behind vulnerabilities; MITRE ATT&CK classifies attacker techniques. Both are maintained by MITRE and widely used to label vulnerability reports and detection rules.

CWE™: Copyright © 2006–2026, The MITRE Corporation. CWE is a trademark of The MITRE Corporation. The MITRE Corporation hereby grants you a non-exclusive, royalty-free license to use CWE for research, development, and commercial purposes. Any copy you make for such purposes is authorized on the condition that you reproduce MITRE’s copyright designation and this license in any such copy. CWE Terms of Use

FAQ

QCan I commit .env to git?
A

No. Add .env to .gitignore. Instead commit a .env.example with empty values to share which keys are needed.

QIf even one secret leaks from .env, what do I rotate?
A

Not just the one you confirmed — rotate every key that was in .env. Priority: external API / OAuth secrets → encryption keys → mail → DB. Act as if all were seen.

QHow do I check whether .env leaked?
A

Open https://yourdomain/.env in a browser. If the contents show, it's exposed (200 with a body = bad). If it's blocked (403/404) you're okay for now.

QWhat does .env mean?
A

It's usually read 'dot env'. env is short for environment, and the leading dot marks a hidden file on Linux and macOS. The file is a list of NAME=value lines such as DB_PASSWORD=…, which the app loads as environment variables.

QCan I put secrets in variables that start with NEXT_PUBLIC_ or VITE_?
A

No. In Next.js (NEXT_PUBLIC_) and Vite (VITE_), variables with these prefixes have their values embedded at build time into the JavaScript sent to the browser, where anyone can read them. Being in .env doesn't make them secret. Use API keys only in server-side code and don't give them these prefixes.

QI accidentally pushed .env to GitHub. What now?
A

First revoke and reissue every key it contained. Deleting the file or rewriting history can't undo copies that may already exist, so revocation comes before deletion. Then add .env to .gitignore and remove it from the history.