Supabase Service Role Key Leaks: Why It Skips Every Rule

The Supabase service_role key ignores row level security entirely. See how it ends up in a frontend bundle, how to check yours safely and what to do the moment it leaks.

Dr. Security3 MIN READ

๐Ÿ’ก The short version: Supabase has a public key for browsers and a secret key for servers. The secret one (the older service_role key, or the newer sb_secret_ key) bypasses row level security completely. If it is in your frontend, your database rules no longer matter. Treat it as an emergency and rotate it.

Row level security is the lock on your Supabase tables. The service role key is a master key that walks through every lock without noticing it's there. ๐Ÿ—๏ธ That is exactly what it's for, on a server you control, and exactly why it must never travel to a browser.

๐Ÿ”‘ Which key is which

โœ… Public key

  • Names: publishable key (sb_publishable_), or the older anon key.
  • Postgres role: anon or authenticated.
  • Row level security: applies.
  • Safe in the browser? Yes.

๐Ÿšซ Secret key

  • Names: secret key (sb_secret_), or the older service_role key.
  • Postgres role: service_role, which has the BYPASSRLS attribute.
  • Row level security: skipped.
  • Safe in the browser? Never, not even on localhost.

That last line comes straight from Supabase's docs. They also note that they block secret keys in browsers by checking the request's User-Agent and answering 401, and then say plainly that this doesn't make a leaked key safe to keep using. Supabase also plans to retire the older anon and service_role keys by the end of 2026, so you may see both styles for a while.

๐Ÿ˜ฌ How it ends up in the frontend

  • Someone (or an AI assistant) hits a permissions error, finds that the secret key makes it vanish and uses that key in client code.
  • The key is stored under a browser-exposed variable name such as VITE_ or NEXT_PUBLIC_.
  • A .env file is deployed with the site.

The error went away. So did every protection you had. Fix permissions errors with a proper policy instead. Our RLS guide shows how.

๐Ÿ•ต๏ธ Check your site without pasting secrets anywhere

Search the JavaScript your site loads for sb_secret_, and for the word service_role. The older keys are tokens made of three parts separated by dots. The middle part holds the role. You can read it locally in your browser console, without sending it to any website:

JSON.parse(atob("PASTE_THE_MIDDLE_PART".replace(/-/g,"+").replace(/_/g,"/"))).role

If it prints service_role, it's the secret key. If it prints anon, it's the public one and that is fine. Please don't paste a real key into an online decoder.

๐Ÿšจ If you find it

  1. Create a replacement and revoke the leaked key in your project's API key settings. Follow Supabase's rotation guide, since the exact steps differ between the new sb_secret_ keys and the older JWT keys.
  2. Move the new key to the server and read it from an environment variable, in an edge function or API route.
  3. Redeploy so the old bundle stops being served.
  4. Look at your data. Check logs and recent changes for anything you don't recognise, and if personal data could have been read, find out what you must tell people where they live.

Removing the key from your code isn't enough. Anyone who already copied it still has it.

๐Ÿค– How a scan helps

Dr. Security looks for the key in your public JavaScript, and it is careful about it. The public anon key is never reported. A Supabase token is flagged only when its role is service_role, and the key is always redacted in the result. More on finding leaked keys in your frontend.

๐Ÿš€ Check yours in a couple of clicks. Run a free scan and see whether a secret key is in your public code.

  • supabase service role key
  • service_role key leak
  • supabase secret key
  • supabase security
  • rotate supabase key

FIND OUT WHAT YOU'RE EXPOSING.