Guide
Is your Supabase service_role key exposed? How to check and what to do
Short answer: open your live site, open DevTools, search every loaded file for sb_secret_, then for eyJ, and decode anything that starts with eyJ to see its role. "role":"anon" is the public key and is fine. "role":"service_role", or any sb_secret_... string, means a key that bypasses all your Row Level Security is sitting in code every visitor downloads. If you find one, rotate it first and clean up after. Deleting it from your code does not make it safe, because copies are already out there.
This is a check for your own app, and takes about ten minutes.
Why this key matters so much
Supabase gives your project two kinds of key.¹
- The publishable key (
sb_publishable_..., or the legacyanonkey) is meant to ship in web pages and mobile apps. It runs as theanonrole when nobody is signed in, and asauthenticatedwhen someone is. Row Level Security decides what it can reach. - The secret key (
sb_secret_..., or the legacyservice_rolekey) is for servers. It runs as theservice_rolePostgres role, which has theBYPASSRLSattribute, so none of your policies apply to it.¹ ²
That's why a leaked secret key is a different order of problem from a leaked publishable key. You can have perfect RLS and still lose everything. Supabase says a secret key must never be in a browser, a shipped app or source control, "even on localhost".¹
How it ends up in the browser
Almost always the same way. A builder or an AI assistant needs a key in the front end, the variable comes back undefined, and the quick fix is to add a prefix. Vite only exposes variables that start with VITE_, and Next.js only exposes NEXT_PUBLIC_ ones, and both copy the value into the JavaScript at build time.³ ⁴ So VITE_SUPABASE_SERVICE_ROLE_KEY "works" immediately, by publishing the key. Our env vars guide covers that mechanism in full.
Other routes: a key pasted into a component, a .env file committed to git, or server code imported into a client component.
Step 1: Search what your live site ships (5 minutes)
Check the deployed site, not your source folder. The deployed files are what a stranger can read.
- Open your live site in Chrome and press F12.
- Click through the app first (sign in, open the dashboard, visit a few pages). Many apps load code in pieces as you navigate, and DevTools only searches files that have actually loaded.
- Open the Sources tab, then press Ctrl+Shift+F (Cmd+Option+F on a Mac) to search across all loaded files.
- Search for each of these in turn:
sb_secret_,service_role,eyJ.
What the results mean:
| You find | What it means |
|---|---|
sb_secret_ followed by a long random string |
A new-style secret key. Any hit in browser code is a leak. |
service_role as a variable name or comment |
Worth reading around it. The name alone isn't a key, but it tells you where to look. |
Long strings starting eyJ |
JWTs. Your legacy anon key looks like this too, so decode each one (below). |
A legacy service_role key is a JWT, and the words service_role are encoded inside it, so searching for service_role alone will not find the key itself. That's why you search for eyJ as well.
Step 2: Decode any eyJ key to see which one it is
A JWT has three parts separated by dots. The middle part is just encoded text, not encrypted, and it names the role. Don't paste a key you suspect is secret into an online decoder. That hands it to a third party. Decode it locally, in the Console tab of the same DevTools window:
const key = 'PASTE_THE_KEY_HERE';
JSON.parse(atob(key.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')));
You'll get an object like { iss: "supabase", ref: "...", role: "anon", ... }.
role: "anon"is the public key. It's supposed to be there.role: "service_role"is the problem.
Step 3: Check git history
# From your project folder: has a secret key ever been committed?
git log --all --oneline -S "sb_secret_"
git log --all --oneline -- .env .env.local .env.production
If the repository is public, or ever was, treat anything those commits contain as published. Also check your host's environment variables for a secret stored under a VITE_ or NEXT_PUBLIC_ name.
If you found one: rotate first
Treat the key as already used, because scanners crawl public JavaScript. Supabase's order is: fix the cause of the leak, create a new key, move everything to it, then retire the old one.⁵
- Fix the cause. Remove the key from the front end and any
VITE_orNEXT_PUBLIC_variable. Move whatever needed it into a server route or a Supabase Edge Function that reads it from a server-side secret. Otherwise the new key leaks the same way. - Create a new secret key in your project's Settings, under API Keys.
- Replace the old key everywhere it's used: servers, Edge Functions, cron jobs, webhooks. If an Edge Function checks callers with
withSupabase({ auth: 'secret' }), it accepts only the key nameddefault, so update it to accept the new key by name.⁵ - Retire the old key. For a secret key, delete it. For a legacy
service_rolekey, deactivate the legacy keys. That also switches off the legacyanonkey, so any browser or mobile client still using it needs a publishable key first.⁵ Deleting a secret key can't be undone, so wait until everything has moved over, unless someone is actively abusing it. In that case retire it immediately and accept that components still using it will fail until you swap them. - For a leaked legacy
service_rolekey, also revoke the legacy JWT secret. The legacy keys are JWTs signed by that secret, so anything that still trusts it can accept the leaked key. Supabase's JWT signing keys guide covers the move.⁵ ⁶
"The new secret keys don't work in a browser, so I'm fine"
No. Supabase's new secret keys are rejected when a request looks like it came from a browser, and that returns HTTP 401. But Supabase's own documentation notes that an attacker can still use the key from other tools.² A secret key in your bundle is a full leak even if your app errors when it tries to use it.
After rotating: look for damage
A secret key can read, write and delete anything, and reach admin functions for user accounts.¹ So after rotating:
- Look for unfamiliar rows, missing rows and new accounts in your main tables and your Auth users list.
- Check Storage for files you didn't upload.
- If you have backups and something was changed or deleted, you may need to restore. How far back depends on your plan, so check before you need it.
- If real people's data was readable or changed, UK GDPR may require you to report it. A personal data breach that's likely to risk people's rights has to be reported to the ICO as soon as possible and, where feasible, within 72 hours of you becoming aware of it.⁷
Prevent a repeat
- Never put a secret in a variable with a public prefix. If a tool tells you to, that's the signal to move the work server-side.
- Use a separate secret key per backend component, so one leak means rotating one key.²
- A free outside-in scan is a useful second pair of eyes. Quick Check fetches your page and up to six of the scripts it links to, and looks for
sb_secret_keys and legacy JWTs whose role isservice_role. It's a look-only, GET-only scan of what the page loads. It doesn't see code that only loads on other routes, and it doesn't test your database or Row Level Security.
For the database side, work through the Supabase RLS launch checklist. If you also want to understand why a publishable key being visible is normal, see Is your Supabase anon key safe to expose? If you're rotating a legacy service_role key anyway, it's a good moment to move the whole app to publishable and secret keys.
FAQ
Does Row Level Security protect me if my service_role key leaks?
No. The service_role Postgres role has the BYPASSRLS attribute, so policies are never applied to a secret key. Rotate the key.
I found a key starting with eyJ. Is that bad?
Not necessarily. Legacy anon keys start with eyJ too. Decode the middle part locally and read the role: anon is expected, service_role is a leak.
Can I just delete the key from my code and redeploy? That's necessary but not enough. Everyone who loaded the old build, and anyone who cloned the repo, may have a copy. The old key has to stop working, which means rotating it.
Is a leaked publishable (anon) key the same emergency? No. Supabase designs that key to be public, and what it can reach depends on your RLS policies and grants.¹
Want a second pair of eyes? The $49 Launch Check looks for exposed keys on your live URL and, with your permission, checks whether anonymous visitors can read your tables. We never need your passwords or keys.
Sources
- Supabase docs, API keys (publishable vs secret keys, Postgres roles,
BYPASSRLS, never in a browser or source control, "even on localhost"): https://supabase.com/docs/guides/api/api-keys - Supabase docs, API keys: Handling secret keys safely (browser block returns 401 but the key can still be used from other tools; separate key per component): https://supabase.com/docs/guides/api/api-keys
- Vite docs, Env Variables and Modes (
VITE_prefix, values bundled at build time): https://vite.dev/guide/env-and-mode - Next.js docs, Environment Variables (
NEXT_PUBLIC_inlined at build time): https://nextjs.org/docs/app/guides/environment-variables - Supabase docs, Rotate a leaked API key (order of steps, new key by name, deactivating legacy keys, revoking the legacy JWT secret): https://supabase.com/docs/guides/security/rotate-leaked-api-keys
- Supabase docs, JWT signing keys: https://supabase.com/docs/guides/auth/jwt-signing-keys
- ICO, Personal data breaches: a guide (notify the ICO as soon as possible and, where feasible, within 72 hours if a risk is likely): https://ico.org.uk/for-organisations/report-a-breach/personal-data-breach/