Short answer
A leaked Supabase secret key (sb_secret_…) or legacy service_role key bypasses row level security, so treat it as full access to your data. First close the hole it leaked from, then create a new secret key, switch every server component to it and retire the old key: delete a secret key, or disable the legacy keys and later revoke the legacy JWT secret. Then search your database for pasted keys, check your logs while they are still kept, and only rewrite git history if you really need to.
Ask an AI about this page:ChatGPTClaude
On this page
Key takeaways
- A secret or
service_rolekey bypasses row level security. Assume whoever has it can read and change every row and every file. - Close the leak first, then rotate. A new key in the same frontend bundle leaks again on the next deploy.
- New secret keys (
sb_secret_…) are retired by deleting them. Legacy JWT keys are retired by disabling the legacy keys and later revoking the legacy JWT secret. - Keys pasted into the database (webhooks, cron jobs, functions) keep working until you replace them. Three queries find them.
- Logs on the Free plan are kept for one day. Look before they are gone.
What a leaked secret key can do
Supabase has two kinds of keys. The publishable key (sb_publishable_…, or the legacy anon key) is meant to be public; your policies decide what it can reach. The secret key (sb_secret_…, or the legacy service_role key) is the opposite: it runs as a role that bypasses row level security, and Supabase’s Storage docs say service keys get unrestricted access to all Storage APIs.
So a leaked service_role key is not “a config value in the wrong place”. It is the master key to your data, usable from any computer, without your app. Supabase’s API keys guide is explicit that rotating matters because an attacker can still use the key from other tools, even after you remove it from your code.
The most common ways it leaks in AI-built apps: it was put into a VITE_ variable and shipped in the JavaScript bundle (see secret in a VITE_ variable), it was pasted into frontend code to “fix” a permission error (see service_role key in the frontend), or it was committed to a public repository.
The first 60 minutes
The times are a guide for a small app with one person on it. If you can see that someone is actively using the key right now, skip ahead to step 4 and retire it immediately: Supabase’s guide says to do that even though components still using the old key will stop working until you switch them.
Minutes 0–5: Confirm which key leaked and where
Compare the leaked value with Settings → API Keys in your Supabase dashboard. A string starting with sb_secret_ is a new secret key; a long string starting with eyJ is a legacy JWT key (anon or service_role). A leaked anon or publishable key is not an emergency, because it is public by design.
Write down where you found it: the live website, a public repo, a chat, a screenshot, a log. That decides step 2 and step 6.
Minutes 5–15: Close the hole it came out of
Supabase’s rotation guide says to fix the root cause before rotating. If the key is in your frontend, the new key would ship the same way. Remove it from browser code and from every VITE_ variable, move the work that needed it into an Edge Function or another server, and deploy. Supabase refuses new secret keys in browsers (it checks the User-Agent and answers 401), but that block does not apply to the legacy service_role key, and it does nothing against someone who copied the key into their own script.
Minutes 15–25: Create a new secret key and switch every server component
In Settings → API Keys, create a new secret key. Replace the old key in every place that runs on a server: Edge Function secrets, your backend’s environment variables, CI jobs, scripts. Supabase recommends one secret key per component, so the next leak means rotating only one.
Don’t forget keys that live inside the database. Step 3a below finds them.
Minutes 25–35: Retire the leaked key
New secret key (sb_secret_…): delete it in Settings → API Keys. Deleting a secret key can’t be undone, so first confirm every component uses the new one.
Legacy service_role key: first replace the legacy anon key with a publishable key in your browser and mobile apps, because disabling the legacy keys turns off both anon and service_role. Then disable the legacy keys. Supabase warns: never re-enable them afterwards, because that brings the leaked key back. Finally, revoke the legacy JWT secret, since both legacy keys are JWTs signed by it. Follow the JWT signing keys guide: move the legacy secret to “previously used”, wait at least the access token lifetime plus a margin (Supabase’s example: 1 hour 15 minutes for a 1 hour token expiry), then revoke it.
Minutes 35–50: Check the logs while they still exist
Open Logs in the dashboard. Look at API Gateway, Postgres, Auth and Storage logs for the period since the key leaked: unusual IP addresses, bulk reads of tables your app never lists, deletes, new users or changes you didn’t make. Logs are kept for 1 day on Free, 7 days on Pro, 28 on Team and 90 on Enterprise, so export what you need now.
Minutes 50–60: Clean up the copies
Remove the key from every place it was written down: .env files in shared folders, CI logs, chat messages, tickets, screenshots. For git history, read the section below before you rewrite anything.
Step 3a: find keys pasted into your database
Database webhooks, pg_net calls and pg_cron jobs often carry a key in a header, and AI tools sometimes hard-code one into a function. These keep working with the old key until you change them, and Supabase’s rotation guide specifically mentions webhooks that send the legacy service_role key. Run these in the SQL Editor. eyJhbGci is how every JWT starts, so the queries also catch the harmless anon key; check each hit.
select n.nspname as schema, p.proname as function_name
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname not in ('pg_catalog', 'information_schema')
and (p.prosrc like '%eyJhbGci%' or p.prosrc like '%sb_secret_%');select t.tgname as trigger_name, t.tgrelid::regclass as on_table
from pg_trigger t
where not t.tgisinternal
and (encode(t.tgargs, 'escape') like '%eyJhbGci%'
or encode(t.tgargs, 'escape') like '%sb_secret_%');select jobid, jobname
from cron.job
where command like '%eyJhbGci%' or command like '%sb_secret_%';Replace each key you find with the new secret key. Better still, keep the key out of SQL entirely, for example by having the job call an Edge Function that reads its own secret. If a webhook sends the legacy key as Authorization: Bearer, Supabase’s guide says to send the new secret key in the apikey header instead.
Should you rewrite git history?
Usually not, and not first. GitHub’s guide on removing sensitive data says to revoke or rotate a secret first, and that once it can no longer grant access you generally don’t need to rewrite history. Rewriting doesn’t reach other people’s clones or forks, and cached views keep old commits reachable by hash.
Rewriting with git filter-repo and a force push also has costs: every commit hash after the leak changes, collaborators can lose work or push the old data back, and tools that sync with your repository see a different history. If your project is connected to an AI builder that syncs with the branch, check how it handles force pushes before you do one. Our guide on secrets in git history walks through finding old secrets and deciding whether a rewrite is worth it.
After the first hour
Once the key is dead, decide what the leak exposed. If logs show reads of personal data, or you can’t rule them out because the logs are gone, you may have a personal data breach. Under the GDPR (Art. 33) that has to be reported to the supervisory authority within 72 hours of becoming aware of it, unless it is unlikely to result in a risk to people. Get advice on your own case.
Then make the next leak smaller: one secret key per component, no secret in any VITE_ variable, a secret scanner on your repository, and the habit of asking “does this need the secret key, or should a policy allow it?” before you reach for it.
A fix prompt for your AI builder
Use this after you rotated the key, to find the places that made the leak possible.
Search this whole repository for Supabase secret keys: strings starting with sb_secret_, any JWT starting with eyJ that is used as a service_role key, and environment variables named like SERVICE_ROLE, SECRET_KEY or SUPABASE_SECRET. List every file and line.
Remove any secret key from frontend code and from every VITE_ variable. Where frontend code used it to bypass a permission error, move that operation into a Supabase Edge Function that reads the secret from its environment and checks the signed-in user before acting, or fix the RLS policy instead. The browser may only use the publishable (anon) key.
Do not write any key value into code, migrations or SQL. Do not rewrite git history. Show me the list and the planned changes before editing.
Frequently asked questions
Is deleting the commit enough when I pushed my service_role key to GitHub?
No. Anyone may already have copied it, and forks, clones and cached views keep the old commit. The fix is to rotate: create a new secret key and retire the leaked one. GitHub’s own guidance is to revoke or rotate first; once the key no longer works, rewriting history is usually not necessary.
Will my users be logged out when I rotate the key?
Swapping API keys does not end user sessions by itself. Revoking the legacy JWT secret is the step that can: Supabase says to move it to previously used first and wait at least the access token lifetime plus a margin (1 hour 15 minutes for a 1 hour expiry) before revoking it, so existing sessions can expire normally.
How do I know if my key is a legacy service_role key or a new secret key?
New secret keys are short strings that start with sb_secret_. Legacy keys are long JWTs that start with eyJ. Compare the leaked value with the keys listed under Settings → API Keys in your Supabase dashboard; don’t paste a secret into an online JWT decoder.
Do I have to tell my users about the leak?
It depends on what the key exposed and where you operate. Under the GDPR, a personal data breach has to be reported to the supervisory authority within 72 hours of becoming aware of it, unless it is unlikely to result in a risk to people. Get legal advice if personal data may have been read.
Sources
- 1.Rotate a leaked or compromised API key — Supabase Docs
- 2.Understanding API keys — Supabase Docs
- 3.JWT signing keys — Supabase Docs
- 4.Logging — Supabase Docs
- 5.Removing sensitive data from a repository — GitHub Docs
- 6.Art. 33 GDPR: Notification of a personal data breach — gdpr-info.eu