Guide

Leaked an API key on GitHub? What to do in the next 10 minutes

You (or your AI coder) committed a real key and pushed it. Take a breath: the fix is mostly clicking the right buttons in the right order. The order matters more than anything else in this guide. Make the leaked key useless first. Cleaning up the repo comes after, and is often optional.

Updated 9 min read

First: rotate the key, don't just delete the commit

Deleting the file, or even rewriting git history, doesn't take the key back. Anyone who cloned the repo, opened the commit, or runs one of the bots that watch public pushes may already have a copy. GitHub's own guide puts it plainly: if the data is a secret, as a first step you need to revoke and/or rotate that secret. Once it's revoked, the copy in your history is just a dead string, and GitHub notes that this alone may be enough.

How fast is fast enough? In 2023, Palo Alto Networks' Unit 42 watched an attack campaign that found and used AWS keys exposed on GitHub within five minutes of them being pushed. Treat any key that reached a public repo as already copied. A private repo is less urgent but not safe: forks, clones, CI logs and anyone with access all have it.

Rotation steps, provider by provider

Find the key's prefix to know whose it is, then follow that provider's steps. These are taken from each provider's current docs (linked at the end).

Key starts withProviderWhere to rotate it
sk_live_, rk_live_Stripe (live)dashboard.stripe.com/apikeys (opens in a new tab)
sk-proj-, sk-OpenAIOpenAI API keys page (opens in a new tab)
sk-ant-AnthropicClaude Console API keys (opens in a new tab)
sb_secret_, or a JWT with "role":"service_role"SupabaseProject Settings, API Keys
AKIA (plus a 40-character secret)AWSIAM console, Users, Security credentials
ghp_, github_pat_GitHubgithub.com/settings/tokens (opens in a new tab)

Stripe

  1. Open the API keys page

    In the Stripe Dashboard, go to API keys (opens in a new tab). Find the leaked key under Standard keys or Restricted keys.

  2. Rotate it now

    Click the overflow menu (⋯) next to it and choose Rotate key. In the Expiry dropdown pick Now, which deletes the old key straight away. (Any later expiry keeps both keys working for that long; that's for planned rotations, not leaks.)

  3. Copy the new key

    Click Rotate API key and copy the new value immediately. Stripe shows it once.

  4. Check what it was used for

    From the same ⋯ menu, View request logs shows recent requests made with the key. Look for refunds, payouts or customer exports you didn't make.

Stripe scans public repositories too, and says that if it detects an exposed secret or restricted key it notifies you and in some cases deactivates the key itself. Don't wait for that email. Also consider moving your app to a restricted key with only the permissions it needs: Stripe now recommends them over full secret keys, and they limit what a leaked key can do.

OpenAI

  1. Revoke the key

    Open the API keys page (opens in a new tab), find the key, and revoke (delete) it. OpenAI's safety guide says to revoke an exposed key promptly and replace it.

  2. Create a new one

    Create a new project key. OpenAI recommends setting an expiration date on project keys, so a forgotten key stops working on its own.

  3. Check usage and cap spend

    Look at your usage for spend you don't recognise. Then set a hard limit: Project settings, Limits, Edit spend limit, enter a Monthly spend limit and turn on Enforce a hard limit. Spend alerts alone only notify you; a hard limit makes requests fail with a 429 once it's reached (OpenAI notes enforcement isn't instant, so spend can slightly exceed it).

Anthropic

In the Claude Console API keys page (opens in a new tab), open the key's three-dot menu and choose Delete API key, then create a new one. Anthropic is a GitHub secret scanning partner: when GitHub finds an Anthropic key in a public repo, Anthropic says it automatically deactivates it and emails you. If yours was deactivated, that's why your app stopped working; create a new key either way.

Supabase

Only the secret keys matter here. The publishable key (sb_publishable_, or the legacy anon key) is meant to be public: it ships in your frontend, and row level security is what protects your data from it. The secret key (sb_secret_, or the legacy service_role key) bypasses row level security entirely, so a leaked one is a full read and write pass to your database.

  1. Create a new secret key

    In the dashboard, open Settings, API Keys and create a new secret key.

  2. Swap it in everywhere

    Replace the old key in every server and job that uses it (your host's env vars, edge functions, cron jobs). If the leaked one was a legacy service_role JWT, replace it with the new secret key.

  3. Turn the old one off

    Once everything runs on the new key, delete the leaked secret key (this can't be undone), or for legacy anon and service_role keys, deactivate the legacy keys in the same section. Supabase says it automatically revokes the new-format secret keys it detects in public GitHub repos, but that doesn't cover legacy JWT keys.

AWS

  1. Deactivate the leaked key

    In the IAM console, open Users, pick the user, then the Security credentials tab. Under Access keys, choose Actions, Deactivate on the leaked key. Deactivating is instant and reversible; deleting is permanent.

  2. Create a replacement

    Choose Create access key for the same user, put the new pair in your app's env vars, and deploy.

  3. Delete the old key

    Once nothing needs it, choose Actions, Delete.

  4. Look for what it did

    Check CloudTrail's event history and your billing page for activity you don't recognise, such as new EC2 instances in regions you don't use. Rotating a user's key doesn't cancel temporary credentials someone may already have created with it, so look for those too.

If AWS detected the leak first, you may find a policy called AWSCompromisedKeyQuarantineV3 attached to the user and a support case open. AWS's instruction is not to remove that policy, and to follow the steps in the support case.

GitHub tokens

Delete the token at github.com/settings/tokens (opens in a new tab), create a new one with the fewest scopes that work, and check your security log for activity you don't recognise.

Put the new key where your app reads it, and redeploy

Your app should read keys from environment variables, never from code. On Vercel, open the project, choose Environment Variables, click the three dots on the variable, Edit, paste the new value and check which environments it applies to (Production, Preview, Development), then Save.

Then redeploy. Vercel's docs are explicit: environment variable changes only apply to new deployments. Open Deployments, the latest production deployment, the three dots, Redeploy. Until you do, production keeps the old value.

Text
# The key belongs in .env.local (for local dev) and in your host's settings.
# .env.local is gitignored; .env.example lists the names with no values.
STRIPE_SECRET_KEY=
OPENAI_API_KEY=
SUPABASE_SECRET_KEY=
A .env.example you can commit: names only, no values.

Then remove it from the code

With the key dead, take it out of the code so the next commit doesn't carry it forward:

  1. Replace the literal with process.env.STRIPE_SECRET_KEY (or whatever your provider's variable is called).
  2. If you committed a whole .env file, stop tracking it with git rm --cached .env and add .env* to .gitignore (plus !.env.example if you keep an example file).
  3. Commit and push. Run git status before each commit and make sure no .env file is listed. AI coding tools often run git add . for you, which is exactly how most .env files end up on GitHub.

Do you need to rewrite git history?

Often not. If the key is rotated, the copy in history can't do anything. Rewriting history is worth it when the file also held something you can't rotate (personal data, a private key you must keep), or when you simply don't want it there. It's disruptive, so know the cost first. GitHub's documented method uses git-filter-repo version 2.47 or newer:

Terminal
# Work on a fresh, normal clone (not --mirror)
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
cd YOUR-REPOSITORY

# Remove a file from every commit
git-filter-repo --sensitive-data-removal --invert-paths --path .env

# ...or replace the secret text wherever it appears.
# passwords.txt has one secret per line; it's replaced with ***REMOVED***
git-filter-repo --sensitive-data-removal --replace-text ../passwords.txt

# Overwrite GitHub's copy
git push --force --mirror origin
From GitHub's "Removing sensitive data from a repository". Pushes to refs/pull/* will be rejected; that's expected, they're read-only.

What GitHub warns about, in plain words:

  • Every commit hash after the change is rewritten, so everyone with a clone has to re-clone or rebase (not merge), or they'll push the secret right back.
  • Forks keep the old commits, and GitHub can't give you their owners' contact details.
  • Cached views and pull request references keep the old data until GitHub Support removes them. You request that through the Support portal, and Support helps only when rotating the credential can't solve the problem.
  • Closed pull requests lose their diffs, and commit signatures are stripped.

This is exactly why rotation comes first: history rewriting is slow and incomplete, and rotation is instant and total.

Stop it happening again

  • Keep push protection on. For pushes to public repositories, GitHub's push protection for users is on by default and blocks known key formats before they land. If it stops you, don't click through with "I'll fix it later": that creates an open alert and publishes the key.
  • Read your secret scanning alerts. Secret scanning runs free on public repos. Alerts for keys from providers in GitHub's partner program (Stripe, OpenAI, Anthropic, Supabase and many more) go straight to the provider rather than to your repo's alerts, which is how those automatic notices and revocations happen.
  • Ignore env files from day one. .env* in .gitignore before the first commit.
  • Cap the blast radius. Use restricted Stripe keys, a hard monthly limit on AI keys, and expiry dates on keys that support them.
  • Scan before you push. A pre-commit hook that checks for keys catches what push protection doesn't know about, including private repos.

Where Corvis fits

Corvis checks every push to the repos you connect for leaked keys and tells you on Telegram, with the rotation link and checklist for that provider. With your OK it opens a pull request that moves the key into an environment variable. It can't see inside your Stripe or AWS account, so it never claims the old key is dead: you mark the incident resolved once you've rotated it.

To stop keys before they're committed at all, add the Corvis pre-commit hook with npx -y @usecorvis/cli hook install. It runs on your machine and blocks a commit that adds a secret key; the same checks run inside Cursor, Claude Code and Windsurf through npx -y @usecorvis/cli mcp.

Want to know whether a public repo has a key in it right now? The free repo scan tells you, no sign-up needed.

Sources

Checked against these pages on 24 October 2026. Providers change their dashboards; if a step looks different, the source has the current version.

  1. GitHub Docs: Removing sensitive data from a repository (opens in a new tab)
  2. GitHub Docs: About secret scanning (opens in a new tab)
  3. GitHub Docs: About push protection (opens in a new tab)
  4. Stripe Docs: API keys (rotating keys) (opens in a new tab)
  5. Stripe Docs: Best practices for managing secret API keys (opens in a new tab)
  6. OpenAI: Safety best practices (revoke compromised keys) (opens in a new tab)
  7. OpenAI: Spend limits (opens in a new tab)
  8. Anthropic: API key best practices (opens in a new tab)
  9. Supabase Docs: Understanding API keys (rotate a leaked key) (opens in a new tab)
  10. Supabase: Security retro 2025 (automatic revocation of secret keys) (opens in a new tab)
  11. AWS Docs: Update (rotate) access keys (opens in a new tab)
  12. AWS Docs: AWSCompromisedKeyQuarantineV3 (opens in a new tab)
  13. Vercel Docs: Rotating secrets (opens in a new tab)
  14. Palo Alto Networks Unit 42: EleKtra-Leak (exposed IAM keys used within five minutes) (opens in a new tab)

Is your repo leaking right now?

The free scan checks a public GitHub repo for leaked keys and risky code, and grades it from A to F.

Scan a repo free