Supabase service_role or secret key exposed in frontend code – detect, rotate, fix

By Abheesht Tripathi · Published

Risk: a service_role JWT or sb_secret_… key is shipped in browser JavaScript (it bypasses all RLS)

Why this is the worst Supabase leak

The publishable key (sb_publishable_…, or the legacy anon JWT) is meant to be public: requests made with it run as anon or authenticated, and RLS decides what they can do. The secret key (sb_secret_…, or the legacy service_role JWT) maps to the service_role Postgres role, which has BYPASSRLS. Anyone who copies it from your JavaScript can read, change and delete every row in every exposed table, and manage your users through the Auth admin API. Your policies don't matter any more.

Supabase's own docs note that new secret keys refuse to work in a browser (requests are matched on the User-Agent header and get HTTP 401). That breaks your app, but it does not protect the key: an attacker uses curl, not a browser. Legacy service_role keys have no such check.

How it ends up in the bundle

How to detect it

Build the app, then search the output. New-format keys are easy to spot:

npm run build
grep -rl "sb_secret_" dist/ build/ .next/static/ 2>/dev/null

Legacy keys are JWTs, and the anon and service_role keys share the same first part. Search for the last 20 characters of your service_role key (from the dashboard), which are unique to it:

grep -rl "<last-20-chars-of-service-role-key>" dist/ build/ .next/static/ 2>/dev/null

Also check the browser itself: open DevTools, Network tab, click any request to supabase.co and look at the apikey header. Search your git history too (git log -p -S "sb_secret_"): a key that was ever pushed to a public repo is leaked even if it was deleted later.

Rotate it (follow this order)

From Supabase's guide for a leaked key:

  1. Fix the cause first (step below), or the new key leaks the same way.
  2. In Settings → API Keys, create a new secret key.
  3. Put the new key in your server environments (Edge Function secrets, your backend host).
  4. Confirm every component works with the new key.
  5. Delete the compromised secret key. If someone is actively abusing it, delete it immediately and accept a short outage.
  6. If the leaked key was the legacy service_role JWT: replace the legacy anon key with a publishable key in your clients, deactivate the legacy keys in Settings → API Keys, and revoke the legacy JWT secret (see Supabase's JWT signing keys guide). Don't re-enable the legacy keys later: that restores the leaked one.

Then check for damage: unexpected rows, changed roles or admin flags in your tables, new users in Authentication, and unknown files in Storage.

Fix the cause: move privileged work server-side

The browser gets the publishable key only. Anything that truly needs to bypass RLS runs in an Edge Function or your backend, after checking who is asking:

// browser
const supabase = createClient(SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY);
const { data, error } = await supabase.functions.invoke('admin-list-users');

// supabase/functions/admin-list-users/index.ts (server)
// 1. verify the caller's JWT, 2. check they are an admin in your own table,
// 3. only then create a client with the secret key from the function's environment.

Most “I need the service key” cases are really a missing RLS policy. Write the policy instead, and keep the secret key for jobs that have no user at all.

How to verify

Rebuild and rerun both searches: no matches. Call your REST endpoint with the old key: it must now fail with 401. Your app should still work for normal users, which proves it never needed the secret key in the browser.

Related guides

More in Auth, JWT and secrets · all guides