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.
๐ก The short version: Supabase has a public key for browsers and a secret key for servers. The secret one (the older
service_rolekey, or the newersb_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 olderanonkey. - Postgres role:
anonorauthenticated. - Row level security: applies.
- Safe in the browser? Yes.
๐ซ Secret key
- Names: secret key (
sb_secret_), or the olderservice_rolekey. - Postgres role:
service_role, which has theBYPASSRLSattribute. - 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_orNEXT_PUBLIC_. - A
.envfile 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
- 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. - Move the new key to the server and read it from an environment variable, in an edge function or API route.
- Redeploy so the old bundle stops being served.
- 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.