Sub topic 6
Finding the secret and the commits that contain it
This chapter assumes you can run basic Git commands, know that commits are identified by hashes, and have already made a fresh clone of the affected repository. It also assumes you have revoked and rotated the leaked credential. Rotation comes first because anyone who copied the secret can still use it until it is revoked.
Before you rewrite anything, find out what you are rewriting. You need to know which files hold the secret, which commits introduced or removed it, and which refs (branches and tags) reach those commits. That decides whether you remove a whole file or replace text inside files.
Look at the whole repository first
git filter-repo --analyze reads all of history and writes a report. It does not change the repository.
git filter-repo --analyze
less .git/filter-repo/analysis/path-all-sizes.txtThe reports are in .git/filter-repo/analysis. path-all-sizes.txt lists every path that ever existed, with its size. It is useful for spotting files such as .env, key files, or database dumps that were committed and later deleted. A path that no longer exists in your current checkout can still be in history, so a listing of the working tree is not enough.
Search history for the exact string
If you know the secret, or part of it, use Git's pickaxe search. The -S option lists commits where the number of occurrences of the string changed. That means it finds the commit that added the secret and the commit that removed it.
git log --all --oneline -S'AKIAEXAMPLEKEY12345'--all searches every ref, not just the branch you have checked out. Without it you can miss a copy of the secret on another branch or tag.
The output looks like this:
9c1e4ab Remove credentials from config
3f7d2e0 Add deployment configThe older commit (3f7d2e0) added the secret. The newer one (9c1e4ab) deleted it from the current files. Both are in history, and the secret is still readable in 3f7d2e0 and in every commit between the two.
Find out which files and refs are affected
For each commit that -S returned, list the files it changed:
git show --stat 3f7d2e0To see which branches and tags contain the commit, run:
git branch --all --contains 3f7d2e0
git tag --contains 3f7d2e0Write down the answers. They tell you which refs will change after the rewrite, and later which collaborators and releases are affected.
Common errors
- Searching only the current branch. Always pass
--all, or you will miss secrets on other branches and tags. - Searching only for the full secret. A secret may be split across lines or encoded. Also search for a distinctive prefix, such as the key ID, and the variable name.
- Assuming a deleted file is gone. Check
path-all-sizes.txtand the-Sresults, not the working tree. - Forgetting that
-Smatches changes in count. A commit that moves the string between files without changing the count may not be listed. Check the neighbouring commits.
Exercise
You run git log --all --oneline -S'AKIAEXAMPLEKEY12345' and see two commits, one that added the key and one that removed it. The key is no longer in your working tree. Is the repository clean, and why?
Show answer
No. The commit that added the key still exists in history, and so do all commits between the two. Anyone who clones the repository can read the key there. The history has to be rewritten, and the key must already be rotated.