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

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

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:

5. Prevent a repeat

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