Supabase service_role key exposed: what to do now
Short answer
The service_role key bypasses every Row Level Security policy, so anyone who has it has full read and write access to your database. Rotate the key immediately, then change your code so the key is only used on the server. Do the rotation first and the investigation second.
Why you got this
The service_role key is meant for trusted server code. It leaks when it is put in a front-end environment variable such as one starting with NEXT_PUBLIC_ or VITE_, pasted into a client file by an AI assistant, committed to a public repository or shipped in a mobile app.
The anon key is different and is safe to expose, but only if RLS is correct. People often mix the two up, so check which one was exposed. You can paste the JWT into a decoder and look at the role claim: service_role is the dangerous one.
What to do now
Rotate the key now, in the Supabase dashboard under Project Settings. If the leaked key is a legacy service_role JWT (starts with eyJ), creating a new sb_secret_ key does not revoke it: rotate the legacy JWT secret, or move to the new API keys and disable the legacy keys. Rotating the JWT secret also changes the anon key and signs out all users. If the leaked key is an sb_secret_ key, create a new secret key, deploy it, then delete the leaked one.
Update the new key in your server environment variables and redeploy. Remove it from the front end and from git; deleting a file is not enough if the key is in history.
Check logs in the dashboard for unexpected API, auth or storage activity from the time the key was exposed. Look for new admin users, changed rows and downloaded files.
If personal data was reachable, record the exposure window and decide whether you must notify users.
How to fix it properly
Use the key only in server code that runs on your own backend, such as an API route, an edge function or a server action. The variable must not start with NEXT_PUBLIC_ or VITE_, because those prefixes are copied into the browser bundle.
// app/api/admin/route.ts (server only)
import { createClient } from '@supabase/supabase-js';
const admin = createClient(
process.env.SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY! // never NEXT_PUBLIC_
);In the browser, use only the anon (publishable) key together with correct RLS policies. If a feature really needs admin power, put it behind a server endpoint that checks who the caller is and what they may do before it uses the key.
Turn on secret scanning for your repository, and add the .env file to .gitignore. Keep separate keys for development and production so a leak in one place is easier to contain.
How to check it is fixed
Search your built output for the old key and for the prefix eyJ, which every JWT starts with, and for sb_secret_, the prefix of the new secret keys, then decode any JWT hit and read its role. Open your site, view its JavaScript sources and confirm the new key is not there either.
The free ScanAnt scan looks for service_role keys in the JavaScript your site ships. It decodes only the role claim, shows the key masked, never uses it and never stores it whole. It cannot see your git history or private server code, so rotating the key remains the real fix.
Questions
Do I need to rotate the anon key too?
Only if you rotate the JWT secret, which changes both. The anon key itself is public, but rotating it is harmless once you update your apps.
I deleted the commit. Is it safe now?
No. Assume anyone could have copied the key within minutes of it being public. Rotate it regardless.
How do I know if someone used it?
Check the Supabase logs for API, auth and storage requests and look for rows or users you did not create. Absence of evidence is not proof, so rotation matters more.
Can I keep the key in a Vite or Next.js env file?
Yes, but only without the public prefix, and only read it in server code. Anything with NEXT_PUBLIC_ or VITE_ ends up in the browser.
Related checks
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 →