Skip to content
FunnelGuard

Fix guide by FunnelsGuard

Exposed .env file: how to find it and fix it fast

OPS-163High impactDetectedBy FunnelGuardPublished Updated
On this page

What this issue is

A .env file stores settings for an application as KEY=value lines. Frameworks such as Laravel, Symfony, Node apps and many Docker setups use it. It often contains the database login, payment and email API keys, and an app secret.

The file is only meant to be read by the application on the server. It becomes exposed when it sits inside the public web root and the server is willing to serve it. Then anyone can open https://yourdomain.com/.env in a browser and read it.

This is common after a deployment that copies the whole project folder into the public directory, after pointing the domain at the project root instead of a public subfolder, or after a server migration that drops the original block rules.

Why it costs you leads or sales

Automated scanners constantly request /.env on every site they can find. If it is readable, the secrets inside can be used within minutes, not weeks.

What a leaked .env can lead to
Secret in the fileWhat an attacker can doEffect on your business
Database passwordRead or delete customer and lead recordsLost leads, data breach duties
Email/SMTP or API keySend spam or phishing from your domainBlocklisted domain, form emails land in spam
Payment or CRM API keyRead orders, create refunds, export contactsDirect financial and data loss
App secret keyForge sessions or tokensAccount takeover
Ad or analytics tokensChange or drain campaigns and dataWasted spend, corrupted tracking
How an exposed .env turns into lost business
  1. Scanner requests /.envBots try this URL on many sites
  2. Server returns the fileNo rule blocks dotfiles
  3. Secrets copiedDB, SMTP and API keys read
  4. Abuse of your systemsData theft, spam, fraud
  5. Leads and trust lostDowntime, blocklists, clean-up

The break is the web server serving a file it should never serve.

How to check if your site has it

Test your site for a readable .env
  1. Open the URL in a private window

    Visit https://yourdomain.com/.env. A 403 or 404 page is good. If you see lines like DB_PASSWORD=..., the file is exposed.

  2. Check with curl

    Run the command below. A 200 status with content means exposed. Do this for the main domain and for every subdomain and staging site.

  3. Check common variants

    Try /.env.local, /.env.production, /.env.backup, /.env.example and /app/.env, /config/.env, /api/.env. Frameworks and old deployments often leave these.

  4. Search for a soft 200

    Some sites return a 200 with an HTML error page. Read the body: only real KEY=value lines count as a leak.

Check the status code of common env pathsbash
for p in .env .env.local .env.production .env.backup; do
  printf "%s " "$p"
  curl -s -o /dev/null -w "%{http_code}\n" "https://yourdomain.com/$p"
done

How to fix it

There are two parts: stop the file being served, then rotate the secrets. Do both. Blocking alone does not undo a leak that already happened.

Fix an exposed .env file
  1. Block the file right now

    Add a server rule (see the tabs below) or temporarily delete or rename the file if the app can run without it.

  2. Move it out of the web root

    Point the domain at the framework's public folder (for Laravel, the public directory) so the project root and .env are not reachable. Keep .env one level above.

  3. Rotate every secret

    Change the database password, SMTP credentials, API keys, app key and any tokens. Update the new values in the .env and restart or clear config caches.

  4. Review for misuse

    Check database users, API dashboards, email sending logs and ad accounts for activity you do not recognise.

  5. Keep it out of Git

    Add .env to .gitignore and commit only a .env.example with empty values.

Block access to .env by platform
  1. Apache (.htaccess or vhost)

    Add: <FilesMatch "^\.(?!well-known)"> Require all denied </FilesMatch>. On Apache 2.2 use Order allow,deny and Deny from all instead. Reload Apache.

  2. Nginx

    Add inside the server block: location ~ /\.(?!well-known) { deny all; return 404; } then run nginx -t and reload Nginx.

  3. Fix the document root

    Set the root to the public subfolder, not the project folder. This is the real fix; the rules above are a safety net.

Nginx: deny all dotfiles except .well-knownnginx
location ~ /\.(?!well-known) {
    deny all;
    return 404;
}
Apache 2.4: deny all dotfiles except .well-knownhtaccess
<FilesMatch "^\.(?!well-known)">
    Require all denied
</FilesMatch>
Before and after

Exposed

  • /.env returns 200 with passwords
  • Domain points at the project root
  • Secrets unchanged since the leak

Fixed

  • /.env returns 403 or 404
  • Domain points at the public folder
  • All secrets rotated and old ones revoked

How to confirm it's fixed

Confirmation checklist
  • https://yourdomain.com/.env returns 403 or 404 in a private window
  • The curl loop shows no 200 for .env, .env.local, .env.production, .env.backup
  • Staging and other subdomains return 403 or 404 too
  • The site and its forms still work after rotating secrets
  • Old database, API and mail credentials are revoked, not just replaced
  • Access logs show no requests to the file after the fix returned 200

How to prevent it happening again

Prevention habits
  • Always serve from a public folder, never the project root
  • Keep dotfile blocking in the server config as a default for every site
  • Add .env to .gitignore and deploy secrets through your host's environment settings
  • Do not upload backups or copies such as .env.bak to the server
  • Re-test /.env after every deploy, migration or server change
  • Give each service its own key with minimal permissions so a leak does less harm

How FunnelGuard handles this

FunnelGuard does not check for an exposed .env file today. It is a possible future check, not a current feature.

What FunnelGuard does do is run your real customer journeys on a schedule: lead forms, checkouts, booking and chat buttons. It checks each step, the tracking hits sent to ad and analytics platforms, and whether test leads reach your inbox and CRM. If a leaked or rotated key breaks a form, an email or a tag, that kind of failure can show up there, but FunnelGuard will not tell you the cause is an exposed file.

To watch for this yourself, add /.env to a post-deploy checklist and run the curl loop above. You can also schedule that command from a cron job or CI step, or use a vulnerability scanner or your host's security tools that test for sensitive files. Repeat it for every subdomain.

Questions people ask

How do I check if my .env file is publicly accessible?

Open https://yourdomain.com/.env in a private browser window, or run curl -I on the same URL. A 403 or 404 means it is blocked. If you see lines such as DB_PASSWORD=..., it is exposed and you should block it and rotate the secrets.

Why is my .env file visible in the browser?

Usually the domain points at the project root instead of the public folder, or the server has no rule denying dotfiles. A deployment or migration may also have dropped an earlier block rule. Fix the document root and add a deny rule.

How do I block access to .env in Nginx?

Add location ~ /\.(?!well-known) { deny all; return 404; } inside your server block, run nginx -t, then reload Nginx. Also set the web root to the public folder so the file is not in it.

How do I block .env in Apache or .htaccess?

On Apache 2.4 add a FilesMatch rule for names starting with a dot, with Require all denied, and keep .well-known allowed. Reload Apache if you edited the main config. Moving the file out of the web root is safer still.

What should I do if my .env file was exposed?

Block the file first, then rotate every secret in it: database password, API and mail keys, app key and tokens. Revoke the old ones, check logs and provider dashboards for misuse, and review access logs for earlier requests to /.env.

Is an exposed .env file a data breach?

It is a serious security exposure. If someone used the secrets to access personal data, it may count as a breach under laws such as GDPR. Get advice on your reporting duties if you find evidence of access.

Does FunnelGuard detect an exposed .env file?

No. FunnelGuard does not check for this yet; it is a possible future check. It monitors funnel journeys, tracking hits and lead delivery, so test /.env yourself after deploys.

Can Shopify, Wix or Squarespace sites have an exposed .env file?

Not on the hosted platform itself, because you cannot upload server files there. The risk applies to any separate server, backend or app you connect, and to private keys pasted into public page code.

Does FunnelGuard catch this?

Detected

FunnelGuard checks for this today.

How: site safety (Stage 26, daily): /.env confirmed by content (verified sites).

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.