Service role key in the browser: how to find it and fix it

How it ends up in the browser

It usually starts with an error. A query fails because of Row Level Security, someone asks the AI to fix it, and the quickest fix is to create the Supabase client with the service_role key. The error disappears, and so does every protection. Other common paths:

How to check it yourself

  1. Open your live app, open the developer tools and search across all loaded files (Ctrl+Shift+F, or Cmd+Option+F on a Mac) for service_role, sb_secret_, sk_live_ and sk-.
  2. Supabase's older keys are JWTs (they start with eyJ). The anon key is fine to find; the one whose payload says "role":"service_role" is not. You can paste a key into a JWT decoder you trust to see its role.
  3. Search your code for the key's variable name and check that it's only used in server code (API routes, edge functions), never in a file that starts with "use client" or in a page component.
  4. Check that /.env and your source maps (.map files) can't be downloaded from the live site.

If you found it: rotate first

Removing the key from the code isn't enough, because copies of the old JavaScript may already exist. In the Supabase dashboard, go to the project's API keys settings and create a new secret key (or rotate the JWT secret for the older keys), update your server environment with it, and revoke the old one. Do the same in Stripe, OpenAI or any other service whose key was exposed. Then check your database logs and data for changes you didn't make.

Prompt to fix it

Fix prompt

Remove every secret key from front-end code. The Supabase client used in the browser must use only the anon/publishable key; any code that needs the service_role or secret key must move to a server function or API route that checks the user's session and permissions before doing anything. Rename environment variables that hold secrets so they don't start with VITE_, NEXT_PUBLIC_ or EXPO_PUBLIC_. Make sure .env files and source maps are not published. Don't change how the app looks. List every file you changed, and remind me to rotate the keys that were exposed.

Want us to run this test for you?

The US$9 crash test without code scans the JavaScript any visitor downloads for secret keys (Supabase service_role and secret keys, Stripe, OpenAI, Anthropic, AWS, GitHub and others) without keeping their values, checks for exposed .env files and source maps, and tests your database with two test accounts. You get the evidence and a fix prompt for each issue.

Related guides

Updated 2026-11-25