MELLEPRISE MELLEPRISE AI MP Blog
← All articles

Your supabase service role key is in the frontend — rotate it now

Pastel comic of a golden master key taped to a shop window while every small door in the row stands open

Your supabase service role key is sitting in the JavaScript your visitors download, and the app looks perfectly fine — that is the problem.

Sign-up works. Forms save. The dashboard shows rows. Nobody gets an error, because the key that bypasses every rule you meant to write is doing its job silently. The failure is invisible until someone searches your bundle for service_role or sb_secret_ and finds the master key to your entire database.

The supabase service role key is a master key

Supabase hands you two families of keys. The publishable key (older projects call it anon) is meant for browsers and identifies your project while row level security decides what it may touch. The secret key — the service_role JWT in older projects — maps to a Postgres role with BYPASSRLS. Policies never apply to it.

Read that again: the service role key ignores RLS on purpose. It exists for backends you control — Edge Functions, a Node API, cron jobs — where it must do things users may not, like writing audit rows or managing accounts. In a browser it is not a shortcut. It is a leak.

With the key, a stranger can:

  • Read every row in every table your grants allow — users, orders, messages
  • Insert, update, or delete data with no policy in the way
  • Create or confirm auth users through the admin API
  • Read storage objects your app treats as private

Supabase is replacing the JWT-style keys with short sb_publishable_ and sb_secret_ strings, and the legacy service_role key is deprecated toward the end of 2026. The rule does not change: short or long, old or new — the elevated key stays server-side.

How it ended up in your frontend

Nobody decided to ship the master key. The usual path is quieter: your app hit a permission error, and the supabase service role key was the fastest way to make it stop.

Row level security errors feel like bugs. The insert fails, the AI coding tool retries, and at some point it discovers the other key — the one where the error disappears. It does not understand that the error was the point. It sees a failing test and fixes it.

Three places to look first:

  1. Client env vars. Anything prefixed VITE_, NEXT_PUBLIC_, or REACT_APP_ is compiled into the public bundle. VITE_SUPABASE_SERVICE_ROLE_KEY in .env is not hidden — it is shipped.
  2. The Supabase client init. Search for createClient(. If the second argument is the service role key — or a variable that resolves to it — the browser is running as root.
  3. Git history. A key removed last week still sits in old commits. Search the history, not just the working tree.

The supabase anon key is the only key that belongs in this code. If your app needs the elevated one for a real reason, that code path belongs on a server.

Check your app in five minutes

  1. Open your live site, open DevTools, go to Sources. Search all loaded files for service_role, sb_secret_, and eyJ.
  2. In the repo, run a search for service_role, sb_secret_, and SERVICE_ROLE — including .env files and build output in dist, build, or .next.
  3. Check the variables your hosting provider injects. A secret named correctly still becomes public if the framework copies it into client code.
  4. Look at git history for the same strings. Found means leaked — assume copied.

If nothing turns up, the remaining risk is the opposite one: the anon key with no supabase rls policies behind it — quieter, but the same open database.

Fix it — rotate, move, then write the missing policies

  1. Create a new secret key in Supabase Dashboard → Settings → API Keys. Do not disable the old one yet — running code still uses it.
  2. Move every legitimate use to the new key: Edge Function secrets, server env vars, CI. The elevated key never gets a VITE_/NEXT_PUBLIC_ prefix again.
  3. Retire the leaked key. For legacy service_role, retiring also affects the paired anon key — swap clients to the publishable key in the same pass. Retiring is the step that actually locks the door.
  4. Remove it from the frontend. The client goes back to the publishable key. Redeploy and confirm the bundle no longer contains it.
  5. Write the policies the AI skipped. Enable RLS and scope SELECT/INSERT/UPDATE/DELETE to auth.uid() or your membership model. This is the real fix — the key only ever masked the missing policy.
  6. Verify logged out. Call your REST endpoint with only the publishable key. Private tables must return empty or 401, not rows.

Where the service role key legitimately lives

There are real reasons the key exists. Server code that must ignore RLS — provisioning users, cross-tenant jobs, webhook handlers — runs it from server-side env vars or Edge Function secrets. The test is simple: could a stranger read this value from the thing you ship? If yes, it is the wrong place.

Env vars that never left the laptop are a sibling failure — the lovable deploy checklist covers the five production settings that break the same weekend, and supabase rls walks the policy side in detail.

Pre-launch checklist

  • Bundle search finds no service_role, sb_secret_, or eyJ strings
  • Only publishable-key env vars carry VITE_/NEXT_PUBLIC_/REACT_APP_ prefixes
  • Secret key lives in server env or Edge Function secrets — never in the repo
  • Git history is clean, or the old key is retired
  • RLS enabled with policies — the fix the AI worked around
  • Logged-out REST calls return empty, not your tables

A supabase service role key in the browser is not a workaround. It is your database, handed out with the page.

FAQ

What can someone do with my supabase service role key?

Everything. The service role key bypasses row level security, so a stranger can read, change, or delete every row your grants allow, create users, and often read storage files. Treat any exposure in frontend code or a public repo as a leak.

Is the service role key the same as the anon key?

No. The anon key is public by design and safe in the browser when RLS policies are set. The service role key is a root credential for trusted backends only. Newer projects show publishable and secret keys — the secret key is the service role equivalent.

My key was in a public repo for one day. Rotate anyway?

Yes. Bots scan public repos and websites for keys continuously, so assume it was copied. Create a new secret key in the Supabase dashboard, move every legitimate use to it, then retire the old key. Retiring is what actually closes the door.