.env File Accessible Publicly or Committed to Git
What Is This Vulnerability?
The .env file, which typically stores database credentials, API keys, and other secrets, has been committed to version control or is accessible through a public web server. This exposes every secret in the file to anyone who can view the repository or request the file URL.
Why It Happens
Developers create .env files locally and forget to add them to .gitignore before making their first commit. Some web server configurations serve static files from the project root, making .env accessible via a direct HTTP request. Starter templates sometimes include example .env files that get committed accidentally.
Example Code
DATABASE_URL=postgres://admin:s3cretPassw0rd@db.example.com:5432/myapp
STRIPE_SECRET_KEY=sk_live_4eC39HqLyjWDarjtT1zdp7dc
AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
JWT_SECRET=super-secret-jwt-signing-key-2024# Environment files with secrets
.env
.env.local
.env.production
.env.*.local
# Include a template without real values
# !.env.exampleHow Hackers Exploit It
Attackers use tools like dirsearch and gobuster to probe web servers for common filenames including .env. On GitHub, simple searches for committed .env files surface thousands of results. Once found, the attacker gains access to every service credential stored in the file, often including database, payment, and cloud provider access.
How to Fix It
Add .env to .gitignore before your first commit. If the file was already committed, remove it from tracking with git rm --cached .env, then rotate every secret that was exposed. Configure your web server to deny requests for dotfiles. Use a secrets manager for production deployments instead of relying on .env files.