Skip to content
ScanAnt

I leaked an API key on GitHub: what do I do?

Short answer

Revoke or rotate the key at the provider right now; deleting the commit does not help, because bots copy keys from public repositories within minutes. After that, remove the key from your git history, move secrets to environment variables and turn on push protection. Check the provider's usage logs for anything you did not do.

Why you got this

Automated scrapers watch the stream of public GitHub events and test every string that looks like a key. A key pushed to a public repository, even for a minute, should be treated as taken. Private repositories are safer but not safe: forks, collaborators and a later switch to public all expose old commits.

It happens most often when a .env file is committed by accident, when a key is pasted into source code for a quick test or when an AI assistant writes a real key into a config file. Git remembers every commit, so deleting the line later leaves the key in history.

What to do now

  1. Revoke or roll the key in the provider's dashboard, and create a new one. Do this before anything else; it is the only step that actually stops misuse.

  2. Open the provider's usage or billing page and look for requests, charges or resources you did not create. Set spending limits while you are there.

  3. Put the new key in an environment variable or your host's secret store, never in the code, and redeploy.

  4. Then clean the repository, covered below. Rotating comes first because history cleanup does not un-leak anything that was already copied.

How to fix it properly

Stop tracking the file and ignore it in future. If the key lived in .env, make sure the file is ignored and keep a .env.example with empty values.

bash
git rm --cached .env
echo ".env" >> .gitignore
git add .gitignore
git commit -m "Stop tracking .env"

Remove the key from history with git filter-repo (install it first). Put the old key and a replacement in a text file, run the rewrite and force-push. Tell collaborators to re-clone. GitHub may still serve old commits by SHA or through pull requests and forks; contact GitHub Support to purge cached views.

bash
# replacements.txt contains one line: OLD_KEY_VALUE==>REMOVED
git filter-repo --replace-text replacements.txt
# filter-repo removes the remote, so add it back
git remote add origin <your-repo-url>
git push --force --all && git push --force --tags

In the repository settings under Code security, enable secret scanning and push protection, which blocks a push that contains a known key format. Add a pre-commit scanner such as gitleaks to catch mistakes on your own machine.

Use separate keys per environment and the narrowest permissions the provider offers, so a future leak costs less.

How to check it is fixed

Search the whole history for the old key with git log --all -S followed by the key value, and search your deployed site's JavaScript too, since many leaks come from a key bundled into the front end rather than the repository.

The free ScanAnt scan reads the JavaScript your site publishes and reports payment, AI, cloud, e-mail and database keys it finds, shown masked and never stored whole. It cannot see your private repository, so use GitHub secret scanning for that and treat the scan as a check of what is live.

Questions

I deleted the commit within a minute. Am I safe?

Not necessarily. Scrapers react quickly and GitHub keeps events. Rotate the key anyway; it costs minutes and removes the doubt.

Does making the repository private help?

It stops new copies but does not undo existing ones. Rotating is the fix.

Will GitHub tell me?

Secret scanning partners can be notified when a key matches a known format, and some providers revoke it for you. Do not rely on it; check yourself.

Is a Firebase or Supabase anon key a leak?

No, those are public by design. Secret keys, service_role keys and payment or AI keys are the ones to rotate.

Check your site after the fix

The quick scan is free for any public site and needs no signup. Checks that touch your database or files run only after you prove you own the site.

Choose a scan level →