Deutsch
Security wikiSupabase · Keys

What is the Supabase service role key (secret key)?

Also known as: secret key, sb_secret_, service_role key, SUPABASE_SERVICE_ROLE_KEY

The Supabase service_role or secret key bypasses row level security. What it is for, why it must never reach the browser and what to do if it did.

By Tim GeithnerPublished 2 min read

Short answer

The service role key, now called the secret key (sb_secret_…), is the admin key of a Supabase project. It runs as the service_role role, which bypasses row level security, so whoever holds it can read and change every row. Use it only in server code such as edge functions, never in the browser, and rotate it at once if it ever shipped to a client.

Ask an AI about this page:ChatGPTClaude

On this page

The service role key, now called the secret key (sb_secret_…), is the administrator key of a Supabase project. Requests made with it run as the Postgres role service_role, which has the BYPASSRLS attribute: it ignores every row level security policy. It exists for trusted back-end work such as edge functions, cron jobs and admin scripts.

The service role key in an AI-built app

The typical mistake happens when a query fails. A table has RLS, the frontend gets an empty result or permission denied, and the AI tool “fixes” it by creating the Supabase client with the service role key instead of the anon key. The page works again, because the key skips the policies. It now also skips them for every visitor, since the key sits in the JavaScript bundle, often in a VITE_ variable.

A second pattern is an edge function that uses the key correctly on the server but takes the user ID from the request body. The function then acts on whatever account the caller names, which is just as open.

How to check your app

  1. Search your frontend code and .env files for service_role, SERVICE_ROLE and sb_secret_. Any hit outside supabase/functions/ or other server code is a finding.
  2. For legacy keys that start with eyJ, decode the middle part of the JWT: a "role": "service_role" claim means it is the admin key.
  3. Confirm which API roles skip RLS in your project:
Which API roles skip RLS?
select rolname, rolbypassrls
from pg_roles
where rolname in ('anon', 'authenticated', 'service_role')
order by rolname;

Only service_role should show true. That is the role the secret key uses.

Supabase now answers browser requests that carry a secret key with HTTP 401, based on the User-Agent header. Its own docs say this is no full safeguard: a leaked key still works from any script.

How to fix or prevent it

  • Keep the key in server-side secrets only, for example Edge Function secrets, and read it there.
  • In edge functions, act as the signed-in user wherever possible so RLS still applies, and use the admin client only for work that must cross users, filtered by the caller’s verified ID.
  • Use a separate secret key per back-end component, as Supabase recommends, so a leak means rotating one key, not all.
  • If the key ever reached a browser or a repository, follow Leaked service_role key: the first 60 minutes. The code fix is on service_role key in the frontend.

Frequently asked questions

Can I use the service role key in my React app if I am careful?

No. Everything in browser code can be read by visitors. With the key they skip every RLS policy. Supabase also answers browser requests that carry a secret key with 401, so it would not even work reliably.

How do I tell the legacy anon key and service_role key apart?

Both are JWTs that start with eyJ. Paste the middle part into a JWT decoder: the role claim says anon or service_role. The new keys are easier: sb_publishable_ versus sb_secret_.

My service role key was in a public repo. Is deleting the commit enough?

No. Treat it as leaked: fix the cause, create a new secret key, move every server component to it, then retire the old one. Removing the commit does not take the key back from anyone who copied it.

Sources

  1. 1.Understanding API keys — Supabase Docs
  2. 2.Rotate a leaked API key — Supabase Docs
  3. 3.CREATE ROLE (BYPASSRLS) — PostgreSQL Documentation
  4. 4.Row Level Security — Supabase Docs
  5. 5.Securing Edge Functions — Supabase Docs

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt