Guide

.env file publicly accessible on Vercel, Netlify or Cloudflare Pages? How to check and fix it

Last checked 25 Nov 2026 · 5 min read · by Ship Weekend

Short answer: open https://your-site.com/.env and https://your-site.com/.git/HEAD in a private window. A 404 is what you want. If you see lines like NAME=value or ref: refs/heads/main, the file is public: rotate every key that was in it first, then deploy only your build folder. If you see your normal app, or a blank page, that's usually just your app answering every unknown address, not a leak. The content decides, not the status code.

A normal framework deploy from git doesn't publish .env. This happens in a few specific setups, and they're easy to fall into with AI-built apps.

Step 1: Test it (2 minutes)

Test your own site, and also your *.vercel.app, *.netlify.app or *.pages.dev preview addresses, which can serve older builds.

In a browser, visit these on your domain:

/.env
/.env.local
/.env.production
/.git/HEAD
/.git/config

Or from a terminal. This prints only status codes:

SITE="https://your-site.com"
for p in /.env /.env.local /.env.production /.git/HEAD /.git/config; do
  printf '%-18s %s\n' "$p" "$(curl -s -o /dev/null -w '%{http_code}' "$SITE$p")"
done

Read the results like this:

Result Meaning
404 or 403 Good. The file isn't being served.
200 and your normal app or a blank page Probably your single-page app's fallback. Check the content (below).
200 and NAME=value lines, or ref: refs/heads/..., or a [core] section Exposed. Go to the fix.

Telling a real file from a fallback

Many single-page apps answer every unknown path with index.html and a 200. A host's "rewrite everything to index.html" rule does exactly that. So a 200 alone proves nothing. This check looks at the content and prints only a yes or no, so no secrets end up in your terminal history:

SITE="https://your-site.com"
curl -s "$SITE/.env" | head -c 2000 | grep -qE '^[A-Za-z_][A-Za-z0-9_]*=' \
  && echo ".env: looks like a real env file, EXPOSED" || echo ".env: not an env file"
curl -s "$SITE/.git/HEAD" | head -c 200 | grep -qE '^(ref: |[0-9a-f]{40}$)' \
  && echo ".git/HEAD: EXPOSED" || echo ".git/HEAD: not a git file"

A real .git/HEAD starts with ref: (or, rarely, is a bare 40-character commit hash). A real .env has NAME=value lines. An HTML page has neither.

How it happens

1. The published folder is the project root. Hosts publish one folder. Netlify says only files in the publish directory are deployed,¹ and Cloudflare Pages uploads the build output directory you set.² If that folder is . (the repo root), or you have no build step and the root is served, every file in it is public, including .env.

2. A file in public/. Vite copies everything in public/ to the root of the build output as-is.³ A .env, backup or config.json dropped there, sometimes by an AI assistant "fixing" a missing variable, goes live.

3. Uploading a whole folder. Drag-and-drop deploys and CLI deploys upload what's in the folder. .gitignore doesn't help there. Vercel's CLI skips some files by default, including .git, .env.local and .env.*.local, but plain .env and .env.production aren't on that list.⁴

4. .env was committed to git. If it's in your repository, git-based deploys have it in the checkout. Whether it ends up served depends on the published folder, but it is exposed to anyone who can read the repo.

Step 2: Rotate before you clean up

If anything came back exposed, treat every value in that file as leaked: database passwords, API keys, JWT secrets, webhook secrets. Removing the file stops new downloads. It doesn't take back copies. If .git was readable, treat every secret ever committed as leaked too.

For Supabase, only the secret key (sb_secret_... or legacy service_role) needs rotating: the publishable or anon key is designed to be public. This guide has the rotation order. For other providers, create a new key at the provider, update your host's environment variables, redeploy, then revoke the old one.

Step 3: Fix what gets published

Serve only the build output. Set Netlify's publish directory to your build folder, such as dist or build, not the repo root.¹ On Vercel, set the Output Directory in Project Settings, or outputDirectory in vercel.json.⁵ On Cloudflare Pages, set the build output directory to your framework's folder, such as dist for a Vite app.²

Keep .env out of public/. Move any env or backup files out of it.

For folder or CLI deploys, exclude files. On Vercel, a .vercelignore file lists files that won't be uploaded or served.⁶ Add:

.env
.env.*
.git

Stop tracking it in git:

echo ".env*" >> .gitignore
git rm --cached .env
git commit -m "Stop tracking .env"

Old commits still contain it, which is why rotating comes first.

Put secrets in the host's settings, not in files: Project Settings, Environment Variables on Vercel; Site configuration, Environment variables on Netlify; Settings, Environment variables on Cloudflare Pages.

Step 4: Redeploy and re-test

Redeploy, then repeat Step 1 on your main domain and the preview addresses. Hosts keep earlier deployments reachable at their own addresses, so a fixed site can leave an old build online. Check your host's list of deployments and delete any that contained the file.

The leak that isn't a file

Even with no .env on the server, variables with a public prefix (VITE_, NEXT_PUBLIC_) are written into your JavaScript, so a secret placed in one is public. To check, search your live site's files for secret prefixes. Our env vars guide explains why, and the service_role key guide shows the DevTools search.

Checking this automatically

The free Quick Check requests those same five paths (/.env, /.env.local, /.env.production, /.git/HEAD, /.git/config) and looks at what comes back, so a single-page app's fallback page isn't reported as a leak. It's a GET-only scan of what your URL serves. It can't see your repository, your build settings or your database permissions. For those, work through the pre-launch checklist.

FAQ

My /.env returns 200 but it's just my app. Am I exposed? Usually not. Open it and read it. If it's your app's HTML, it's the fallback page. If it has NAME=value lines, it's exposed.

Do I need to rotate keys if the file was only exposed for a few minutes? Yes. Automated scanners request these paths constantly, so assume anything that was reachable has been read.

Does adding .env to .gitignore fix an exposed file? No. .gitignore only stops future commits. It doesn't change what your host publishes or remove the file from git history.


Want a second pair of eyes? The $49 Launch Check looks for exposed files and keys on your live URL and gives you a plain-English fix list. We never need your passwords or keys.

Sources

  1. Netlify docs, Monorepos / build settings (only files in the publish directory are deployed): https://docs.netlify.com/build/configure-builds/monorepos/
  2. Cloudflare docs, Pages: Build configuration (build output directory, framework presets, environment variables): https://developers.cloudflare.com/pages/configuration/build-configuration/
  3. Vite docs, Static Asset Handling (the public directory is copied to the root of the output as-is): https://vite.dev/guide/assets
  4. Vercel docs, Build features: Ignored files and folders (default CLI exclusions): https://vercel.com/docs/deployments/build-features#ignored-files-and-folders
  5. Vercel docs, vercel.json (outputDirectory): https://vercel.com/docs/project-configuration/vercel-json
  6. Vercel docs, Exclude files from deployments with .vercelignore: https://vercel.com/docs/deployments/vercel-ignore