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
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
- Point the web root at the framework's public folder only.
- Block dotfiles at the web server: deny all requests for paths beginning with
/.. - Remove backups and archives from public folders.
- Keep
.envout 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