Topic 4 of 4 · 5 min read
Detecting and rotating leaked AI keys
Goal: Respond to a leaked model API key by detecting, revoking, rotating and checking usage in the right order.
Treat every leaked key as already used: revoke it first, then replace it, then look at what it did. Deleting the commit or the message does not help, because copies exist the moment a secret is published.
Check yourself · 1 of 3
A model API key was pushed to a public repo ten minutes ago. What do you do first?
Why it matters
Model API keys are valuable. A stolen key can run someone else's workloads on your bill, or reach your data through a connected tool. Attackers and bots scan public code for key patterns. GitHub's secret scanning and push protection exist because this happens. A leak can also happen in places that are not repos: chat transcripts, support tickets, screenshots, build logs and agent traces.
Detect early
- Turn on secret scanning and push protection in your repositories, and run a scanner such as gitleaks or trufflehog in CI and as a pre-commit hook.
- Scan agent transcripts, logs and ticket exports for key patterns too.
- Set spend limits and alerts with your model provider, and watch for usage from unusual regions, models or times.
- Use separate keys per app and environment, so an alert tells you which one leaked.
The rotation runbook, in order
- Revoke or disable the exposed key at the provider now. Do not wait to find the cause.
- Issue a new key with the narrowest scope, and store it in the secret manager.
- Deploy the new key to every place that used the old one, and confirm the apps work.
- Check usage for the exposure window: requests, models, spend, IP addresses. Look for other secrets nearby, such as a database URL in the same file.
- Clean up. Remove the secret from the repo and from history if you can, but do not rely on it. Add a detection rule for the pattern.
- Write it up: what leaked, how, how long it was exposed, and the change that prevents a repeat.
Before and after
Weak: a developer notices a key in a public commit, force-pushes to remove it, and moves on. The key is still valid and has been copied.
Better: the developer revokes the key in minutes, rotates it, reviews the provider's usage page for the window and adds a pre-commit scan.
What goes wrong
- Rotating before revoking, which leaves the old key working while you plan.
- Forgetting a second copy of the key in a CI variable or a teammate's laptop.
- No spend alert, so abuse is found on the monthly invoice.
Examples
A runbook as a plan
- Revoke
prod-chat-keyin the provider console. - Create
prod-chat-key-2limited to the models the app uses. - Update the secret manager and redeploy both services.
- Review usage from the first commit time to revocation time.
- Add a pre-commit scan and a spend alert at a set daily limit.
- Post a short incident note.
A pre-commit scan
# .git/hooks/pre-commit
gitleaks protect --staged --redact || {
echo "Possible secret staged. Commit blocked."; exit 1;
}
It stops the common case before the secret leaves the laptop.
Key points
- Revoke the leaked key first, because rewriting history does not remove copies that already exist.
- Secret scanning, push protection and pre-commit scanners catch keys before and after they are committed.
- Leaks also happen in transcripts, logs, tickets and screenshots, so scan those too.
- Separate keys per app and environment tell you quickly what leaked and limit the damage.
- Spend limits and usage alerts at the model provider reveal compute abuse early.
Common mistakes
- Deleting the commit and calling it fixed while the key is still valid.
- Rotating the key but not checking what the old key did.
- Using one shared key for every app, so nothing tells you where it leaked.
- Having no spend alert on the model account.
Check yourself · 2 of 3
Why use a separate API key for each app and environment?
Check yourself · 3 of 3
After you have rotated a leaked key, what should you still do?
Practice items, not counted towards your mastery.
Start here
- Secrets Management Cheat Sheet (OWASP) (opens in a new tab)ArticleFree
Central storage, short-lived secrets, rotation and auditing: the baseline every agent setup should meet.
- MCP specification: Authorization (opens in a new tab)StandardFree
How MCP servers and clients must handle OAuth, audience-bound tokens and token passthrough.
- MCP security best practices (opens in a new tab)DocsFree
Names the confused deputy and token passthrough attacks and what servers must do instead.
More for this topic
- About secret scanning (GitHub Docs) (opens in a new tab)DocsFree
What scanning and push protection catch in repos, and how alerts are handled.