Sub topic 13
When a Secret Leaks: Revoke, Rotate, Review
This chapter assumes you can use Git and a command line, know what an API key or password does, and have a basic grasp of environment variables. It also assumes you have read the earlier chapters on where data flows to AI tools and how to keep secrets out of source code. Here you learn what to do after prevention has failed.
Treat every exposure as a compromise
A secret can reach an AI tool in several ways:
- You paste a log that contains a token.
- An agent reads
.envand echoes it in a reply. - A key gets committed while an assistant is editing your repository.
- A tool server sends a request body to a third party.
In every case, assume the value is known to someone other than its owner. You usually cannot prove it was used or not used. You can only make it worthless.
The response has three phases, in this order:
- Revoke: make the leaked value stop working.
- Rotate: issue a replacement and get your systems running on it.
- Review: find out what the old value was used for, then fix the cause.
Cleaning up Git history or deleting a chat is not part of the first two phases. Those actions hide the secret but do not make it safe.
Why rotate before you clean up
Teams often start by rewriting Git history, because that feels like removing the problem. This is the wrong first move for three reasons.
- Automated scrapers watch public repositories and can pick up a pushed key within minutes. Deleting the commit later does not recall copies they already took.
- Forks, clones, CI caches, and pull request refs can keep the old commit.
- A prompt sent to a provider may sit in retention storage or logs outside your control.
Revoking takes seconds and ends the risk. Cleanup takes hours and does not end it.
Step 1: Identify the secret and its scope
Before you revoke, answer four questions quickly:
- What kind of credential is it (cloud key, database password, API token, signing key)?
- Which system issued it, and who can revoke it?
- What could it do? Check the attached permissions, not the name.
- Where did it go (public repo, private repo, chat, pasted log, agent transcript)?
Scoped credentials from the earlier chapter pay off here. A read-only token limited to one project is a small incident. An admin key is a large one.
Step 2: Revoke
Revoke at the issuer. Changing the value in your application does not invalidate the old one.
For an AWS access key, deactivate first rather than delete. Deactivation is reversible, so if something critical still uses the key you can turn it back on while you fix the dependency.
aws iam update-access-key \
--user-name ci-deployer \
--access-key-id AKIAEXAMPLEEXAMPLE1 \
--status InactiveReplace the user name and key ID with your own. Once the replacement is deployed and you have finished reviewing, delete the old key:
aws iam delete-access-key \
--user-name ci-deployer \
--access-key-id AKIAEXAMPLEEXAMPLE1Other issuers follow the same pattern. GitHub personal access tokens are revoked in the account settings. Database passwords are changed with the database's own user-management command. Cloud keys are revoked in the provider's IAM console or CLI.
Some secrets cannot be revoked by flipping a switch, such as a signing key that clients have pinned or a shared password used in many places. For these, plan the rotation carefully, because the old value stays valid until you replace it everywhere.
Step 3: Rotate and redeploy
Create the replacement and store it where the earlier chapters put secrets: a secrets manager or the runtime environment, never the source tree.
aws iam create-access-key --user-name ci-deployerThe output contains the new secret key exactly once. Send it straight to your secrets store and do not paste the output into a chat or an AI prompt. This is the same mistake that caused the incident, and it is easy to make while stressed.
Then:
- Update every consumer: CI variables, deployed services, developer machines, scheduled jobs.
- Restart or redeploy anything that caches the old value in memory.
- Confirm the application works with the new value.
- Delete the old credential, if you only deactivated it.
If the application reads its credential from an environment variable at startup, a rotation needs a restart. A service that never restarts will keep using a dead key until it fails.
Step 4: Review what happened
Now check whether anyone used the old value. Start with the issuer's audit log. On AWS, CloudTrail records management events for the last 90 days, and you can filter by access key:
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=AccessKeyId,AttributeValue=AKIAEXAMPLEEXAMPLE1 \
--max-results 50Look for calls from unfamiliar IP addresses, regions you do not use, user agents that differ from your tooling, and actions such as creating users, changing policies, or launching instances. Note that this lookup covers management events only. Data access such as S3 object reads appears only if you enabled data event logging beforehand.
If you find misuse, move to a wider response: contain the affected resources, revoke anything the attacker created, notify your security contact, and check whether other credentials were reachable from that system.
If you find no misuse, record that, together with the limits of your logs. "No evidence of use" is a weaker statement than "not used".
Step 5: Clean up copies
After rotation, the leaked value is harmless, but removing it still reduces noise and stops scanners from raising the same alert repeatedly.
Rewriting history needs care because it changes commit hashes for everyone. A common tool is git filter-repo. Put one replacement rule per line in a file:
AKIAEXAMPLEEXAMPLE1==>REMOVED_KEYThen run it from a fresh clone:
git filter-repo --replace-text replacements.txtForce-pushing the result is destructive, so coordinate with your team first. Teammates must re-clone or reset, and hosting providers may keep the old objects reachable until they run garbage collection, so contact their support if the content is sensitive.
For AI tools, also:
- Delete the conversation or session where the secret appeared, if the tool allows it.
- Check local transcript, history, and log files that an agent or editor extension wrote to disk.
- Review the provider's retention terms from the earlier chapter to understand how long the prompt may persist. Deleting a chat may not delete backend copies.
None of this replaces rotation. It only reduces who can see the old value.
Different leaks, different first actions
| Where it leaked | First action | Extra follow-up |
|---|---|---|
| Public Git repository | Revoke immediately | Check audit logs closely; assume scraped |
| Private Git repository | Revoke and rotate | Review who has repository access |
| Pasted into an AI chat | Revoke and rotate | Delete the chat; check retention terms |
| Echoed by an agent in terminal output | Revoke and rotate | Check shell history and agent transcripts |
| Sent to a tool server | Revoke and rotate | Review that server's trust and logging |
Find the cause and add controls
An incident is incomplete until you know why the secret was reachable. Ask:
- Was the secret in a file that an ignore rule should have excluded?
- Was it hard-coded because the environment-based setup was inconvenient?
- Did the agent have read access it did not need?
- Was the credential broader or longer-lived than the task required?
Then add a control that would have caught it. Pre-commit scanning stops the most common repeat. This configuration runs Gitleaks on staged changes. Pin rev to a current release tag of the project.
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.4
hooks:
- id: gitleaksInstall it and scan everything already tracked, so existing leaks surface too:
pip install pre-commit
pre-commit install
pre-commit run --all-filesPre-commit hooks run only on machines where someone installed them, and they can be skipped. Back them up with the same scanner in CI.
Gitleaks can also scan the full commit history, which is useful during an incident to find other secrets in the same repository. Recent versions use gitleaks git, while older ones use gitleaks detect. Check the version you have installed.
Exercise
A developer pastes a CI log into an AI chat and notices it contains a live database password. Put these actions in a sensible order: (a) delete the chat, (b) change the database password and update the services that use it, (c) check database logs for logins from unfamiliar hosts, (d) add the log-redaction step to the team's workflow. Explain why (a) is not first.
Show answer
The order is (b), (c), (a), (d). Changing the password comes first because it makes the leaked value useless, whatever happens to the chat. Reviewing the logs next shows whether anyone used the password before the change. Deleting the chat reduces who can see the old value, but the provider may keep copies and the password is already compromised, so it does not remove the risk. Adding redaction last fixes the cause so the leak does not repeat.