CCyberwiki

Responding to a leaked API key

An API key in a public repository, in JavaScript sent to the browser or in a downloadable .env file is compromised from the moment it appears there. Automated scanners find public secrets within minutes, and attackers can use them within hours. Removing the key in a new commit, or deleting and recreating the repository, does not stop anyone who already copied it. The first job is to revoke the key with the provider that issued it.1

Revoke now or replace first

Revoke at once when the key is still active, was in a public repository or is a production key. When downtime is the bigger worry, move the application to a new key with the same permissions first, then revoke the old one.1 If the provider's logs show someone using the key, retire it right away, even though the parts of the application that use it stop working until the new key is in place.2

Fix the cause of the leak first, or the new key can leak the same way.2 A key in a Next.js variable whose name starts with NEXT_PUBLIC_ is copied into the JavaScript sent to every browser,3 so a new key saved under the same name ends up there again.

Revoke the key at the provider

Stripe and AWS show a new secret key only once, so copy it into the hosting settings before leaving the page.45

Table 1. Where to replace a leaked key
ProviderSteps
StripeAPI keys page, the key's overflow menu, Rotate key. Under Expiration, Now deletes the old key at once; with a later time, both keys keep working for up to 7 days.4
OpenAIRotate the key on the API Keys page, platform.openai.com/api-keys.6
SupabaseSettings, API Keys: create a new secret key, replace the old one everywhere, then delete the old one. Deleting cannot be undone.2
AWSIAM, Users, the user, Security credentials, Access keys, Create access key. After the switch, Actions, Deactivate on the old key, and later Actions, Delete.5

GitHub revokes its own personal access tokens found in public repositories, and reports other key formats it recognises to their provider, which may revoke them.1

Supabase legacy keys

For a leaked legacy service_role key, deactivate the legacy keys and revoke the legacy JWT secret that signed them, since anything that still trusts that secret accepts the leaked key. Browser and mobile clients then need a publishable key in place of the legacy anon key.2 Like the newer secret keys, the service_role key bypasses every row level security policy, so a leaked one exposes all of the project's data.7 The Management API commands need a personal access token. Run them on your own computer, because the first one prints the new key:2

export PROJECT_REF="your-project-ref"
export SUPABASE_ACCESS_TOKEN="your-personal-access-token"

# create a new secret key and print it
curl --fail-with-body -X POST "https://api.supabase.com/v1/projects/$PROJECT_REF/api-keys?reveal=true" \
  -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"type": "secret", "name": "rotated_secret_key"}' | jq -er '.api_key'

# list the keys to find the id of the leaked one
curl "https://api.supabase.com/v1/projects/$PROJECT_REF/api-keys" \
  -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" | jq '.[] | {id, name, type}'

# delete the leaked secret key
export KEY_ID="id-of-the-compromised-key"
curl --fail -X DELETE "https://api.supabase.com/v1/projects/$PROJECT_REF/api-keys/$KEY_ID" \
  -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN"

# or, for a leaked legacy key: turn off the legacy anon and service_role keys
curl --fail -X PUT "https://api.supabase.com/v1/projects/$PROJECT_REF/api-keys/legacy?enabled=false" \
  -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN"

AWS access keys

On AWS, check the old key's Last used value before deactivating it, and delete it only once the application works with the new key. The commands for the AWS CLI, its command line tool, are aws iam create-access-key, aws iam get-access-key-last-used, aws iam update-access-key and aws iam delete-access-key.5 When there are signs of unauthorized activity, look for the AWSCompromisedKeyQuarantineV2 policy on each IAM user (an identity inside the AWS account), delete root user access keys you did not create and deactivate IAM users you did not create.8

Deploy the new key

Vercel applies a changed environment variable only to new deployments, so redeploy after saving the new key.9 A NEXT_PUBLIC_ value is fixed at build time and needs a new build.3 Restarting the application does not revoke the old key.10 Other copies turn up in a code search for org:YOUR-ORG "SECRET-STRING" and in the repository's Settings, under Security, Secrets and variables and Deploy keys.1

Find out whether the key was used

Read the provider's logs for the whole time the key was exposed,1 looking for who had access, when they used it and when the key was last rotated.10 Each Stripe key's overflow menu has View request logs.4 OpenAI's Usage page, platform.openai.com/usage, shows whether usage matches the team's own work.6 On AWS, read the CloudTrail Event history and review the charges in Cost Explorer.8

The logs may not settle it. Toyota's T-Connect access key was public on GitHub from December 2017 to September 2022, and the server's access history could neither confirm nor rule out use by a third party.11 The full case is in Exposed credentials in source code repositories.

Trace the leak

Record the key's type and provider, the file and line where it sits, and who owns it. git log -S finds the commit that added it and its author.1

Clean the repository

Once the key is revoked, rewriting the history may not be warranted, and GitHub Support helps only when rotating the credential cannot reduce the risk. A rewrite changes commit hashes, breaks the diffs of closed pull requests and needs a force push that overwrites the copy on GitHub, and an old clone can push the key back.12 If the key has to go anyway, the tool is git-filter-repo 2.47 or later. These commands remove a file from every commit, or replace the strings listed in a file, then push the result:12

git-filter-repo --sensitive-data-removal --invert-paths --path PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA
git-filter-repo --sensitive-data-removal --replace-text ../passwords.txt
git push --force --mirror origin

The old commits may still be readable in forks, in clones and through their commit IDs (SHA-1 hashes) in cached views on GitHub.12 Close the secret scanning alert with Close as and the reason Revoked.1

Prevent the next leak

Turn on push protection in the repository, and Push protection for users in the personal account, so GitHub blocks a push that contains a key it recognises.1 A pre-commit scanner such as gitleaks checks for keys before a commit is made.12 Keys belong in a secrets vault such as AWS Secrets Manager or HashiCorp Vault, or in environment variables where the platform has no vault.13 On OpenAI, set a monthly spend limit with hard enforcement, since spend alerts alone do not stop API traffic, and give project keys an expiration date.6

Prompt for a coding agent

To have an AI coding agent trace a revoked key and move it out of the code, give it this prompt.

An API key for [SERVICE] has leaked and I am revoking it at the provider myself. Search this repository and its full git history for the key and the variable that holds it, and tell me the files, the commits and who committed it. Do not print the key. List where the key is set outside the code, such as CI secrets, hosting environment variables and deploy scripts. If browser code reads it, for example through a NEXT_PUBLIC_ variable, move that call to server-only code. Read the key only from an environment variable, add the env files to .gitignore, and tell me which deployments need a new build after I set the new value. Do not rewrite git history or force push.

In SecFlash

On the Security page, SecFlash reports a secret in the code or git history (SF-SEC-702), a password in the code (SF-SEC-709), a key in an AI agent's config file (SF-SEC-718), a workflow that can leak a credential (SF-SEC-805) and a readable .env file (SF-SEC-617). The Exposure page shows keys and tokens in public JavaScript (SF-EXP-301, SF-EXP-302) or in page HTML (SF-EXP-304, SF-EXP-305), a downloadable environment file (SF-EXP-307) and a readable git repository (SF-EXP-104). On the Cloud page, SF-CLD-604 reports secret scanning off on a public repository and SF-CLD-605 reports push protection off.

See also

References

  1. a b c d e f g h "Remediating a leaked secret in your repository". GitHub Docs. GitHub.
  2. a b c d e Supabase (updated 19 November 2026). "Rotate a leaked API key". Supabase Docs.
  3. a b "How to use environment variables in Next.js". Next.js Docs. Vercel.
  4. a b c "API keys". Stripe Documentation. Stripe.
  5. a b c "Update access keys". AWS Identity and Access Management User Guide. Amazon Web Services.
  6. a b c "Best practices for API key safety". OpenAI Help Center. OpenAI.
  7. back "API keys". Supabase Docs. Supabase.
  8. a b "Resolve issues with unauthorized activity in AWS accounts". AWS re:Post Knowledge Center. Amazon Web Services.
  9. back "Environment variables". Vercel Docs. Vercel.
  10. a b "Secrets Management Cheat Sheet". OWASP Cheat Sheet Series.
  11. back Toyota Motor Corporation (7 October 2022). [Notice on the possible leak of T-Connect customer email addresses] (in Japanese).
  12. a b c d "Removing sensitive data from a repository". GitHub Docs. GitHub.
  13. back "Best practices for managing secret API keys". Stripe Documentation. Stripe.