Git history secret exposure review
Brief
Deleting a file in a new commit leaves it in every earlier commit, branch and tag, so a key committed once stays readable to anyone with a clone. The review searches every ref for key-shaped strings, lists where each still lives, and masks every value it shows.
Exam prep OSCP
On this page
- Situation
- A full clone or mirror of a repository named in the scope letter, handed over by the client or public, with every branch and tag. Every command reads the local copy; no value is printed beyond its first four characters, and no credential we find is ever tried.
- Goal
- A list of every secret-shaped string in the history: the commit, the file, the branches and tags that still reach it, and its first four characters.
- Outputs
- Captured on 5 Nov 2026 in the Atarus Bench review lab, edition
review-lab-0.1: git 2.47 on Debian 13 against a lab repository built by a script with fixed names, dates and contents, so the commit ids repeat. The key id has the shape AWS uses for its documentation examples and works nowhere, the private key file is a placeholder line, not a key, and the other values are lab strings.
Check first
- That the copy holds every ref. A plain clone brings every branch as a remote-tracking ref and every tag, which
--allsearches; a mirror clone (git clone --mirror) also brings refs the host keeps for pull requests. - The shapes worth searching: cloud key ids, private key headers, connection strings with a password, and tokens with a known prefix such as
ghp_,xoxb-orsk_live_. - Whether the client already runs secret scanning or push protection, so known hits are matched to their alerts instead of reported twice.
- The client's agreement that values are masked in our notes and that nothing found is used.
Steps
Which refs does the copy hold?
git for-each-ref --format='%(refname) %(objectname:short)'
Expected output
refs/heads/feature/nightly-export b597f86 # <- a branch nobody merged
refs/heads/main e2d0539
refs/tags/v0.9-rc1 c9a993d # <- an annotated tag: this is the tag's own id, not its commit's
Three refs: the main branch, a feature branch that was never merged, and a release tag. Every one is searched, because a key removed from main stays in a tag cut before the removal, and a branch nobody merged is still pushed to every clone. In a plain clone the branches show as refs/remotes/origin/....
This step has no clean or failing result of its own: it is the inventory the next steps search. If the copy holds fewer branches and tags than the host shows for the repository, it is incomplete, so we fetch again before searching.
Which commits ever added or removed a secret?
export SECRET_RE='AKIA[0-9A-Z]{16}|BEGIN [A-Z ]*PRIVATE KEY|://[^:/]+:[^@/]+@'
git log --all --date=short --format='%h %ad %s' -G "$SECRET_RE"
Expected output
b597f86 2026-04-06 Nightly export to the partner # <- adds a private key on the branch
4443777 2026-03-16 Move settings to the environment # <- removes the settings file
b6d3bb9 2026-03-09 Add database and storage settings # <- adds a key id and a password
-G lists every commit whose diff adds or removes a line matching the pattern, across all refs. The pattern has three shapes: an AWS access key id, the header of a private key file, and a URL that carries a user and a password. Three commits match: one added the settings, one removed them, and one on the feature branch added a key file. The removal suggests someone noticed; it did not take the value out of the history.
Negative result. No commit matches. That covers these three shapes only, so a scanner with a full rule set runs next.
Where does each one still live?
git rev-list --all | xargs git grep -lE "$SECRET_RE"
git tag --contains b6d3bb9
git branch --contains b6d3bb9
Expected output
b597f860aa83cc23f117f6e774be280f29df5282:deploy/export_key # <- the tip of the unmerged branch
b6d3bb9adf3cdcb964899b9438fcae4c755600cd:config/settings.env
v0.9-rc1 # <- the release tag still holds the settings commit
feature/nightly-export
* main
git grep over every commit prints each commit and file that still holds a match. The --contains lines show which refs reach the commit with the settings: the release tag, the feature branch and main itself. In a plain clone use git branch -a --contains, since the other branches are remote-tracking refs there; the lab ran in the original repository, where every branch is local. Deleting the file on main changed nothing for anyone who holds a clone, a fork or a CI cache.
Negative result. No commit holds a match, the same answer the previous step gave: no commit in any ref ever held these shapes.
How do I show the value without copying it?
git show v0.9-rc1:config/settings.env | sed -E 's/=(.{4}).*/=\1.../'
Expected output
DATABASE_URL=post...
AWS_ACCESS_KEY_ID=AKIA... # <- enough to name the kind of key, nothing more
AWS_SECRET_ACCESS_KEY=lab-...
Four characters tell the client which kind of credential it is and show it is stored in plain text; the masking runs before anything is printed, so the values never reach our notes. This sed masks only NAME=value lines. A private key file has no =, so we never git show one; its path and commit from the git grep list are the evidence. We do not call AWS, the database or the partner's server with anything we find. The client checks whether each key is still active from its own side, for an AWS key with aws iam get-access-key-last-used from an administrator account, and rotates it.
Negative result. The file holds references to a secrets store or environment variable names, not values.
Is the tip clean, and what does the fix take?
git grep -cE "$SECRET_RE" main || echo 'nothing on the tip of main'
Expected output
nothing on the tip of main # <- what the developers see, and why the history was missed
The tip of main is clean, which is what most people looking at the repository today see, and why the history goes unchecked. The unmerged branch's tip still holds the private key. The finding is the history: a database password and an AWS key pair reachable from main, the release tag and the feature branch, and a private key on the unmerged branch.
Fix. Rotate every value first: the old one is in every clone, fork and cache, and rewriting history does not reach those. Then, if the repository is to stay, run git filter-repo --invert-paths --path config/settings.env --path deploy/export_key, force-push every branch and tag, and ask the host's support to drop cached views of the old commits, such as pull request refs. Everyone with a clone clones again. Then stop the next one: secret scanning with push protection on the host and a pre-commit scanner on developer machines.
The defender sees
Nothing on the client's side: every command reads a local copy. The host's audit log shows the clone itself on plans that record git events. The history is what the defender should have seen: had secret scanning or push protection been on, the commit that added the settings would have raised an alert or been refused at push time. If neither was on, that goes in the report beside the finding.
Done when
Every ref has been searched, each hit has its commit, file and reaching refs recorded with the value masked, and the client holds the list to rotate from.
Not covered
Searching an organization's public repositories and forks, CI logs, container images and package registries, secrets with no recognisable shape, and rewriting history, which the client does after rotating.