VibeSecCheck
Home/Security Guides/exposed-env-file-fix
Critical RiskSecrets & ExposureNginxDockerVPSNode.jsNext.js

How to Fix Exposed /.env and /.git Files

Automated bots continuously scan every new domain on the internet for /.env and /.git/HEAD within minutes of SSL certificate issuance. If your server returns your .env file, assume every credential inside is already stolen.

Why AI Builders Introduce This

When indie founders self-host AI-generated apps on a VPS, Coolify, or custom Nginx/Express static server, AI-written configs frequently serve the project root directory instead of just dist/ or public/.

Real-World Exploit Impact
  • Instant compromise of DATABASE_URL, STRIPE_SECRET_KEY, AWS keys, and JWT secrets
  • Exposed /.git directory allows attackers to reconstruct your entire private source code
  • Automated bots exploit leaked AWS/OpenAI keys within under 5 minutes of discovery

Vulnerable Pattern vs. Production Fix

Vulnerable Express / Nginx Root Exposurejavascript
// server.js — DANGER: Serves project root including .env and .git!
app.use(express.static(process.cwd(), { dotfiles: "allow" }));
Locked Down Nginx + Express Configuration
# nginx.conf — Deny all hidden dotfiles (/.env, /.git, /.htaccess)
location ~ /\.(?!well-known).* {
    deny all;
    access_log off;
    log_not_found off;
    return 404;
}

# In Express, ONLY serve ./public with dotfiles ignored:
# app.use(express.static(path.join(__dirname, "public"), { dotfiles: "ignore" }));

Copy-Paste AI Fix Prompt (Cursor · Lovable · Bolt · Claude Code)

Audit our hosting, Docker, Nginx, and static file server configuration to guarantee /.env and /.git can NEVER be served publicly:
1. Ensure Express/Fastify/Nginx only serves compiled public assets (never project root) and explicitly blocks hidden dotfiles (except /.well-known).
2. Add .env, .env.*, and .git to .dockerignore and .gitignore.
3. Verify no .env file was ever copied into public/ or dist/.

How to Verify Your Fix Worked

  1. 1Run `curl -i https://yourdomain.com/.env` and `curl -i https://yourdomain.com/.git/HEAD` — both must return 404/403 (not 200 with env lines).
  2. 2If either was EVER publicly accessible, immediately rotate every single key, DB password, and signing secret inside.
  3. 3Re-scan your live URL with VibeSecCheck's passive dotfile signature check.

Frequently Asked Questions

Why did a generic scanner flag my /.env even though I'm on Vercel?

Many SPA routers return HTTP 200 with index.html for unknown paths (soft-404). VibeSecCheck validates actual KEY=VALUE and `ref: refs/heads/` signatures so SPA soft-404s never trigger false alarms.

If I block /.env now, do I still need to rotate my keys?

Yes, 100%. Internet scanners watch Certificate Transparency logs and crawl new domains automatically. Once a .env file is served publicly, treat every secret inside as compromised.

What if I accidentally committed .env to a public GitHub repo?

Deleting it in a new commit is not enough because it stays in git history. Rotate every secret immediately, add .env to .gitignore, and scrub git history.

Related Security Playbooks