Supabase service role key exposed in your frontend: what to do right now
By AI App Medic · Updated · Supabase · Security · API keys
Supabase gives every project two kinds of keys. The public one (legacy anon or new sb_publishable_...) is designed for the browser and is limited by Row Level Security. The secret one (legacy service_role or new sb_secret_...) bypasses RLS completely. If the secret key is in your frontend bundle, anyone who opens DevTools has full read and write access to every table.
1. Confirm which key leaked
- Keys starting with
sb_secret_are secret.sb_publishable_keys are public. - Legacy keys start with
eyJ(they are JWTs). Decode the middle part and look at theroleclaim:"role": "service_role"means secret. - Search your deployed JavaScript (DevTools > Sources > search) and your Git history for the key. A key committed to a public repository is exposed even if the site never used it.
2. Contain it
If you use the new API keys: in the Supabase dashboard go to Project Settings > API Keys, create a new secret key for your server code, update your Edge Functions/backends to use it, then delete the leaked key.
If you use the legacy JWT keys: the cleanest path is to switch to the new publishable/secret keys, update your app and functions, and then disable the legacy keys. The alternative, rotating the project's JWT secret, invalidates both legacy keys and signs users out, so the frontend needs the new anon key immediately after.
Do this before you fix the code. Every hour the key stays valid is an hour someone can use it.
3. Check for damage
- Review tables for unexpected rows, deleted data or changed roles/admin flags.
- In Authentication > Users, look for accounts you do not recognise and unexpected changes to existing users.
- Check Storage for unexpected files or deletions.
- Look at your project's API and Postgres logs around the time the key was live.
- If personal data may have been accessed, you may have legal notification obligations depending on where you and your users are. Get proper advice.
4. Move privileged code to the server
Find every place the frontend used the secret key, usually a second createClient call for "admin" actions. Replace each with:
- A normal client using the publishable key plus correct RLS policies, if the action should be allowed for that user; or
- A Supabase Edge Function that verifies the caller's JWT, checks they are allowed to do the action, and only then uses the secret key (available to Edge Functions as an environment secret).
5. Prevent a repeat
- Never give the secret key a
VITE_orNEXT_PUBLIC_prefix. Those are inlined into the browser bundle. - Keep
.envfiles out of Git and tell your AI assistant explicitly: "never use the service role key in client code". - Re-scan the live site after every big AI-generated change.
FAQ
How do I know whether the key in my code is the anon key or the service role key?
New-style keys are labelled by prefix: sb_publishable_ is public, sb_secret_ is secret. Legacy keys are JWTs; decode the middle part (for example on jwt.io) and look at the role claim: anon is public, service_role is secret.
Does rotating the key log my users out?
Deleting a new-style secret key does not affect user sessions. Rotating the legacy JWT secret invalidates both legacy keys and existing user sessions, so plan it carefully.
Where should code that needs the service role key run?
Only on a server you control: Supabase Edge Functions, a serverless function, or a backend. Never in browser code.
Related guides
- Netlify build fails with "Secrets scanning found secrets in build": tell a public key from a real leak
- Supabase error: infinite recursion detected in policy for relation
- Supabase "permission denied for table" on a new table: add the missing GRANT
- RLS is enabled but your Supabase data is still public: how to audit your policies
- Supabase storage bucket public by mistake: close it and switch to signed URLs