Skip to main content

Supabase is retiring anon and service_role keys. Use the migration to find the key that shouldn't be in your app

Swapping keys is ten minutes. Checking which key your live app actually ships is the part that prevents a breach.

Updated 6 min readMarkdown
Supabase is retiring anon and service_role keys. Use the migration to find the key that shouldn't be in your app

Short answer: before you swap keys, check which key your live app ships today. If the browser bundle contains the legacy service_role JWT, you've had a full database bypass in public, and migrating without rotating leaves it valid until you deactivate legacy keys. Check, migrate, deactivate, then re-scan.

Supabase is moving every project from the legacy JWT keys (anon and service_role) to publishable (sb_publishable_...) and secret (sb_secret_...) keys. Its docs say the legacy keys are being deprecated, both systems work side by side until you switch the old ones off, and new or recently restored projects may not have legacy keys at all.

Most migration guides stop at "replace the variable and redeploy." That's fine for the anon key. It skips the question that matters: was the wrong key in the browser all along?

Why this migration is a security moment

Legacy keys are both long JWTs starting with eyJ. They look almost identical. The only difference is a field inside the token: "role": "anon" or "role": "service_role". That's why AI builders mix them up: an RLS error appears, someone (or something) swaps in the "other key," the error disappears, and the service_role key ships to every visitor.

The new keys fix that confusion going forward. A sb_secret_ key is obviously secret by name, and Supabase rejects it when the request comes from a browser user agent. But that browser check doesn't stop someone who copies a key from your bundle and uses it from a script. The migration is the moment to find out what you shipped before.

Step 1: find out which key your live app uses

On your published site:

  1. Open DevTools → Network, reload, click any request to supabase.co, read the apikey header.
  2. Open Sources, search all files for eyJ, service_role, sb_secret_.
  3. Decode any eyJ token locally (never paste it into a website):
const k = 'PASTE_KEY';
JSON.parse(atob(k.split('.')[1].replace(/-/g,'+').replace(/_/g,'/')));

"role":"anon" is expected. "role":"service_role" is the problem. The Supabase JWT role decoder explains each field if you'd rather read it there.

Step 2: if you found service_role in the browser

Treat it as compromised. It has been public for as long as that build was live. The service_role key exposed fix has the full sequence; in short:

  1. Create the new publishable and secret keys (Settings → API Keys).
  2. Move whatever needed elevated access into an Edge Function that reads the secret key from secrets.
  3. Put the publishable key in the browser. If pages break, they were relying on RLS being bypassed. Write proper policies for those tables.
  4. Deactivate the legacy keys. Until you do, the leaked service_role JWT still works. Deactivating legacy keys disables both old keys, so finish moving every client first.
  5. Check logs for requests you don't recognize from the period the key was public.

Step 3: if you only found the anon key

Good, but the migration still has two traps:

  • Rename vs. revalue. In Lovable and Bolt apps, the frontend usually reads one VITE_ variable. Put the publishable key in that variable (or update the code to read a new one). Adding a differently named variable does nothing. And because Vite bakes values in at build time, you must redeploy.
  • Webhooks and pg_net. Database webhooks that send the legacy service_role key as Authorization: Bearer won't accept a secret key there; Supabase's guide says to send it on the apikey header instead. Fix those before you deactivate legacy keys, or background jobs silently fail.

Step 4: check that RLS still holds

The publishable key has the same low privileges as anon, so your policies behave the same. That also means any weak policy is still weak. While you're in there:

select tablename, policyname, cmd, qual from pg_policies
where schemaname = 'public' and (qual = 'true' or with_check = 'true');

Step 5: re-scan the live app

After redeploying, confirm from outside that the bundle has only sb_publishable_, no eyJ service_role token and no sb_secret_, and that anonymous reads on private tables return nothing. Paste the URL into the free scan at proofforge.live: it decodes Supabase keys it finds in the bundle and runs count-only anonymous reads on your tables. The first full fix is free; the $9 Fix Report unlocks every fix with prompts for your builder.

A note for AI builders

If Lovable, Bolt or Cursor did the migration for you, read the diff. Look for the publishable key in the frontend variable, the secret key only in Edge Function secrets, and no eyJ string left anywhere in the client code. Ask the assistant to list every place each key is used; it's faster than searching by hand and it catches webhooks you forgot.

The order, on one line

Check the live key → migrate → fix webhooks → deactivate legacy keys → re-scan. The key swap is ten minutes. Skipping the check is how a two-year-old leak survives the migration.

Common questions

Will my app break if I do nothing? Possibly, once Supabase switches legacy keys off for good. Projects restored from a pause may already lack legacy keys, which is a common source of "it worked yesterday" bugs.

Is the publishable key safer than the anon key? Same privileges, same RLS. What improves is clarity: nobody mistakes sb_publishable_ for a secret, or sb_secret_ for something safe to ship.

Do I need to rotate the anon key? Not for security; it was meant to be public. You replace it as part of the migration and deactivate it with the legacy pair.

What about mobile apps? Shipped app binaries keep the old key until users update. Plan the deactivation date around your app-store release, not your web deploy.

Can a scan tell me if someone used the leaked key? No. A scan shows what's exposed now. Whether anyone used an old key is a question for your Supabase API logs, and only for the window those logs cover.

Should I create one secret key or several? Supabase lets you create multiple named secret keys. One per backend component means a future leak forces one rotation, not all of them.

Built by Brennan Zambo.

No comments yet. Be the first to comment.

More from this blog