← All posts

Field notes / Security

Your Deleted Secrets Aren't Deleted: Understanding Exposed Git Repositories

Developers use Git every day to track changes, collaborate, recover previous versions of code, and occasionally figure out who committed that questionable line at 2:13 in the morning.

That history is incredibly useful.

It can also be incredibly useful to an attacker.

When a web application is deployed with its .git directory accessible from the web, an attacker may be able to reconstruct some or all of the application's source repository. That can expose much more than the files currently running in production. Git may contain previous versions of files, configuration changes, internal paths, developer information, and secrets that someone thought they deleted months ago.

Understanding why requires looking at what Git actually stores.

What Is the .git Directory?

When you clone or initialize a Git repository, Git creates a hidden directory named .git.

For example:

my-application/
├── .git/
├── index.php
├── config.php
├── css/
└── images/

The application's working files are outside .git.

The .git directory contains the information Git needs to understand the repository itself, including:

.git/
├── HEAD
├── config
├── index
├── logs/
├── objects/
└── refs/

This includes information about commits, branches, references, and the objects representing repository content.

Normally, none of this should be accessible to someone visiting the application.

But deployment mistakes happen.

A developer might copy the entire development directory into the web root:

cp -r my-application/ /var/www/html/

Or a deployment pipeline might package files without excluding .git.

If the web server is willing to serve those files, the application's version-control metadata may become remotely accessible.

Discovering Git Exposure

One of the simplest tests is requesting:

/.git/HEAD

For example:

curl https://example.com/.git/HEAD

A vulnerable server might respond:

ref: refs/heads/main

That tiny response reveals something important.

The .git repository is accessible.

Another useful request is:

curl https://example.com/.git/config

Depending on the repository, this may reveal configuration information such as repository metadata or remote locations.

Directory listing does not need to be enabled for this vulnerability to exist.

A request to:

https://example.com/.git/

might return:

403 Forbidden

while:

https://example.com/.git/HEAD

still returns the file.

Testing specific Git resources is therefore more reliable than checking whether the directory itself can be browsed.

Why HEAD Matters

Git's HEAD file identifies the currently checked-out branch or commit.

A common value is:

ref: refs/heads/main

That tells Git that HEAD points to the main branch.

The corresponding reference may exist at:

.git/refs/heads/main

and contain a commit hash:

8f1e7b2c...

That hash points into Git's object database.

Now things become more interesting.

Git Is an Object Database

Git does not simply keep backup copies of every file inside .git.

It stores repository information as objects.

The major object types are:

Blob

A blob contains file content.

Conceptually:

blob
└── database.php contents

Tree

A tree describes a directory and references blobs or other trees.

Conceptually:

tree
├── index.php → blob
├── database.php → blob
└── includes/ → tree

Commit

A commit references a tree and contains information about that repository state.

Conceptually:

commit
├── tree
├── parent commit
├── author
├── committer
└── commit message

These objects connect together to describe the repository.

A simplified relationship looks like:

HEAD
  ↓
branch reference
  ↓
commit
  ↓
tree
  ↓
blobs
  ↓
source files

If enough of those objects can be retrieved, the repository can potentially be reconstructed.

Why Source-Code Disclosure Matters

At first glance, exposing source code might sound less serious than something like remote code execution.

But source code changes the attacker's position dramatically.

Without source access, an attacker sees the application from the outside:

HTTP request
     ↓
???
     ↓
HTTP response

With source access:

HTTP request
     ↓
routing
     ↓
authentication
     ↓
authorization
     ↓
database queries
     ↓
filesystem operations
     ↓
HTTP response

The attacker can now inspect how the application makes security decisions.

That can reveal vulnerabilities that are difficult or impossible to identify through normal browsing alone.

Examples might include:

$query = "SELECT * FROM users WHERE id = " . $_GET['id'];

or:

if request.headers.get("X-Admin") == "true":
    admin = True

or:

const jwtSecret = "super-secret-key";

The exposed repository itself may not be the final compromise.

It can provide the information necessary to find the next vulnerability.

Git History Makes This Worse

Suppose a developer accidentally commits credentials:

DB_USER = "admin"
DB_PASSWORD = "correct-horse-battery-staple"

They notice the mistake and replace it:

DB_USER = os.getenv("DB_USER")
DB_PASSWORD = os.getenv("DB_PASSWORD")

Then they commit the correction.

The current file no longer contains the password.

Problem solved?

Not necessarily.

Git's purpose is to preserve history.

The earlier commit may still contain:

DB_PASSWORD = "correct-horse-battery-staple"

An attacker who recovers the repository may therefore examine previous commits.

For example:

git log

shows repository history.

Changes between commits can be inspected with:

git diff <old-commit> <new-commit>

Individual commits can be examined with:

git show <commit>

A particularly interesting commit message might be:

Remove hardcoded database credentials

From a defender's perspective, that's responsible maintenance.

From an attacker's perspective, it's practically a treasure map.

Deleted Does Not Mean Gone

Consider this history:

Commit A
    config.py contains API_KEY

        ↓

Commit B
    API_KEY removed from config.py

        ↓

Commit C
    configuration moved to environment variables

Looking only at the current application:

config.py

contains no secret.

Looking at Git history:

Commit A

may still contain it.

This leads to an important security principle:

Removing a secret from the current version of a Git repository does not remove the secret from the repository's history.

Once a credential has been committed, it should generally be treated as compromised.

The correct response is not merely deleting the value.

The credential should be revoked or rotated.

History can be rewritten when appropriate, but rotation addresses the more important problem: copies of the original credential may already exist elsewhere.

What Else Can Leak?

Credentials are only one possibility.

An exposed repository may reveal:

Application architecture

/api/
/admin/
/internal/
/legacy/

Database structure

CREATE TABLE users (...)
CREATE TABLE password_resets (...)

Internal services

http://internal-api:8080

Cloud infrastructure

s3://company-private-backups

Development endpoints

https://dev-api.internal.example

Authentication logic

def verify_token(token):
    ...

Security controls

if user.role == "admin":
    ...

Old functionality

A route removed from the current application might still appear in previous commits.

Even comments can become valuable:

# TODO: remove temporary admin bypass before production

Hopefully the developer remembered.

Git remembers whether they did.

Automated Repository Recovery

Manually understanding the repository structure is useful because it explains why the vulnerability works.

In an authorized assessment, automation can make recovery considerably easier.

Tools such as git-dumper attempt to retrieve accessible Git resources and reconstruct the repository locally.

Conceptually:

git-dumper https://target.example/.git/ recovered-repository

If enough repository data is exposed, the recovered directory can then be examined with normal Git tooling:

cd recovered-repository

git status
git log
git branch

Automation is useful, but it should not replace understanding the underlying problem.

The vulnerability exists because sensitive repository metadata and objects are being served by the web server, not because a particular dumping tool exists.

Testing for Impact

Finding:

/.git/HEAD

is evidence of exposure.

It does not automatically establish the full impact.

A good security assessment continues by determining what information can actually be recovered.

Questions worth answering include:

This distinction matters when reporting severity.

An accessible .git/HEAD file and a fully recoverable repository containing active production credentials are technically related findings, but their real-world impact is very different.

Preventing Git Exposure

The best defense is simple:

Do not deploy .git into the web root.

Production artifacts should contain only the files required to run the application.

For example, a deployment process might explicitly exclude Git metadata:

.git/
.gitignore
.gitattributes

Container builds should follow the same principle.

A .dockerignore file can prevent repository metadata from entering the build context:

.git
.gitignore

Web servers can also explicitly deny access to hidden files and directories as an additional defensive layer.

But server configuration should be considered defense-in-depth.

The stronger architectural control is ensuring the repository metadata never reaches the production web root in the first place.

Secrets Need Their Own Controls

Preventing .git exposure does not solve secret management.

Secrets should not be committed to repositories at all.

Applications should use appropriate secret-management mechanisms such as environment-based configuration or dedicated secret-management systems.

Repositories and CI/CD pipelines should also use secret scanning where appropriate.

If a secret is accidentally committed:

1. Revoke or rotate the secret.
2. Replace the application configuration.
3. Investigate where the repository has been copied.
4. Remove the secret from repository history when appropriate.
5. Determine whether the exposed credential was used.

Simply deleting the line and committing again is not sufficient.

The Bigger AppSec Lesson

An exposed .git directory is interesting because it demonstrates how a relatively mundane deployment mistake can undermine multiple layers of application security.

The application might have strong authentication.

Its authorization checks might work correctly.

Its database might not be directly accessible from the Internet.

Its production configuration might contain no hardcoded credentials.

And yet exposing the repository may give an attacker the blueprint needed to start dismantling those protections.

That is why source-code disclosure should not be dismissed as merely informational.

The important question is not:

Can the attacker read .git/HEAD?

The important question is:

What does access to the repository allow the attacker to learn or do that they could not do before?

That is ultimately what application security testing is about.

Finding a vulnerability is only the beginning.

Understanding what it changes is the real work.

← Back to the blog