Sub topic 14
What to Do When a Key Leaks
This chapter assumes you can use Git and a terminal. It also assumes you know what a .env file is and have created a key in a provider console. A leaked key is not a disaster if you act in the right order. The goal is to stop the key from working first, then clean up.
Why speed matters
Automated scrapers watch public repositories and paste sites. A key pushed to a public repository can be found and used within minutes. Treat any key that left your machine in plain text as compromised, even if you deleted it a moment later. Deleting a file does not remove the key from Git history, from forks, or from copies other people already made.
The response order
1. Revoke first
Open your provider console and revoke (disable) the exposed key. Do this before anything else. Rewriting Git history takes time, and the key keeps working until you revoke it.
2. Replace the key
Create a new key and put it in your environment variable, .env file, or CI secret. Then restart or redeploy anything that used the old key. If you had separate keys for development, testing, and production, only the exposed one needs replacing. This is one benefit of using separate keys.
3. Review usage and billing
Look at the provider's usage dashboard for the period since the exposure. Check for:
- Requests you did not make, or from unexpected times.
- Spikes in token counts or spend.
- Models you do not normally call.
If you find charges you did not cause, contact the provider's support and report them. Many providers will review fraudulent usage, but you need to report it quickly. A spend limit set earlier caps how much damage can occur here.
4. Clean up the repository
Revocation makes the old key useless, so cleanup is about hygiene, not safety. Still, do it so that scanners stop flagging the repository.
If the file is still tracked, stop tracking it and add it to .gitignore:
git rm --cached .env
echo ".env" >> .gitignore
git check-ignore -v .env
git commit -m "Stop tracking .env"This removes the file from future commits only. The key remains in earlier commits. To remove it from history, use a tool such as git filter-repo, then force-push and ask collaborators to re-clone. This rewrites shared history, so tell your team before you do it. Because the key is already revoked, you can skip the rewrite if it would cause more trouble than it solves.
Also check other places the key may have gone:
- Logs, error reports, and CI output.
- Chat messages, tickets, and screenshots.
- Forks, pull requests, and copied code snippets.
5. Find the cause and prevent a repeat
Work out how the key leaked. Common causes are a committed .env, a key hard-coded in a script, or a key placed in client-side code. Then add a guard. Enable secret scanning on your repository and add a pre-commit check so the same mistake is caught earlier next time.
Quick reference
| Step | Action | Why |
|---|---|---|
| Revoke | Disable the key in the console | Stops all use immediately |
| Replace | Issue and deploy a new key | Restores your service |
| Review | Check usage and billing | Finds misuse and costs |
| Clean up | Untrack file, optionally rewrite history | Removes the key from view |
| Prevent | Scanning and pre-commit checks | Catches the next mistake |
Exercise
You notice you pushed a .env file with your LLM key to a public repository ten minutes ago. What is the first thing you do, and why not delete the file and push again?
Show answer
Revoke the key in the provider console first. Deleting the file and pushing again only removes it from the latest commit. The key stays in Git history, and anyone who already copied it can still use it until it is revoked. After revoking, create a new key, review usage, and then clean up the repository.
Exercise
After revoking and replacing a leaked key, you see a spike in requests in the usage dashboard that you did not make. What should you do?
Show answer
Report it to the provider's support and ask them to review the charges. Also check which models were called and when, to confirm the time of the misuse. A spend limit configured beforehand would have capped the cost.