Leaked an API key? Do these 5 steps in this order
Deleting the key from your code is not step 1. Anyone who already copied it can still use it.
Published here , tested on temp repo, fake key · 3 sources · Resume reading
Deleting a leaked key from your code does not stop anyone who already copied it. The old commits keep it anyway. Five steps in this order, built on GitHub's docs (checked Nov 8): rotate first, rewrite history last.
1. Revoke or rotate the key at the provider
This is the only step that stops use. GitHub's docs say "as a first step you need to revoke and/or rotate that secret." [1]
2. Check the provider's usage log
Look for calls you did not make, from the first push until you rotated [2].
3. Put the new key in an env var or secret store
Never in code.
const key = process.env.API_KEY;4. Find every copy in history
Search only the first few characters so the full key stays out of your shell history. The diff still prints it.
git log --all -p --oneline -S "sk_fake_ab"In my test: grep said gone, git log -S printed the key from both commits.
5. Rewrite history with git-filter-repo
Use version 2.47 or later [1]:
git-filter-repo --sensitive-data-removal --replace-text ../passwords.txtThe file passwords.txt holds the leaked key, one per line. Filter-repo replaces it with ***REMOVED***. Then force push as the GitHub docs show.
Forks and clones keep the old commits anyway. That is why step 1 comes first.
The rule
Rotate, check, replace, find, clean.
Send this to the friend who deleted a leaked API key from their code and thinks it is fixed.
Keep this guide
Filed under Security
Questions about this guide
Why search only the first few characters?
So the full key stays out of your shell history. The diff still prints the whole thing.
Why is rewriting history last?
Forks and clones keep the old commits anyway. Rotating first is the only step that stops use.
Sources
- GitHub docs: removing sensitive data from a repositorydocs.github.com
- GitHub docs: remediating a leaked secretdocs.github.com
- Git docs: git-loggit-scm.com