Skip to content
mentoor

Topic 4 of 4 · 5 min read

Detecting and rotating leaked AI keys

Goal: Respond to a leaked model API key by detecting, revoking, rotating and checking usage in the right order.

Treat every leaked key as already used: revoke it first, then replace it, then look at what it did. Deleting the commit or the message does not help, because copies exist the moment a secret is published.

Check yourself · 1 of 3

A model API key was pushed to a public repo ten minutes ago. What do you do first?

Choose one answer

Why it matters

Model API keys are valuable. A stolen key can run someone else's workloads on your bill, or reach your data through a connected tool. Attackers and bots scan public code for key patterns. GitHub's secret scanning and push protection exist because this happens. A leak can also happen in places that are not repos: chat transcripts, support tickets, screenshots, build logs and agent traces.

Detect early

  • Turn on secret scanning and push protection in your repositories, and run a scanner such as gitleaks or trufflehog in CI and as a pre-commit hook.
  • Scan agent transcripts, logs and ticket exports for key patterns too.
  • Set spend limits and alerts with your model provider, and watch for usage from unusual regions, models or times.
  • Use separate keys per app and environment, so an alert tells you which one leaked.

The rotation runbook, in order

  1. Revoke or disable the exposed key at the provider now. Do not wait to find the cause.
  2. Issue a new key with the narrowest scope, and store it in the secret manager.
  3. Deploy the new key to every place that used the old one, and confirm the apps work.
  4. Check usage for the exposure window: requests, models, spend, IP addresses. Look for other secrets nearby, such as a database URL in the same file.
  5. Clean up. Remove the secret from the repo and from history if you can, but do not rely on it. Add a detection rule for the pattern.
  6. Write it up: what leaked, how, how long it was exposed, and the change that prevents a repeat.

Before and after

Weak: a developer notices a key in a public commit, force-pushes to remove it, and moves on. The key is still valid and has been copied.

Better: the developer revokes the key in minutes, rotates it, reviews the provider's usage page for the window and adds a pre-commit scan.

What goes wrong

  • Rotating before revoking, which leaves the old key working while you plan.
  • Forgetting a second copy of the key in a CI variable or a teammate's laptop.
  • No spend alert, so abuse is found on the monthly invoice.

Examples

A runbook as a plan

  1. Revoke prod-chat-key in the provider console.
  2. Create prod-chat-key-2 limited to the models the app uses.
  3. Update the secret manager and redeploy both services.
  4. Review usage from the first commit time to revocation time.
  5. Add a pre-commit scan and a spend alert at a set daily limit.
  6. Post a short incident note.

A pre-commit scan

# .git/hooks/pre-commit
gitleaks protect --staged --redact || {
  echo "Possible secret staged. Commit blocked."; exit 1;
}

It stops the common case before the secret leaves the laptop.

Key points

  • Revoke the leaked key first, because rewriting history does not remove copies that already exist.
  • Secret scanning, push protection and pre-commit scanners catch keys before and after they are committed.
  • Leaks also happen in transcripts, logs, tickets and screenshots, so scan those too.
  • Separate keys per app and environment tell you quickly what leaked and limit the damage.
  • Spend limits and usage alerts at the model provider reveal compute abuse early.

Common mistakes

  • Deleting the commit and calling it fixed while the key is still valid.
  • Rotating the key but not checking what the old key did.
  • Using one shared key for every app, so nothing tells you where it leaked.
  • Having no spend alert on the model account.

Check yourself · 2 of 3

Why use a separate API key for each app and environment?

Choose one answer

Check yourself · 3 of 3

After you have rotated a leaked key, what should you still do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic