Fixing a Leaked Supabase Service Role Key
Even if you wrote perfect PostgreSQL RLS policies, putting SUPABASE_SERVICE_ROLE_KEY inside a browser bundle or NEXT_PUBLIC_ variable gives attackers god-mode admin access to your entire database and Auth user list.
When a founder hits an RLS permission error during development, AI assistants sometimes suggest 'switch to the service role key so the query works' — directly inside a shared lib/supabase.ts imported by client components.
- Completely bypasses Row Level Security on EVERY table in every schema
- Allows attackers to read/delete auth.users, forge magic links, or hijack admin accounts
- Allows downloading or wiping private Supabase Storage buckets
Vulnerable Pattern vs. Production Fix
// lib/supabase.ts — Imported by React components!
import { createClient } from "@supabase/supabase-js";
export const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY! // GOD-MODE KEY IN BROWSER!
);// lib/supabase/admin.ts — SERVER ONLY
import "server-only";
import { createClient } from "@supabase/supabase-js";
export function createAdminClient() {
const url = process.env.NEXT_PUBLIC_SUPABASE_URL!;
const serviceKey = process.env.SUPABASE_SERVICE_ROLE_KEY!; // Never NEXT_PUBLIC_!
return createClient(url, serviceKey, {
auth: { persistSession: false, autoRefreshToken: false },
});
}Copy-Paste AI Fix Prompt (Cursor · Lovable · Bolt · Claude Code)
Audit all Supabase client instantiations in this codebase: 1. Search for SERVICE_ROLE, service_role, or hardcoded Supabase JWTs. 2. Ensure browser/client code ONLY ever uses NEXT_PUBLIC_SUPABASE_ANON_KEY / VITE_SUPABASE_ANON_KEY. 3. Isolate any service_role usage into a dedicated server-only file (with import "server-only") called exclusively by verified webhook routes or admin server actions. 4. Replace any service_role hack used to dodge RLS with proper user-scoped RLS policies.
How to Verify Your Fix Worked
- 1If service_role was ever exposed in a client bundle or public repo, rotate your JWT secret / API keys in Supabase Dashboard immediately.
- 2Decode any JWT present in your browser Network tab at jwt.io — the `role` claim must ONLY ever be `anon` or `authenticated`, never `service_role`.
- 3Scan your live domain with VibeSecCheck, which decodes embedded Supabase JWT payloads to detect service_role leaks automatically.
Frequently Asked Questions
How can I tell if a Supabase JWT is anon vs service_role?
Base64-decode the middle segment of the JWT: the JSON payload contains `"role": "anon"` for public keys and `"role": "service_role"` for admin keys. VibeSecCheck checks this automatically.
When should I actually use SUPABASE_SERVICE_ROLE_KEY?
Only on trusted backend servers (Next.js Route Handlers, Supabase Edge Functions, cron jobs) for tasks like processing verified Stripe webhooks or background jobs.
Does Supabase's new secret key format (sb_secret_*) change this rule?
The same strict rule applies: publishable keys (`sb_publishable_*` / `anon`) are for browsers; secret keys (`sb_secret_*` / `service_role`) must stay strictly server-side.