PatchPaste · Guides

Last updated 30 October 2026

Supabase service_role key exposed? How to check and fix it

Supabase gives you two kinds of key. The anon / publishable key is meant to be in your browser code. The service_role / secret key is the master key: it skips Row Level Security on every table. If it ends up in your frontend, anyone who opens dev tools can read, edit or delete all of your data. Here is how to check and what to do, in the right order.

Short answer: search your live site's JavaScript for sb_secret_ and service_role. If either is there, remove it from client code, create a new secret key for your server, then delete (or deactivate) the leaked one. Or scan your live app free and we'll look for it from the outside.

Which Supabase keys are safe in the browser?

KeyLooks likeIn the browser?
Publishablesb_publishable_...Yes, by design
anon (legacy)JWT starting eyJ, role anonYes, by design
Secretsb_secret_...Never
service_role (legacy)JWT starting eyJ, role service_roleNever

The public keys only reach what your RLS policies allow. The secret keys map to the service_role Postgres role, which has BYPASSRLS, so no policy you write applies to them. Supabase is deprecating the legacy anon and service_role JWT keys by the end of 2026 in favour of publishable and secret keys (Supabase docs).

1. Check your live site (2 minutes)

Check what is actually deployed, not just your repo. Open your site, then dev tools → Sources (Chrome) or Debugger (Firefox), and search all files (Ctrl/Cmd + Shift + F) for:

Or from a terminal, against your production build output:

# Vite / Lovable / Bolt
npm run build && grep -rnoE "sb_secret_[A-Za-z0-9_-]+|service_role" dist/

# Next.js
npm run build && grep -rnoE "sb_secret_[A-Za-z0-9_-]+|service_role" .next/static/

2. Tell an anon JWT from a service_role JWT

A JWT is three base64 parts separated by dots. The middle part says which role it is. Decode it locally (don't paste a possibly-secret key into a website):

echo 'PASTE_THE_eyJ..._KEY' | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null; echo
# {"iss":"supabase","ref":"abcd...","role":"anon",...}          <- fine
# {"iss":"supabase","ref":"abcd...","role":"service_role",...}  <- leaked admin key

3. Find out how it got there

Fix the cause before rotating, or the new key will leak the same way. The usual culprits:

4. Move admin work to the server

The browser should only ever use the publishable (or anon) key. Anything that genuinely needs admin rights goes in a server route or Supabase Edge Function that checks who is calling first. Paste this into Lovable, Bolt or Cursor:

My Supabase service_role / secret key is shipped to the browser. Remove it from all frontend code and from any env var with a VITE_, NEXT_PUBLIC_, EXPO_PUBLIC_ or REACT_APP_ prefix. The browser Supabase client must use only the publishable (or anon) key. Move every operation that needed the admin key into a Supabase Edge Function or server route that verifies the signed-in user before acting, and read the secret key there from a server-only env var. Where the admin key was only used to get past an RLS error, write the correct RLS policy instead. List every file you changed.

5. Rotate and retire the leaked key

Once the new build no longer contains the key, follow Supabase's rotation order:

  1. In Settings → API Keys, create a new secret key.
  2. Put it in your server-side environment variables only (Vercel/Netlify project settings, Edge Function secrets), replacing the old one everywhere.
  3. Confirm every server component works on the new key.
  4. Retire the leaked key. A leaked sb_secret_ key: delete it (this can't be undone). A leaked legacy service_role JWT: deactivate the legacy keys in the same section. That also turns off the legacy anon key, so switch your frontend to a publishable key first.

Note: the new sb_secret_ keys return 401 when Supabase detects a browser User-Agent, which stops your own frontend using one by accident. It doesn't stop someone who copied the key from using it with curl, so a leaked one still has to go.

6. Check whether anything was touched

Look at your Supabase logs (API / Postgres) for unexpected requests, and compare row counts or recent updated_at values in sensitive tables. If personal data could have been read, you may have breach-notification duties (for example under UK/EU GDPR), so note when the key went live and when you rotated it.

7. Stop it coming back

This leak is usually re-introduced by the next AI edit, not by a person. Add the grep in step 1 to your build, and re-check the live site after every deploy. PatchPaste scans are free, so you can scan again after each deploy. Also worth checking: whether your tables are readable with the public key.

Exposed service_role key FAQ

What happens if my Supabase service_role key is exposed?

Anyone who copies it can read, change or delete every row in every table exposed by your API, because the service_role Postgres role has the BYPASSRLS attribute. Row Level Security policies do not apply to it at all. Treat it as a full data breach risk and rotate it.

How do I know if my service_role key is in my frontend?

Search your built JavaScript (or the Sources tab in your browser's dev tools on the live site) for sb_secret_ and service_role. Legacy keys are JWTs starting with eyJ; decode the middle part and look for "role":"service_role". A public anon or publishable key is fine; a service_role or secret key is not.

Is the Supabase anon key also a secret?

No. The anon key and the newer sb_publishable_ key are designed to be public and only reach what Row Level Security allows. Only the service_role key and sb_secret_ keys must stay on the server.

Does the new sb_secret_ key stop working in the browser?

Supabase says secret keys are rejected with HTTP 401 when the request looks like it comes from a browser (it checks the User-Agent header). That stops accidental use from your frontend, but an attacker who copies the key can still use it from curl or a script, so a leaked sb_secret_ key must still be deleted.

How do I rotate a leaked Supabase service_role key?

Fix the leak first, then create a new secret key in Settings > API Keys, switch every server component to it, confirm nothing still uses the old one, and then retire the old key. Delete a leaked sb_secret_ key (this cannot be undone). For a legacy service_role JWT, deactivate the legacy keys in the same section; that also switches off the legacy anon key, so move your frontend to a publishable key first.

Why did my AI builder put the service_role key in the frontend?

Usually to make a 'permission denied' or 'new row violates row-level security policy' error go away. Using the admin key makes the error disappear because it skips RLS entirely. The right fix is a correct RLS policy, or moving the privileged action into a server route or Edge Function.

Can PatchPaste check this without access to my code?

Yes. PatchPaste loads your live site like any visitor, reads the JavaScript the browser downloads, and flags sb_secret_ keys and JWTs whose role is service_role. Keys are shown masked in the report. It never logs in and never uses the key.

Check your live app free

Paste your URL. We read the JavaScript your site sends to every visitor and flag Supabase secret and service_role keys (shown masked), plus Stripe, OpenAI and other secret keys, open Supabase tables, exposed files and missing headers. Free report; £9 one-off if you want the paste-in fixes.

Free. No login. No code access.

Want to see one first? View a sample report.