Sub topic 13
When a secret leaks: revoke, rotate, review
This chapter assumes you can use Git and a shell, know what an API key is for, and can set environment variables. It covers what to do after a credential has been exposed to an AI tool, a prompt, a log, or a commit.
Assume the secret is compromised
A leaked secret is any credential that left the place it was meant to live. Common cases are a key pasted into a chat, a key echoed in command output that an agent read, and a key committed to Git. You usually cannot prove that nobody copied it. Provider logs, shared transcripts, forks, and CI caches may all hold a copy. So treat the secret as compromised and make it useless.
The order matters. Rotate first, clean up second. Rewriting Git history feels like a fix, but it does not help while the old key still works.
The three steps
- Revoke. Disable the exposed credential at its source, such as the cloud console or the API provider's dashboard. A deleted key cannot be used again.
- Rotate. Create a replacement, store it in your secret manager or environment, and update every service that used the old one. If you can, create the new key before revoking the old one, so production stays up.
- Review. Check what the credential did while it was exposed.
Review what happened
Look at the credential's activity from the time of exposure until revocation. For an AWS key, CloudTrail records API calls made with it. Look for calls from unfamiliar IP addresses, new users or roles, new compute resources, and reads of storage you did not expect.
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=AccessKeyId,AttributeValue=AKIAEXAMPLEKEYID \
--max-results 50Replace the example key ID with the exposed one. Scoped, short-lived credentials limit what you need to examine, because a key that could only read one bucket has a small blast radius.
Clean up the copies
After the old secret is dead, remove the traces so they do not mislead people or trigger repeat alerts.
- Delete the pasted text from shared chats or notes where you can.
- Check your AI tool's history and any shared conversation links.
- If the secret was committed, remove it from history with a tool such as
git filter-repo, then force-push. Ask collaborators to re-clone. Forks and clones you do not control may still hold it, which is why revocation comes first. - Check CI logs and build artifacts.
Also ask your provider about deleting stored conversations. Retention terms differ by plan, so check the policy from the earlier chapter on provider data.
Fix the cause
A leak is information about a gap. Match the cause to the control that was missing.
| Cause | Control to add |
|---|---|
| Key hardcoded in source | Read it from the environment at runtime |
| Secret file visible to the assistant | Ignore rules for the AI tool |
| Key pasted in a prompt | Redaction before pasting |
| Commit contained a key | Pre-commit secret scanning |
Write a short incident note covering what leaked, when, how it was found, what you rotated, and what you changed. Teams can reuse these notes in their policy.
Exercise
You find an API key in a commit pushed an hour ago to a private repository. A teammate suggests rewriting history with git filter-repo right away. What should you do first, and why?
Show answer
Revoke the key at the provider, then issue and deploy a replacement. The key may already be cloned, cached by CI, or seen by others, so a history rewrite does not make it safe. After rotating, review the provider's logs for use since the commit, then rewrite history and add secret scanning to prevent a repeat.