CriticalLong standing

Your .env file is publicly readable

A publicly reachable .env file hands every API key, database password and signing secret to anyone who asks for it. Here is how to check, and how to fix it properly.

The problem

A .env file holds the credentials your application needs to run: database passwords, API keys, mail and payment provider secrets, session and token signing keys. It belongs on the server, readable only by the application process.

When that file sits inside the directory your web server publishes, anybody can request it over HTTP and read every secret in it. There is no exploit and no skill involved, it is an ordinary file request. Automated scanners sweep the internet for exactly this, so exposure tends to be measured in hours rather than months.

The damage rarely stops at the one site. The same keys are usually reused across staging and production, and database, storage and mail credentials let an attacker read your data, change it, or send mail as you. A single exposed .env is therefore a common route to a full account takeover.

Are you affected?

Request the file from your own site, straight from the browser address bar:

https://your-domain.com/.env

If the browser shows or downloads a file of KEY=value lines, it is exposed. A 404 or 403 means that path is not being served, which is what you want.

Check the variants too, because editors, backups and deploy scripts leave them behind constantly: .env.local, .env.production, .env.development, .env.backup, .env.save and .env.bak.

Treat anything that loads as already read by somebody else. Plan on that basis.

The fix

1. Rotate every secret first

Change every credential that file contained before you touch anything else. Deleting the file achieves nothing while the keys inside it still work. Rotate database passwords, API keys, access tokens and signing secrets, and invalidate existing sessions if your framework allows it.

2. Move configuration out of the web root

The directory your web server publishes should contain only what you mean to publish. Application code and configuration belong above it, not inside it.

3. Deny dotfiles at the web server

Refuse any request for a path that begins with a dot. For nginx:

location ~ /\. {
  deny all;
  return 404;
}

For Apache, inside the virtual host or directory block:

<FilesMatch "^\.">
  Require all denied
</FilesMatch>

4. Keep it out of version control

Add .env and its variants to .gitignore. If the file was ever committed, the secrets are in your git history and must be rotated whatever the working tree looks like now.

5. Prefer injected configuration

On a managed host, set configuration as environment variables in the platform instead of shipping a file at all. Then there is nothing on disk to expose.

Let your agent fix it

Paste this into Claude Code, Cursor, or any coding agent with access to your project.

You are helping me fix a security misconfiguration on a website I own and operate.

Problem: a .env file (or one of its variants) is publicly readable over HTTP, which exposes my application secrets.

Work through the following in order. Stop and confirm with me before any destructive or irreversible step.

1. Identify the stack and the framework in this repository, then tell me which directory the web server actually publishes (the document root) and how you concluded that.
2. Find every environment file in the repository and in the deploy configuration: .env, .env.local, .env.production, .env.development, .env.backup, .env.bak, .env.save, and anything else matching .env*.
3. Report which of those sit inside the published directory.
4. List, by NAME ONLY, every secret these files contain. Never print, log, echo or copy a secret VALUE anywhere: not into a file, a commit message, a comment, or your own notes.
5. Produce a rotation checklist: for each secret name, which provider it belongs to and where it gets rotated. I will do the rotating. Do not attempt to rotate anything yourself.
6. Move configuration out of the published directory, or convert it to platform environment variables, and update whatever code reads it.
7. Add a web server rule that denies all requests for paths beginning with a dot. Detect whether this project uses nginx, Apache, Caddy, or a managed host, and write the rule for that one only.
8. Make sure .gitignore excludes every .env* file, except .env.example.
9. Check whether an environment file was ever committed, using: git log --all --full-history -- '*.env*'. If it was, say so plainly and tell me that rotation is mandatory because the secrets remain in history.
10. Create or update .env.example containing the key names with empty values, so the required configuration stays documented.

Finish with a summary of what you changed and a list of everything I still have to do by hand.