Sub topic 2
Rotate the credential first
This chapter assumes you can clone, commit, branch, and push with Git, and that you know commits are identified by hashes. You also need a command line and permission to change the credential at the service that issued it.
Treat the secret as compromised
Once a secret reaches a remote repository, assume someone else has a copy. Automated scanners watch public hosts and often find new keys within minutes. Clones, forks, CI caches, and local backups can also hold the old history. Rewriting history later removes your copy of the secret. It does not remove copies other people or systems already made.
Rotating means making the leaked value useless. You revoke it, or replace it with a new one, at the service that issued it. Do this before you clean the repository. If you rewrite history first, the secret stays valid and exposed for the whole time the cleanup takes.
The order of work
- Revoke or disable the leaked credential at its source.
- Issue a new credential and deploy it to the systems that need it.
- Keep the new value outside the repository, for example in a secrets manager or an environment variable.
- Check the service's audit logs for use you do not recognize.
- Only then start the history rewrite.
Steps 1 and 2 can be a short outage if services still use the old value. Accept that cost. A brief interruption is better than leaving a working key exposed.
Check for misuse
Rotation stops future use. It does not tell you whether the key was already used. Look at the issuing service's logs for the period since the commit was pushed.
For an AWS access key, you can check when it was last used:
aws iam get-access-key-last-used --access-key-id AKIAEXAMPLEKEY12345You can also deactivate it without deleting it, which keeps the evidence while blocking use:
aws iam update-access-key \
--access-key-id AKIAEXAMPLEKEY12345 \
--user-name deploy-bot \
--status InactiveReplace the key ID and user name with your own values. The key in these examples is a placeholder. Other services have equivalent controls, such as token revocation pages or API calls. If you find unexpected activity, treat it as a security incident and follow your organization's process.
Common mistakes
- Cleaning first, rotating later. The secret stays valid while you work.
- Assuming a private repository is safe. Everyone with read access, and every tool they use, may have a copy. Rotate anyway.
- Deleting the file in a new commit. The old commit still contains the secret and can be checked out by anyone.
- Reusing the old value with small changes. Issue a fully new credential.
- Putting the new secret in the repository. This repeats the original mistake.
What rotation does not cover
Rotation neutralizes the credential. The leaked text may still be sensitive in other ways. For example, a file may reveal internal hostnames or personal data. The next chapters show how to remove the text from history, but they do not replace rotation.
Exercise
A teammate finds a live API token in a commit from three weeks ago. They propose running git filter-repo today and rotating the token next week, when the release is done. What is wrong with this plan, and what should happen instead?
Show answer
The token stays valid, and exposed to anyone who already copied it, for a week or more, and the rewrite cannot remove those copies. Instead, revoke the token and issue a new one first, deploy the new value from outside the repository, and review the service logs for misuse. Then rewrite history.
Exercise
Why is it still necessary to rotate a credential that was committed only to a private repository?
Show answer
Anyone with read access, including former collaborators, and any clone, fork, CI cache, or backup may hold the old history. You cannot confirm that none of them copied the secret, so it must be treated as compromised.