You pushed a key. Maybe a scanner told you, maybe a teammate, maybe the provider sent the email nobody wants. The next ten minutes matter more than the next ten days, and most of the damage I saw in incident response came from doing the right steps in the wrong order. Here is the order. Print it if that helps.
Minute 0 to 2: revoke or rotate at the issuer
Go to the service that issued the key and kill it. Not your repo, not your editor. The issuer. For Stripe that is Developers, API keys, Roll key. For AWS it is IAM, the user, Security credentials, then make the access key inactive and create a new one. For GitHub it is Settings, Developer settings, then delete the token.
If production will break the moment the old key dies, you have two options. Some issuers let you roll with a grace period, where the old key keeps working for a set number of hours. Use the shortest window you can survive. If there is no grace period, take the outage. A few minutes of failed requests is cheaper than an attacker with a working payment key.
- Create the replacement with the narrowest scope the code needs. A leak is a good moment to stop using a root key for a job that needs read access to one bucket.
- Put the new value straight into your secret manager or your host's env settings. Do not paste it into chat, a ticket, or a scratch file.
- If the key was half of a pair, rotate both halves.
Minute 2 to 4: read the provider audit log
You need to know whether this is a near miss or a breach, and the issuer's log is the only source that can tell you. Filter by the key ID and look at the window from the leaking commit until the moment you revoked.
- AWS: CloudTrail, Event history, filter on the access key ID. Look for
GetCallerIdentity,ListBuckets,RunInstances, and any IAM call. - Stripe: Developers, Logs. Filter by API key and look for source IPs that are not yours.
- GitHub: the security log for a personal token, the org audit log for anything broader.
- Databases: connection logs by source IP. A connection string leak means checking for new users and new rows too, not only reads.
If you see activity you can not explain, stop this runbook and start your incident process. Everything past this point assumes the log is clean.
Minute 4 to 6: redeploy with the new value from env
Now touch the code. Replace the literal with an environment read, set the new value in the deploy target, and ship. Fail loudly if the variable is missing. A silent fallback to an empty string turns a config mistake into a confusing auth error at three in the morning.
// before
const stripe = new Stripe("sk_l••••••••YhJ9");
// after
const key = process.env.STRIPE_SECRET_KEY;
if (!key) throw new Error("STRIPE_SECRET_KEY is not set");
const stripe = new Stripe(key);Confirm that .env is in .gitignore and that a .env.example with empty values is committed in its place. Then verify the deploy is healthy on the new key before you move on.
Minute 6 to 8: purge it from history
The key is dead, so this step is cleanup, not containment. It still matters: a dead key in history trains the next scanner run to cry wolf, and it tells a reader how your keys are named and where they live. Work in a fresh clone.
git clone git@github.com:acme/api.git api-clean && cd api-clean # a whole file that should never have been committed git filter-repo --invert-paths --path .env # or a value embedded in a file you need to keep # expressions.txt holds one line: THE_OLD_VALUE==>REMOVED git filter-repo --replace-text ../expressions.txt git remote add origin git@github.com:acme/api.git git push origin --force --all git push origin --force --tags
Two warnings. expressions.txt contains the full old secret, so keep it outside the repo and delete it when you finish. And every collaborator has to re-clone. Anyone who pulls into an old clone and pushes will put the old commits right back.
Minute 8 to 9: add a pre-commit and CI scan
The same mistake will happen again, because the mistake is human. Put a scanner in front of the commit and another in front of the merge. Pre-commit catches it on the laptop. CI catches the person who skipped the hook.
# .pre-commit-config.yaml
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.24.2
hooks:
- id: gitleaksRun pre-commit install once per clone. In CI, run the same scanner on every pull request and make the check required. If you sweep with Leaksweep, the CI config is generated for the host your repo already uses, with an allowlist seeded from the findings you dismissed.
Minute 9 to 10: write the two-line incident note
Write it now, while you remember the times. Two lines is enough, and two lines actually gets written.
2026-09-11 14:02 MT Stripe live key (sk_l••••YhJ9) committed in scripts/seed.ts, pushed 13:51. Rolled 14:04, API logs clean 13:51 to 14:04, redeployed from env 14:07, history purged, gitleaks added.
Masked key, where it was, when it leaked, when it died, what the log showed, what changed. Put it wherever your team keeps such things. Six months from now, when an auditor or a customer asks whether you have ever had a credential exposure, you will have an answer with timestamps instead of a shrug.
The order is the point
- Revoke first, because nothing else reduces risk.
- Read the log second, because it decides whether you are done in ten minutes or ten days.
- Fix the code third, because now it is safe to take your time.
- Clean history fourth, because it is hygiene.
- Prevent fifth, write it down sixth.
Most teams I worked with did this backwards. They deleted the line, force-pushed, and felt better, with a live key still sitting in a scraper's queue. Deleting is not remediation. Rotation is.