Guide

Is your Supabase service_role key exposed? How to check and what to do

Last checked 25 Nov 2026 · 8 min read · by Ship Weekend

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.¹

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.

  1. Open your live site in Chrome and press F12.
  2. 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.
  3. Open the Sources tab, then press Ctrl+Shift+F (Cmd+Option+F on a Mac) to search across all loaded files.
  4. 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", ... }.

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.⁵

  1. Fix the cause. Remove the key from the front end and any VITE_ or NEXT_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.
  2. Create a new secret key in your project's Settings, under API Keys.
  3. 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 named default, so update it to accept the new key by name.⁵
  4. Retire the old key. For a secret key, delete it. For a legacy service_role key, deactivate the legacy keys. That also switches off the legacy anon key, 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.
  5. For a leaked legacy service_role key, 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:

Prevent a repeat

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

  1. 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
  2. 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
  3. Vite docs, Env Variables and Modes (VITE_ prefix, values bundled at build time): https://vite.dev/guide/env-and-mode
  4. Next.js docs, Environment Variables (NEXT_PUBLIC_ inlined at build time): https://nextjs.org/docs/app/guides/environment-variables
  5. 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
  6. Supabase docs, JWT signing keys: https://supabase.com/docs/guides/auth/jwt-signing-keys
  7. 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/