WEBSITE SECURITY

Is your .env file public? How to check in two minutes

How to check whether your .env file, .git folder or backups can be downloaded from your website, how to block them and what to do if they were exposed.

Many websites keep their passwords and API keys in a file called .env. If that file sits inside the folder your web server publishes, anyone can download it by adding /.env to your address. The same goes for the .git folder, which can be used to rebuild your entire source code.

These files are exposed more often than you might think, especially after a quick deployment by copying files to a server. Bots request /.env on websites around the clock.

Only check websites you own or have permission to test.

The two-minute check

Run these in a terminal, replacing the address with your own:

curl -s -o /dev/null -w "%{http_code}\n" https://www.example.com/.env
curl -s -o /dev/null -w "%{http_code}\n" https://www.example.com/.git/HEAD

A 403 or 404 is what you want. A 200 needs a closer look, because some sites return a normal page for every address. Look at the content itself:

curl -s https://www.example.com/.git/HEAD | head -c 200

If you see something like ref: refs/heads/main, your Git folder is public. If /.env shows lines like DB_PASSWORD=, your secrets are public.

Other files worth checking

  • Backups: .env.bak, .env.old, config.php.bak, backup.zip, site.tar.gz
  • Database dumps: dump.sql, database.sql
  • Editor and system files: .DS_Store, .vscode/, .idea/
  • Logs: debug.log, error.log

Our free scanner checks the common ones automatically, and confirms the content before reporting a file, so a site that returns a normal page for every address does not produce false alarms.

How to fix it

The best fix is structural: keep secrets and source control outside the folder your web server serves. Then add a rule that refuses all hidden files as a safety net.

Nginx:

location ~ /\.(?!well-known) {
    deny all;
}

Apache, in your configuration or .htaccess:

RedirectMatch 404 /\.(?!well-known/)

Both rules keep /.well-known/ working, which is used for HTTPS certificates and security.txt. Afterwards, run the two-minute check again to confirm.

If a file was exposed

Fixing the server rule is not enough on its own. Assume anything in the file was copied.

  1. Change every password and rotate every key that was in the file.
  2. Check your server logs for requests to /.env or /.git/ that returned 200, to see when it was downloaded.
  3. If your source code was exposed through .git, check it for other secrets and for anything an attacker could use, such as admin addresses or internal services.

Two minutes of checking now is much cheaper than finding out from someone else.

Check your own website or code for these problems.

Run a free security scan Ask us to review it

More from the blog

AI-WRITTEN CODE

Five security mistakes that are common in AI-written code

AI assistants write working code fast, but working is not the same as safe. Five common mistakes, how to spot them in your own code and how to fix each one.

Read the article

EXPOSED SECRETS

You pushed an API key to GitHub. What to do in the next hour

A step-by-step plan for a leaked key: revoke it first, check whether it was used, clean the repository and stop it happening again.

Read the article

WEBSITE SECURITY

Security headers explained: what each one does and how to add it

Six response headers that protect your visitors, what each one does, and copy-ready settings for Nginx, Apache and Node.js.

Read the article
Let’s chat