Exposed .env Files: Why They Are Dangerous and How to Check Yours

A public .env file can hand attackers your database password, API keys and mail credentials. How it happens, how to check, and what to do if yours leaked.

Yrzo AI28 November 2026 3 min read

What a .env file is

Many web frameworks — Laravel, Node.js, Django, Next.js and others — store configuration in a file called .env. It typically holds:

  • database host, username and password
  • API keys for payment, email and AI services
  • mail server credentials
  • application secrets used to sign sessions and tokens

It is meant to sit on the server and never be served to browsers.

How it ends up public

  • the web server's document root points at the project folder instead of the public/ folder
  • a deployment copies the whole project into a public directory
  • a backup or archive of the project is left in a public folder
  • a misconfigured server serves dotfiles as plain text

Bots scan the internet constantly for /.env, /.env.backup, /.env.production and similar paths. An exposed file is often found within hours.

What an attacker can do with it

With your database credentials they may connect directly if the database is reachable. With payment or AI keys they can run up charges. With mail credentials they can send phishing from your real server. With your app secret they may forge sessions and log in as any user. See session hijacking.

How to check your site

Is your site exposed to this?

Run a free, non-intrusive check of what attackers can see from outside. No account needed.

Visit https://yourdomain/.env in a private browser window. You should see a 404 or 403, not text. Also try common variants such as .env.local, .env.production and .env.bak. A free security scan checks these and similar files automatically.

How to fix it

  1. Point the web root at the framework's public folder only.
  2. Block dotfiles at the web server: deny all requests for paths beginning with /..
  3. Remove backups and archives from public folders.
  4. Keep .env out of git — add it to .gitignore. See exposed .git directories.

If your .env was exposed

Assume every secret in it is compromised.

  • rotate the database password and every API key
  • regenerate the app secret (this will log users out)
  • check provider dashboards for unusual usage
  • review database and mail logs for access you do not recognise
  • consider whether personal data was at risk and your UK GDPR breach duties

Then follow our API key security best practices so a future leak does less damage.

Find exposures before bots do

Yrzo AI checks for exposed configuration, backups and source files on every scan. Run a free scan or get the full report.

Related reading: Exposed .git Directory: How Your Source Code Leaks and How to Stop It · API Keys Exposed in JavaScript: How to Find and Fix Them · Laravel Security Checklist for Production Apps

exposed .env fileenv file security risk.env file publicly accessibleleaked environment variables
Share

Check your own website in 60 seconds

Free check, no account needed. Want the full picture? Full reports start at £149, with up to 110 checks with a 14-day money-back guarantee.

Keep reading