Short answer
Secret rotation is replacing a credential, such as an API key, webhook secret or database password, with a new one and then retiring the old one. Rotate immediately when a secret may have leaked, and on a schedule for long-lived keys. The safe order is: fix the cause, create the new secret, move every component to it, then retire the old one.
Ask an AI about this page:ChatGPTClaude
On this page
Secret rotation is the practice of replacing a credential, such as an API key, a webhook signing secret or a database password, with a new one and then retiring the old one so it stops working. It limits how long a leaked secret is useful. OWASP’s secrets management guidance says rotation must be triggered immediately when a compromise is suspected, and recommends automating it.
Secret rotation in an AI-built app
A typical AI-built app holds a handful of secrets: the Supabase secret or service role key, an OpenAI or Anthropic key, a Stripe secret key and the webhook signing secret. They leak in predictable ways: a key in a VITE_ environment variable, a .env file pushed to GitHub, a key pasted into a chat with an AI tool, a function that logs its configuration.
Removing the key from the code feels like the fix, but it isn’t. Until the provider stops accepting the old key, anyone who copied it can use it. And rotating in the wrong order takes your own app down. Supabase’s leak guide gives an order that avoids both problems:
- Fix the cause first, or the new key leaks the same way.
- Create a new secret in the provider’s dashboard.
- Move every component (edge functions, servers, cron jobs, webhooks) to the new secret and confirm each one works.
- Retire the old secret. If someone is actively abusing it, retire it at once, even if something breaks.
Providers help with step 3. Stripe lets you keep an old webhook signing secret active for up to seven days after rolling it, signing each event with both. Supabase recommends one secret key per back-end component, so a leak means rotating one key, not all of them. Note that deleting a Supabase secret key cannot be undone.
How to check your app
- Make a list of every secret your app uses, where it is stored and which components read it. If you can’t write that list, you can’t rotate quickly.
- Search your repository, including git history, for key prefixes such as
sk_live,sk_test,sb_secret_andwhsec_. - Check your provider dashboards for key age and last use, and look at request logs for traffic you don’t recognize.
How to prevent problems
Keep secrets only in server-side secret stores, give each component its own key, and practise one rotation before you need it. If a key has already leaked, start with Leaked service_role key: the first 60 minutes or, for keys found in commits, Secrets in git history.
Frequently asked questions
Is deleting the key from my code enough after a leak?
No. Anyone who copied it can still use it until the provider stops accepting it. Removing it from code or git history does not revoke it; rotating does.
Do I need to rotate my Supabase anon or publishable key?
Normally not: it is public by design. Supabase’s leak guide is about secret and service_role keys. One exception: retiring a leaked legacy service_role key also deactivates the legacy anon key, so move browser clients to a publishable key first.
How often should I rotate keys that have not leaked?
There is no single number. OWASP recommends basing the interval on risk rather than a fixed calendar, and automating it where you can. Stripe recommends a regular rotation process so your team can replace a key at short notice.
Sources
- 1.Secrets Management Cheat Sheet — OWASP Cheat Sheet Series
- 2.Rotate a leaked API key — Supabase Docs
- 3.Best practices for managing secret API keys — Stripe Docs
- 4.Receive Stripe events in your webhook endpoint — Stripe Docs