
Deleting a Leaked Secret Isn't Enough: Git History, Env Files and Rotation
- كاتب موثّق
محتويات المقال
appsettings.json. The developer empties the value, moves the key to a .env file listed in .gitignore, and pushes a commit called "Move API key to .env". The review passes, the branch is merged, and nobody touches the key itself. A month later the provider's dashboard shows refunds that nobody on the team issued. The key was still in the first commit, and it still worked.
The common misconception
git grep finds nothing. Code search finds nothing. Moving secrets out of source files is also genuinely good advice, so the commit looks like the responsible fix. But all of those tools show the latest version of the code, and a leaked secret doesn't care about the latest version. It cares about every copy that already exists, and about whether the key is still valid.
What actually happens
A commit keeps the old version forever
git grep FAKE-KEY # the current files: no outputgit log --oneline -S FAKE-KEY # commits that added or removed it# a446c90 Move API key to .env# 97d35a1 Add payments configgit show 97d35a1:appsettings.json# {# "Payments": { "ApiKey": "FAKE-KEY-0000-DO-NOT-USE" }# }git show a446c90 -- appsettings.json | grep FAKE# - "Payments": { "ApiKey": "FAKE-KEY-0000-DO-NOT-USE" }Copies don't receive your cleanup
An env file moves the key, it doesn't change it
.env is the same string that sits in history, so the move changes where your next deploy reads it, not whether the leaked copy works. Env files and variables have their own exposure, too: OWASP's Secrets Management Cheat Sheet notes that environment variables "are generally accessible to all processes and may be included in logs or system dumps." And if the key ships inside a mobile app, it's public whatever you do in Git, as the article "Your Mobile App Can't Keep a Secret" explains.
Only revocation makes the leaked copy useless
Closing the leak in the right order
# Empty the value, move the key to .env, push. Incident closed?git commit -am "Move API key to .env"git push# The key is still in the previous commit, in this commit's diff,# in every clone and fork, and it still works at the provider.# 1. Revoke the key in the provider's console, issue a new one,# and store the new key in your secret store or CI secrets.# 2. Keep local env files out of Git for goodecho '.env' >> .gitignoregit rm --cached --ignore-unmatch .env# 3. List every commit that ever contained the old keygit log --all --oneline -S 'FAKE-KEY-0000-DO-NOT-USE'# 4. Only if history must be cleaned: rewrite a fresh clonegit filter-repo --sensitive-data-removal --replace-text ../leaked.txtgit push --force --mirror origin--sensitive-data-removal needs git-filter-repo 2.47 or later. Git's own documentation calls the older git filter-branch a tool whose "use is not recommended" and points to git filter-repo instead.
.env file | |||
How to apply it in your project
Write the runbook before you need it For each secret, note who can revoke it, how to issue a new one and where the app reads it. OWASP's cheat sheet asks for exactly this, including "Who is the point of contact during an incident". Revoke and rotate first When a secret leaks, revoke it at the provider, issue a new one, put it in your secret store and redeploy. Moving the old value somewhere else doesn't count. Check how the old key was used Read the provider's access logs for the exposure window. Look for calls from unknown addresses or actions your app never makes. Decide whether history still needs cleaning Rotation usually ends the risk. Rewrite with git-filter-repo only when the data can't be rotated, such as personal data, and plan for clones, forks and open pull requests. Stop the next leak at the source Ignore env files, turn on push protection and secret scanning, and add a pre-commit secret scan, which OWASP suggests so secrets are caught "before commit/PR".
Key takeaways
Deleting a secret in a new commit leaves it in every earlier commit, and the cleanup diff shows it too. Clones, forks, caches and pull requests keep their copies; a history rewrite can't reach them. Moving a key to a .envfile changes where it's stored, not whether the leaked copy works.Revoke and rotate first: it's the only step that makes the leaked key useless. Treat history rewriting as optional cleanup, and use push protection to stop the next leak.
التعليقات (0)