Skip to content

Exposed .git and .env files: how they go public and how to check

A request for /.git/HEAD that returns ref: refs/heads/main means your repository can be downloaded. Check the paths scanners request, block dotfiles in nginx or Apache and rotate what leaked first.

Offensly Team··Updated ·3 min read

$ curl -s https://example.com/.git/HEAD
ref: refs/heads/main

If your site answers that, your repository is public. That single line is the start of a well-known routine: from HEAD, a tool walks .git/refs, .git/objects and the pack files and rebuilds the whole repository, history included. Files you deleted three years ago come back, and so does every secret that was ever committed. A readable .env is faster still: it is the secrets, in plain text.

Only run checks like these against sites you own or are allowed to test.

The paths scanners request first

Automated scanners do not guess. They request a short, predictable list on every host they find:

PathWhat a 200 with real content gives away
/.git/HEAD, /.git/configThe repository, and often the internal Git remote URL
/.env, /.env.production, /.env.localDatabase passwords, API keys, mail credentials
/.DS_StoreA listing of file names in that folder, including ones not linked anywhere
/backup.zip, /site.tar.gz, /dump.sqlEverything, in one download
/.svn/entries, /.hg/The same problem with older version control

Request each of them against your own site. Look at the body, not only the status code.

A 200 is not always a leak, and a 403 is not always fine

Single-page apps and many frameworks answer every unknown path with 200 and the index.html shell. curl -sI then shows 200 OK for /.env while nothing leaks. That is why the check above uses curl -s and reads the body: a real .git/HEAD contains ref: refs/heads/, a real .env contains KEY=value lines.

A 403 Forbidden stops the download, but it confirms the file exists, and a misconfigured rule that blocks /.git/ while allowing /.git/config is common. Test a file inside the folder, not just the folder. The answer you want is 404 for all of them.

Block dotfiles where the server reads the request

The fix that holds is a server rule that refuses any path segment starting with a dot, with one exception: /.well-known/, which certificate validation and security.txt need.

nginx, inside the server block:

location ~ /\.(?!well-known/) {
    return 404;
}

Apache, in the virtual host or .htaccess:

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

Then remove the cause, because a server rule is a second line of defence. Deploy the build output folder rather than the repository working copy. For container images, add .git, .env* and backup patterns to .dockerignore, since COPY . . otherwise copies them into every image layer. Move backups out of the web root and delete the old ones; a site-old.zip from the last upgrade is usually still there.

If anything was exposed, rotate before you clean up

Assume the files were copied. Scanners request these paths around the clock, so a file that was public for a week was almost certainly fetched. Order matters:

  1. Rotate every credential that was in an exposed .env or anywhere in the repository history. git log -p across the full history shows secrets that were committed and later removed.
  2. Check the logs of the affected services (database, mail provider, cloud account) for use of the old credentials since the files became reachable.
  3. Only then block the paths and redeploy. Blocking first and rotating later leaves a window where the stolen keys still work.

Rewriting Git history to remove a secret does not help once the repository was public. The old copy is already in someone else's hands; the key has to change.

What a leak like this costs

There is no exploit and no skill involved, so there is also nothing to detect: the requests look like ordinary page views in your logs. The fix is two lines of server config and a change to the deploy step. The cleanup after a leaked database password or cloud key can take weeks.

Exposed files often sit next to other forgotten leftovers, such as DNS records pointing to services you no longer use. The Offensly penetration test checks for exposed files together with open ports, TLS, security headers and known vulnerabilities, after you verify the site is yours, and lists the exact URLs it found.

See how penetration testing works

Check your own domain in seconds

The free check reads your SPF, DKIM, DMARC and MX records and tells you exactly what to fix. No sign-up.