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
- An environment variable named
NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEYorVITE_SUPABASE_SERVICE_KEY. Next.js and Vite inline any variable with those prefixes into the client bundle at build time. - A “quick fix” for an RLS error: swap the anon key for the service key in
createClient, and the error disappears. - An admin page in the same frontend that needs to list users.
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/nullLegacy 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/nullAlso 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:
- Fix the cause first (step below), or the new key leaks the same way.
- In Settings → API Keys, create a new secret key.
- Put the new key in your server environments (Edge Function secrets, your backend host).
- Confirm every component works with the new key.
- Delete the compromised secret key. If someone is actively abusing it, delete it immediately and accept a short outage.
- 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
- AI API keys and system prompts in frontend code – find them and move the call server-side
- Supabase “new row violates row-level security policy” – causes and fix
- Supabase “permission denied for table” – a missing GRANT, not a missing policy
- Supabase “JWT expired” – why the token went stale and how to keep it fresh
More in Auth, JWT and secrets · all guides