Sub topic 14
What to do when a secret leaks
This chapter assumes you have used GitHub Actions or a similar CI system, know basic Git, and can read the workflow and IAM examples from earlier chapters. It covers the first hours after you find an exposed credential.
Treat every exposure as a compromise
A leaked secret is a secret that someone else may already hold. Bots scan public repositories within minutes of a push, and CI logs and artifacts may be readable by many people. Do not spend time asking whether anyone saw it. Assume they did and act on that.
The order of your actions matters. Revoke first, clean up second.
The response sequence
- Contain. Revoke or disable the credential at the service that issued it. Deleting the commit or the log does nothing to the credential itself.
- Replace. Issue a new credential and update the pipeline's secret store. Run the pipeline once to confirm that the new value works.
- Investigate. Use audit logs to find out what the old credential did between the time it leaked and the time you revoked it.
- Clean up. Remove the secret from the repository, logs, and artifacts so it does not keep spreading.
- Learn. Find the leak path and close it so it does not happen again.
Contain: revoke at the source
Revocation happens in the system that accepts the credential, not in your CI platform. Deleting a GitHub secret only removes your copy. The old value still works until the provider disables it.
For an AWS access key, deactivate it first. Deactivation is reversible, which helps if you are unsure what depends on the key.
aws iam update-access-key \
--user-name ci-deployer \
--access-key-id AKIAEXAMPLEKEYID123 \
--status InactiveOnce you have confirmed that nothing legitimate still uses the key, delete it.
If the leaked item was an OIDC token, it expires on its own after a short time. That is one benefit of short-lived credentials. If the trust policy was too broad, tighten the sub condition anyway.
Investigate what the secret did
Check the provider's audit trail for activity by the leaked credential. In AWS, search CloudTrail for the access key ID.
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=AccessKeyId,AttributeValue=AKIAEXAMPLEKEYID123 \
--max-results 50Look for actions you did not expect, such as new IAM users, new access keys, resources created in unused regions, or data downloads. If you find any, the incident is larger than one secret. Anything the attacker created may also need to be removed.
Clean up the repository, logs, and artifacts
Removing a secret from the latest commit leaves it in Git history. Rewriting history is cleanup, not containment. If the repository is public or widely cloned, forks and caches may still hold the old commits.
Tools such as git filter-repo can remove a value from history.
git filter-repo --replace-text replacements.txtHere replacements.txt lists the leaked value and what to replace it with. A rewrite changes commit hashes, so collaborators must re-clone or rebase. Coordinate before you force-push.
Also check these places:
- Workflow run logs. Delete the affected runs if your platform allows it.
- Build artifacts and caches that may contain a
.envfile or a config file. - Container images built during the exposure window.
- Copies in chat tools, tickets, and pull request comments.
Close the leak path
Ask how the secret got out. The answer decides the fix.
| Leak path | Typical fix |
|---|---|
| Committed to the repository | Enable push protection and secret scanning |
| Printed in a log | Pass secrets through `env`, avoid `set -x`, mask derived values |
| Exposed to a fork pull request | Do not expose secrets to untrusted code |
| Long-lived cloud key | Switch to OIDC and short-lived credentials |
| Third-party action | Pin actions to a commit SHA and limit permissions |
Write down what happened, when, and what you changed. A short record helps with later audits and with training the team.
Exercise
A developer pushes an AWS access key to a public repository, then notices and force-pushes a commit that removes it ten minutes later. What should you do first, and why is the force-push not enough?
Show answer
Deactivate the access key in IAM first, then create a replacement and check CloudTrail for activity under the old key ID. The force-push only changes the repository's history. Bots may already have copied the key, and forks or cached commits can still contain it. The key stays valid until the provider revokes it.
Exercise
Why is deactivating an access key often a better first step than deleting it?
Show answer
Deactivation stops the key from working immediately but can be undone. If a production system turns out to depend on the key, you can reactivate it briefly while you roll out a replacement. After you confirm nothing legitimate uses it, delete it.