Left the .git folder on your server? Anyone can download your code

If the .git folder went out with your deploy and opens in a browser, it holds every version of your code. Check the HEAD link now, and block the folder in your server config.

Published 4 min read#App security#Web development

There is a folder whose name starts with a dot, which your file manager usually hides, that can give anyone your whole codebase along with every old version of it. All it takes is for it to go out with your site's files to the server, and for the server to serve it like any other file. By the end of this article you will be able to check your site with one request, block the folder, and know what to do if it turns out to be exposed.

What is the .git folder?

The .git folder is where Git keeps everything about the project: the content of every file in every version, the branches, and the settings. The project files you work on are just one version, and the whole history lives in this folder.

According to the official Git book, Git at its core is a store that keeps every piece of content under a key, and that store is the .git/objects directory. File content is stored as a blob, directories as a tree, and every commit points to its tree and to the commit that came right before it.

How does the folder end up on your site?

The .git folder ends up on your site when the deploy copies the whole project folder to the server, or when someone clones the repository straight into the site's public folder. The folder is copied with the rest because it is part of the project.

After that the server makes no distinction: if someone requests a file inside the folder, the server returns it just as it returns an image or a CSS file. And if you have turned off folder listings, that does not protect you here, because the attacker does not need a listing. The file names inside the folder are known and written in the documentation.

What does an attacker get from the folder?

An attacker gets your whole code and its whole history from an exposed .git folder, because the folder holds everything Git needs to rebuild the project. They break nothing: they walk the same path Git itself walks.

The repository layout documentation names these files and what is in them:

File or folder

What it holds

HEAD

points to the active branch, and every valid repository must have it

refs

the references, meaning branches and tags

objects

the repository's object store

config

the repository specific configuration file

logs

records of changes made to the references

So from HEAD they learn the branch, from refs they reach the latest commit, from the commit the tree, and from the tree the content of every file. And since every commit points to the one before it, they walk back version by version. A secret key you deleted a year ago in a new commit is still there in the old version.

How do you check your site now?

You check your site with one request for the HEAD file inside the folder, because that file must exist in any valid repository. If content comes back, the folder is exposed.

curl -i https://example.com/.git/HEAD
  1. Replace example.com with your domain, and run the command.

  2. If it returns a line that starts with ref: followed by a branch name, the folder is exposed and everything in it can be downloaded.

  3. If it returns 404 or 403, try the same request for /.git/config too.

  4. Repeat the check on every subdomain and every staging environment, because each one has its own deploy.

How do you block the folder?

You block the .git folder with a rule in your server config that refuses any request for its path, and with a deploy that never copies the folder in the first place. The rule protects you today, and a clean deploy protects you if the rule is ever removed.

In nginx, the deny all directive, according to the access module documentation, denies access to everyone, and you put it inside a location that matches the folder:

location ~ /\.git {
    deny all;
}

After you reload the config, run the HEAD check again and make sure no content comes back. Then change how you deploy: ship the build output or only the files the site needs, not the whole repository folder.

What if the folder was exposed?

If the folder was exposed, assume your code and its history are now in someone else's hands, and the first thing to do is replace every secret that was ever written in the code. Block the folder first, then work on the secrets.

  1. Search the repository history for every key, password and token ever written in the code, including the ones you deleted later.

  2. Replace each one from its service's dashboard, and make sure the old one stops working.

  3. Check the server logs for requests to the /.git/ path before you blocked it, to find out whether someone got there first.

  4. Stop secrets from entering the repository again (see GitHub push protection for secrets).

And if you once deleted a commit and want to understand why it is still there, the git reflog article shows how hard it is to make Git forget.

Where do you start today?

Run the HEAD check on your site and on every subdomain before you close this page, because one request tells you whether your code is in someone else's hands. And if you want someone to look for this and more on your platform before an attacker does, Muhassan does that check. What did the request return for you?

FAQ

If I delete a secret key from the code in a new commit, is it gone from the folder?

No. Every commit points to the one before it, and the old version with the key stays in the store. Treat the key as exposed and replace it.

My site does not show folder listings. Am I safe?

Not necessarily. The attacker does not need a listing, because the file names inside .git are known and written in its documentation. They request each file by name.

The HEAD link returned 403. Is that enough?

It means the server refuses the request, which is the result you want. Try the config file the same way, and make sure the copy of the site on the server has no such folder at all.

Share

More articles