CCyberwiki

Exposed .git folder

Exposed .git folder
ClassSecurity misconfiguration
Also known asExposed .git directory, exposed Git repository
CWECWE-527: Exposure of Version-Control Repository to an Unauthorized Control Sphere
OWASP Top 10A02:2025 Security Misconfiguration
Typical severityHigh
AffectsSites whose files were copied or cloned onto the server together with the hidden .git folder, and served by nginx, Apache or a similar web server

Git keeps a project's entire repository in a hidden folder named .git at the top of the working copy: the current branch, the settings, the staging index and every stored version of every file.1 When the working copy itself is the web root of a server, for example after a git clone into the served folder or an upload of the whole project folder, a visitor can request /.git/HEAD or /.git/config like any other file. Those few files are enough to start downloading the rest, and the result is the site's source code together with its history.2

What the folder holds

The layout is fixed and documented, so a client knows which paths to ask for without seeing a file list.1

Table 1. Paths inside .git that matter when the folder is public
PathContents
HEADWhich branch is checked out. Every valid repository has this file.
configRepository settings, including the address of each remote (the code host the project pulls from and pushes to)
indexThe staging area, from which object hashes can be read
refs/heads/, packed-refsThe newest commit of each branch
logs/Every change made to each branch tip
objects/The stored files, folders and commits, one file each, or bundled in objects/pack/

Rebuilding a repository from its files

Git is a content-addressable store. Each object is saved under the SHA-1 hash of its content plus a short header: the first two hex characters name a subfolder of objects/, the remaining 38 name the file, and the content inside is compressed with zlib.3 A commit object names a tree and its parent commits; a tree lists the hashes of the files and subfolders it contains.3 One commit hash is therefore a key to the whole snapshot, and its parents lead back to the first commit.

A server that blocks Directory listing still gives the repository away, because every name a client needs can be read from files it already has. The open-source tool git-dumper shows the order: it fetches the fixed files such as HEAD and index, collects branch names from HEAD, config, packed-refs and the logs, takes object hashes from the index and the refs, downloads each object and follows every commit to its parents, then runs git checkout . to write out the working files.4 When listing is on, it downloads the folder recursively instead. Packed objects are found the same way, since objects/info/packs exists to tell simple HTTP clients which packs are present.1

GET /.git/HEAD                    # names the current branch
GET /.git/refs/heads/main         # newest commit hash on that branch
GET /.git/objects/3f/9a1c...      # that commit: points to a tree and a parent
GET /.git/objects/b2/07e4...      # the tree: hashes of every file in it

What leaks

The rebuilt copy holds source code that was never meant to be public, and also diffs, file names, user names, paths and IP addresses recorded in the metadata.2 Each commit also carries the author's name and email from their Git settings.3 A key that was committed and later deleted is still in an older object, which is the case Exposed credentials in source code repositories covers. Git can store authentication in config along with the settings, so a remote address may contain an access token for the code host.5 Developers may also put secrets into a private repository more readily, because it feels safe from outside eyes.5

EMERALDWHALE

The Sysdig Threat Research Team found the EMERALDWHALE campaign in 2024 through its cloud honeypot, where a compromised account made an unusual ListBuckets call naming an S3 bucket that belonged to another account and was open to the public. The bucket held attack tools and more than a terabyte of data, including stolen credentials and logs; AWS took it down after Sysdig reported it.5 The operators' target lists held more than 500 million IP addresses. Between August and September they ran httpx, an open-source scanner that checks many web servers in parallel, against them, requesting /.git/config on each host and keeping responses that contained [core]. They found more than 67,000 such URLs, a list that sold for $100 on Telegram.5

One toolkit then ran git-dumper against each hit. Tokens found in the configs were used to clone further repositories, public and private, which were searched for more keys. The haul was over 15,000 cloud credentials and credentials for more than 10,000 private repositories; of about 6,000 GitHub tokens Sysdig sampled, roughly 2,000 still worked.5 Sysdig suggested the price was high because Internet mapping services such as Shodan and Censys cannot search by URL path, so a list this large has to come from active scanning.5

Detection

Request https://example.com/.git/config from outside the server. The folder is readable when the response holds the line [core], the test the EMERALDWHALE scripts ran across whole address ranges; a vulnerability scanner can run it on your own hosts.5 The same check for .env files and backups is in Exposed sensitive files. Check every host name that serves the site, including old staging hosts.

Prevention and clean-up

The root fix is to deploy files without the repository. A build step that uploads only its output folder never ships .git. For sites deployed from source, git archive writes the tree of a commit to an archive; files marked export-ignore in .gitattributes are left out as well.6

git archive --format=tar.gz -o site.tar.gz HEAD

A server rule blocks the path as a second layer. In nginx, a location written with the ~ modifier matches a regular expression, and deny all refuses every client:78

location ~ /\.git {
    deny all;
}

In Apache, RedirectMatch can return a status outside the 300 range, in which case it takes no target URL. The line works in the server config, a virtual host or .htaccess:9

RedirectMatch 404 /\.git

Blocking the path does not undo a download that already happened. Once a folder has been public, treat every secret in the history and every token in .git/config as known to others and rotation it, as Responding to a leaked API key describes.

Prompt for a coding agent

Check how this project is deployed and make sure the .git folder never reaches the web server. If the deploy clones or copies the project folder into the served directory, change it to upload only the build output, or an archive made with git archive. Add a rule to the web server config (nginx location ~ /\.git { deny all; } or Apache RedirectMatch 404 /\.git) as a second layer. Then list every key, password and token that appears anywhere in the git history or in .git/config remote URLs, so I can rotate them. Show each file you changed.

In SecFlash

The Exposure page reports SF-EXP-104 when the repository can be downloaded from the site and SF-EXP-105 when .git/config is readable. The Security page reports SF-SEC-652 for a readable hidden folder such as .git.

See also

References

  1. a b c Git project. "gitrepository-layout: Git Repository Layout". Git documentation.
  2. a b MITRE. "CWE-527: Exposure of Version-Control Repository to an Unauthorized Control Sphere". Common Weakness Enumeration.
  3. a b c Chacon, S. and Straub, B. "10.2 Git Internals: Git Objects". Pro Git, 2nd edition.
  4. back arthaud. "git-dumper" (README, "How does it work?"). GitHub.
  5. a b c d e f g Sysdig Threat Research Team (30 October 2024). "EMERALDWHALE: 15k Cloud credentials stolen in operation targeting exposed Git config files". Sysdig.
  6. back Git project. "git-archive". Git documentation.
  7. back nginx. "Module ngx_http_core_module: location". nginx documentation.
  8. back nginx. "Module ngx_http_access_module". nginx documentation.
  9. back Apache Software Foundation. "Apache Module mod_alias: RedirectMatch". Apache HTTP Server 2.4 documentation.