Supabase service_role key exposed in the frontend: what to do now
sb_secret_ keys bypass row level security, so anyone who copies one from your JavaScript can read and change every table. Switch the browser client to the publishable or anon key, move privileged work into server code, then revoke the leaked key in Settings > API Keys.How do you tell which key shipped to the browser?
Supabase has two kinds of keys. The publishable key (sb_publishable_...) and the legacy anon key are meant for the browser. The secret key (sb_secret_...) and the legacy service_role key are not. Supabase's docs say they "provide full access to your project's data, bypassing Row Level Security."
The new keys say what they are in their prefix. The legacy keys are both JWTs that start with eyJ, so you have to decode one to see its role. Decoding is local and does not call Supabase:
Decode a legacy key (Node.js 18 or later)
// paste the key between the quotes; this only decodes it
const key = "eyJ...";
const payload = JSON.parse(Buffer.from(key.split(".")[1], "base64url").toString());
console.log(payload.role); // "anon" is the public key, "service_role" is the leak
Find candidate keys in your built files
npm run build
grep -roE "sb_secret_[A-Za-z0-9_-]+|eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+" dist | sort -u
Change dist to .next/static for a Next.js app. Every string that comes back is in the JavaScript your visitors download.
The vulnerable pattern and the fix
AI builders often create one Supabase client and reuse it everywhere. When that client gets the secret key so that an admin feature works, the key ships to every visitor.
Vulnerable: a browser client built with the secret key
// src/lib/supabase.ts, bundled for the browser
import { createClient } from "@supabase/supabase-js";
export const supabase = createClient(
import.meta.env.VITE_SUPABASE_URL,
import.meta.env.VITE_SUPABASE_SERVICE_ROLE_KEY // every visitor gets this
);
Fixed: publishable key in the browser, secret key only on the server
// src/lib/supabase.ts, browser
export const supabase = createClient(
import.meta.env.VITE_SUPABASE_URL,
import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY // or the legacy anon key
);
// server only: an API route, server action or Edge Function
const admin = createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_SECRET_KEY!);
Supabase's docs say a secret key does not work in a browser: Supabase matches on the User-Agent header and returns HTTP 401. That guard is documented for the new secret keys, so do not rely on it for a legacy service_role JWT, and do not rely on it at all for a key someone copies into a script.
Fix it, step by step
- Search your source for
service_role,SERVICE_ROLE,sb_secret_and anyeyJstring, and decode each JWT as shown above. - Point every browser client at the publishable key or the anon key.
- Move each feature that needed the secret key (deleting users, admin dashboards, writes that skip a policy) into server code. Supabase Edge Functions get
SUPABASE_SECRET_KEYSand the legacySUPABASE_SERVICE_ROLE_KEYas default secrets, so you never need to paste the key into a file. - Revoke the leaked key. A new secret key can be deleted in Settings > API Keys and replaced; Supabase says the new keys are not JWTs, so revoking one does not touch the others. Supabase's troubleshooting docs say it is "no longer possible to rotate the legacy anon, service and JWT secrets", so for a leaked legacy service_role key, move to publishable and secret keys and then disable the legacy keys in Settings > API Keys.
- Rebuild and redeploy so the live bundle no longer contains the key, then repeat the grep on the new build.
- Assume the data was readable while the key was exposed. Check that row level security is on with narrow policies (see our RLS guide) and review your project's logs in the Supabase dashboard for activity you do not recognise.
Check it automatically
Caelo decodes every JWT it finds in your project files on your machine and flags the ones whose role is service_role (rule secrets.supabase-service-role). It does not flag the anon key, or the fixed demo key that every local supabase start uses. It also flags sb_secret_ keys (secrets.supabase-secret) and a secret-sounding name behind a browser prefix such as NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY (browser.public-secret-name). It reads files only and does not read git history, so a key you deleted last week will not show up.
Part of the real report from the demo app in the Caelo README
- Supabase service-role key in source
src/lib/admin.ts:3 (secrets.supabase-service-role)
value: service_role JWT (ref xyzcompany)
why: A JWT with role=service_role bypasses row-level security. The anon key is fine; this one is not.
- Install Caelo: npx caelo . or the MCP server
- How Caelo compares with gitleaks, TruffleHog, Semgrep and others
1. Run the free local check
npx caelo .
Caelo is a free, open source (MIT) command line tool and MCP server from Irth Labs that checks the files of an AI-built app for exposed API keys, Supabase and Firebase misconfigurations and .env files with real values. It runs on your machine, uploads nothing and uses no AI model. Each finding comes with a plain-English fix.
2. Act on a finding
Follow the fix in your report and run Caelo again. If Git could commit an untracked file containing secrets, preview a US$19 quick fix for that file.
For findings that need an engineer, Irth Labs offers a US$490 App Health Check. You receive a written report within 48 hours and a fixed repair quote. The fee counts toward the repair if you proceed.
Questions
Is the Supabase anon key also a leak?
No. The anon key and the publishable key are designed to be public, and row level security decides what they can reach. Our anon key guide explains how to check that your policies actually protect your data.
My repository is private. Do I still need to revoke the key?
If the key was in browser code, yes: anyone who opened your site could read it, whatever the repository settings. If it was only in a private repository, revoking is still the safe choice, because everyone with access, every clone and every connected integration has a copy.
Can I keep using the legacy service_role key?
For now it works until you disable it, but Supabase is moving projects from the legacy anon and service_role keys to publishable and secret keys. New secret keys can be revoked one at a time, which makes the next leak much cheaper to fix.
Does Supabase block the service_role key in the browser?
Supabase documents a browser block for the new secret keys only: it checks the User-Agent header and returns 401. That stops an accidental browser call. It does not stop someone who copied the key into curl or a script.
Sources
- Supabase docs: Understanding API keys (publishable, secret, anon and service_role).
- Supabase docs: Migrating to the new API keys.
- Supabase troubleshooting: Rotating anon, service and JWT secrets.
- Supabase docs: Edge Function secrets (default secrets).
- Supabase GitHub discussion #29260: Upcoming changes to Supabase API keys.
- Caelo source: every rule in lib/rules.js.