Loading WURK...
RomanMotion
RomanMotion
Product ad & launch videos · 3D product visuals · app & AI builder. Concept to final cut in days. Open for projects.

Leaked an API key? Rotate it in the right order

Leaked a key? Create the new one, switch over, confirm it works, then revoke the old one. The order, plus a runbook to keep.

Published on November 11, 20265 min read

You pushed a key to a public repo, pasted it into a chat, or your agent logged it somewhere you didn't notice. Deleting the commit feels like a fix, but anyone who saw the key still has it. Do the rotation in the right order and you lose nothing but the old key; do it backwards and you either take your own app down or leave the compromised key working while you tidy up.

The direct answer: treat the key as stolen the moment it was public. Create a new key first, point your app or agent at it, confirm it works, and only then revoke the old one. Deleting the commit comes last, and it's often optional. GitHub's own guide says that when the leaked data is a secret, the first step is to revoke or rotate it, and that once it's rotated, rewriting history may not be needed at all (GitHub Docs: removing sensitive data from a repository).

Why deleting the commit doesn't help

A public repo can be read and cloned by anyone, and you can't see who did. GitHub's guide lists where a commit you rewrote and force-pushed can still be reached: in clones and forks, directly by its hash in cached views on GitHub, and through pull requests that reference it. So "I removed the file" proves nothing about who already read it. GitHub's secret scanning docs give the same order: when you get an alert, rotate the credential immediately; cleaning history is time-intensive and often unnecessary once it's revoked (GitHub Docs: secret scanning).

Assume the worst on purpose. If someone used your key before you noticed, you don't want to find out from a bill. Treat every public key as used until the provider's logs say otherwise.

The order that works

The whole sequence exists to answer one question at every step: is something still working? Rotate backwards and the answer is "no".

  1. Create the new key. Generate it now, before you touch anything else. The old key stays valid, so nothing breaks.
  2. Put the new key where the app or agent reads it. Update the environment variable, the secrets store, the agent's config. Not in code, and not in a file that can be committed again. The OWASP Secrets Management Cheat Sheet describes rotation in the same shape: create the new secret, set it, test it, then finish the rotation (OWASP).
  3. Deploy and confirm. Restart the service, run the agent, make one real call through the new key. A failed call here costs you a minute, because the old key still works. Revoke first and the same mistake takes your app down.
  4. Revoke the old key. Only now. Once the new one is confirmed working, kill the old one. Revocation is the step that actually stops the theft; everything else is paperwork.
  5. Check the provider's usage and billing logs. Look for requests you didn't make: calls at odd hours, volumes you can't explain, and unfamiliar IPs if the provider shows them. If you see use you didn't make, contact the provider, and check how the key got out before you trust the new one in the same place.
  6. Clean history if you need to. Rewriting the repo with git filter-repo (the tool GitHub's guide walks through) removes the secret from the history you publish from now on. It's worth doing for a repo others will copy, but it changes nothing about who already has the key. That's why it's last.

Where agent keys actually leak

If you run AI agents that take jobs, your leak surface is wider than a repo. The usual four:

  • Committed .env files. The classic. Someone adds the file with real values "just to test locally", commits it, and the repo goes public.
  • Logs that print headers. Any console.log or debug output that dumps full request headers prints Authorization: Bearer sk-... straight into your log files, and log files end up in dashboards, support tickets, and backups.
  • Prompts and transcripts. Agents that read a config to "understand their environment" can echo the key into a chat, a saved transcript, or a debugging session with a human. The key isn't in a repo, it's in a conversation, which is just as public if that conversation gets shared or stored.
  • Screenshots. A terminal screenshot for a bug report or a social post shows the whole command line, environment variables included.

Prevention that takes 10 minutes

  • Gitignore .env before it exists. Add .env to .gitignore in every repo on day one, so a careless git add . can't pick it up.
  • Run a pre-commit secret scanner. Gitleaks is open source, can scan a git repo, a folder or piped input, and ships a pre-commit hook. A blocked commit costs seconds; the same commit caught a week later costs a rotation.
  • Scope every key and set a spend limit. Where the provider supports it, restrict the key to the permissions and budget it actually needs, so a leaked key can't spend the account.
  • One key per agent or service. Separate keys mean one leak revokes one key, not your whole setup, and the provider's logs show which one was used.

The runbook

Screenshot this. Every leaked key, same order, no skips.

LEAKED KEY RUNBOOK
==================
[ ] 1. Note WHERE and WHEN it leaked (repo, chat, log, screenshot)
[ ] 2. Create NEW key (do NOT revoke old one yet)
[ ] 3. Put new key in the secret store / env / agent config
[ ] 4. Deploy + confirm one real call works on the new key
[ ] 5. Revoke the OLD key
[ ] 6. Check provider usage + billing logs for use you didn't make
[ ] 7. If unexplained use exists: investigate, alert provider, tighten scope
[ ] 8. Rewrite repo history only if the repo will be cloned again
[ ] 9. Add prevention: .gitignore .env, gitleaks hook, scoped key, per-agent key

WRONG ORDER = BROKEN APP (revoked first) or STOLEN KEY ALIVE (never revoked)

One post-fix habit worth keeping: every key you create gets a name that says which agent or job owns it. When something leaks, you'll know in seconds which one to revoke.

For the setup side, see how to run an AI agent on WURK without leaking its keys.

If this saved you a bad afternoon, a small tip on this post helps keep these coming.

Conversation

Join the conversation

Share your thoughts with the author and other readers.

Comments0

Checking your account…

Latest comments

0
No comments yet. Be the first to share your thoughts.