SaferComputing.com

Reference · Websites

Exposed .git and .env files: check whether your website serves them, and block dotfiles

Check whether your website serves /.git/config, /.env or other dotfiles with curl and status codes, then block them with Apache DirectoryMatch and FilesMatch rules or an nginx location with deny all, and test the rules.

Published
Updated
Last checked
Applies to
Websites on Apache 2.4 or nginx, including Ubuntu servers; the check works on any site
Technical specification
Target artifactFolders and files whose names start with a dot inside a website's document root, such as /.git/, the Git repository, and /.env, environment settings that can include credentials.
Default behaviourThe web server sends any file in the document root that no rule refuses. In a test on 9 November 2026, Apache 2.4.58 and nginx 1.24 on Ubuntu 24.04 answered 200 for /.git/config and /.env with no extra rules.
Safe stateNo .git folder or .env file in the document root; /.git/config, /.git/HEAD and /.env answer 403 or 404 from outside, on every host name; /.well-known/ still reachable for certificate renewals.
Check on Linuxcurl -s -o /dev/null -w '%{response_code}\n' https://DOMAIN/.git/config
Check on macOScurl -s -o /dev/null -w '%{response_code}\n' https://DOMAIN/.env
Check on Windowscurl.exe -sI https://DOMAIN/.git/config, then read the status code on the first line
Fixdeploy only the files the site serves, and keep .git and .env outside the document root
Fix in Apache<DirectoryMatch "/\.(?!well-known)"> and <FilesMatch "^\.">, each holding Require all denied
Fix in nginxlocation ~ /\.(?!well-known) { deny all; } in the server block

Read the result

StatusMeaning
200The server sends the file. Run the same address with curl -s to read it: a Git configuration starts with [core]. Treat the code, its history and any credentials in it as exposed
403A rule or the file permissions refuse it
404Not found, or a rule answers 404
301 or 302A redirect, often to https or to www: test the address it points to

Some sites answer every address with the same page and a 200. If /.env returns your home page, the file is not exposed; read the content, not just the code.

Test every path that matters, on every host name the site answers on:

Linux or macOS terminal
for p in .git/config .git/HEAD .env .htpasswd; do printf '%s ' "$p"; curl -s -o /dev/null -w '%{response_code}\n' "https://DOMAIN/$p"; done

Why it matters

  • The .git folder is the repository itself: its object store holds the committed files and their history, and config holds the repository's settings. Anyone who can read it can rebuild the code, including any password or key that was ever committed.
  • Laravel's documentation says a committed .env file is a security risk if an intruder gains access to the repository, since any sensitive credentials in it would be exposed. An exposed .git folder is that access, and a .env file in the web folder exposes the credentials directly.

Block dotfiles in Apache

Lines in the Apache site file, inside <VirtualHost>
<DirectoryMatch "/\.(?!well-known)">
    Require all denied
</DirectoryMatch>
<FilesMatch "^\.">
    Require all denied
</FilesMatch>
  • <DirectoryMatch> is matched against folder paths on disk and covers the folders that match and the files in them, so /\. catches .git and everything inside it.
  • <FilesMatch> is matched against a file's own name, so ^\. catches .env and .htpasswd in any folder. On its own it does not protect /.git/config, whose own name is config: in our test, that address still answered 200.
  • (?!well-known) leaves /.well-known/ alone. Let's Encrypt's HTTP-01 challenge reads /.well-known/acme-challenge/ on port 80.
  • Put the lines in every site file that serves the folder, the port 80 one included. To cover every site on the server instead, save them as /etc/apache2/conf-available/block-dotfiles.conf and run sudo a2enconf block-dotfiles.
  • Reload only after configtest prints Syntax OK:
Ubuntu, as a user with sudo
sudo apache2ctl configtest
sudo systemctl reload apache2

Block dotfiles in nginx

Lines in the nginx server block
location ~ /\.(?!well-known) {
    deny all;
}

The ~ makes this a case-sensitive regular expression, matched against the requested address. deny all refuses every client; in our test, nginx answered 403. nginx -t checks the syntax and tries to open the files the configuration refers to; reload only when it reports success:

Ubuntu, as a user with sudo
sudo nginx -t
sudo systemctl reload nginx

Test the rules

  1. Run the loop from Read the result again, from outside: every dot path should answer 403 or 404.
  2. Check that the home page still answers 200: curl -s -o /dev/null -w '%{response_code}\n' https://DOMAIN/
  3. If certificates renew through /.well-known/, put a test file in the document root, shown here as /var/www/SITE, and fetch it over port 80. -L follows redirects, as Let's Encrypt does. A 200 means the exception works; a 403 means the rule also blocks the challenge folder:
Ubuntu, as a user with sudo
sudo mkdir -p /var/www/SITE/.well-known/acme-challenge
echo ok | sudo tee /var/www/SITE/.well-known/acme-challenge/test.txt
curl -sL -o /dev/null -w '%{response_code}\n' http://DOMAIN/.well-known/acme-challenge/test.txt
sudo rm /var/www/SITE/.well-known/acme-challenge/test.txt

Troubleshooting

Apache: the whole site answers 403 after adding the rule

The document root itself sits under a folder whose name starts with a dot, such as /var/www/.sites/example, so /\. matches every path. Anchor the pattern at the document root:

Lines in the Apache site file
<DirectoryMatch "^/var/www/\.sites/example/(.+/)?\.(?!well-known)">
    Require all denied
</DirectoryMatch>
Apache: /.git/config still answers 200
  • A <Location> block with Require all granted overrides the rule: Apache merges <Location> sections after <DirectoryMatch> and <FilesMatch>. In our test, adding one reopened /.git/config and /.env.
  • The rule is in the port 80 site file, but the site answers on 443, or the other way round.
  • The configuration was not reloaded.
nginx: a dotfile under one folder still answers 200

If the longest matching prefix location has the ^~ modifier, nginx does not check regular expressions, so the deny rule never runs for that folder. Repeat it inside that block:

Lines in the nginx server block
location ^~ /static/ {
    location ~ /\.(?!well-known) {
        deny all;
    }
}

sudo nginx -T tests the configuration and prints every configuration file, which makes such blocks easy to find.

Sources