Does the service role bypass RLS?

Yes, unless the request carries a user’s token. Here’s which keys run as service_role, why a secret key must never reach a browser, how to act as the user from server code, and what to do if one leaks.

By the NestedBit team · Updated

Yes, with one exception

The service role bypasses row-level security. In Supabase it’s a Postgres role, service_role, with the bypassrls attribute, and Supabase’s docs say a secret key works through that role. So does the older service_role key. A request made with either reads and writes every row of every table it has grants on, whatever your policies say.

The exception: a secret key only bypasses RLS when the request carries no user access token. If the request also carries a signed-in user’s token, it runs under that user’s policies, even though the client was created with the secret key. That catches a lot of server code; it’s covered below.

If you’re new to how roles and policies fit together, start with what RLS is in Supabase.

Which key runs as which role

CredentialLooks likePostgres roleRLS
Publishable keysb_publishable_…anon, or authenticated with a user’s tokenApplies
Legacy anon keyA JWTanon, or authenticated with a user’s tokenApplies
Secret keysb_secret_…service_roleBypassed, unless a user token is sent
Legacy service_role keyA long-lived JWTservice_roleBypassed, unless a user token is sent
SQL Editor, or connecting as postgresDashboard sign-in, or the database passwordpostgresBypassed

Supabase is deprecating the legacy anon and service_role keys by the end of 2026. Creating publishable and secret keys doesn’t turn the old ones off; you disable them separately in Settings > API Keys.

Never ship it to a browser

Anyone holding a secret key can read and change your tables, your Storage files and your user accounts, past every policy. Supabase’s docs are blunt: never use a secret key in the browser or expose it to customers.

Supabase also refuses secret keys from browsers: it matches on the User-Agent header and returns 401. Treat that as a guard against mistakes, not against attackers. A User-Agent is whatever the sender says it is, so a key copied out of your page still works from a script.

How secret keys usually end up in front-end code:

  • A public environment variable. Variables prefixed NEXT_PUBLIC_ in Next.js or VITE_ in Vite are bundled into the JavaScript every visitor downloads.
  • A mobile app. Anything in the app can be pulled out of it.
  • A shortcut while debugging. RLS returns nothing, someone swaps in the secret key, and the app works, for everyone.

If your app only works with the secret key, the problem is a missing policy or grant. See how to enable RLS in Supabase for what each symptom means.

When your server code doesn’t bypass RLS

RLS follows the token in the Authorization header, not the key in apikey. Supabase’s troubleshooting article lists the usual ways a user’s token replaces the secret key there:

  • Server-side rendering clients are built to share the user’s session from cookies, so the user’s token goes out with every request. Create a separate admin client with supabase-js instead.
  • A user’s token set by hand in the client’s headers, often in an Edge Function.
  • Auth calls on the admin client, such as signUp, which store a user session on that client. To create users from the server, use auth.admin.createUser().

A client meant to bypass RLS should hold no session at all:

import { createClient } from '@supabase/supabase-js';

// Server only. A separate client with no user session, so it runs as
// service_role and skips RLS.
export const admin = createClient(
  process.env.SUPABASE_URL!,
  process.env.SUPABASE_SECRET_KEY!,
  { auth: { persistSession: false, autoRefreshToken: false } },
);

Act as the user from server code instead

Most server code doesn’t need to bypass anything. When an API route or Edge Function works on behalf of a signed-in user, pass their access token through, and the same policies that protect the browser protect the server. A bug in your own permission checks then can’t leak other users’ rows.

import { createClient } from '@supabase/supabase-js';

// Runs as the user who made the request, so their RLS policies apply.
// The publishable key goes in apikey; the user's access token in Authorization.
export function clientForRequest(req: Request) {
  return createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_PUBLISHABLE_KEY!, {
    global: { headers: { Authorization: req.headers.get('Authorization') ?? '' } },
    auth: { persistSession: false, autoRefreshToken: false },
  });
}

In Edge Functions, Supabase’s @supabase/server helper does this for you: withSupabase hands your handler a supabase client scoped to the calling user and, in secret-key mode, a supabaseAdmin client that bypasses RLS. Functions verify the caller’s JWT by default (verify_jwt = true); leave that on. Keys belong in the apikey header, and only a user’s token in Authorization.

Keep the admin client for jobs that really act for no one in particular: webhooks, scheduled jobs, admin tools behind their own sign-in, and creating users.

security definer functions: the other bypass

A function marked security definer runs as the role that created it, not the caller. On Supabase that’s usually postgres, which has bypassrls, so the function reads past every policy. That’s useful inside policies, for checking a role without a policy that queries its own table, and dangerous anywhere the API can call it.

Supabase’s docs warn that a security definer function in an exposed schema is callable over the Data API, at /rest/v1/rpc/, with the creator’s privileges. Keep these functions in a schema the API doesn’t serve, set an empty search_path, and grant execute only to the roles that need it:

create schema if not exists private;
grant usage on schema private to authenticated;

-- Runs as its owner (postgres, which bypasses RLS), so keep it narrow:
-- it answers one yes/no question about the caller and returns nothing else.
create or replace function private.is_org_admin(org uuid) returns boolean
  language sql stable security definer set search_path = ''
as $$
  select exists (
    select 1 from public.org_members m
    where m.org_id = org
      and m.user_id = (select auth.uid())
      and m.role = 'admin'
  );
$$;

revoke all on function private.is_org_admin(uuid) from public, anon;
grant execute on function private.is_org_admin(uuid) to authenticated;

To find the ones already in public:

-- security definer functions in schemas the API serves
select n.nspname as schema, p.proname as function_name
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where p.prosecdef and n.nspname = 'public';

Views are the quieter version of the same thing: one created by postgres runs with its owner’s rights and skips the policies on the tables it reads, unless it’s created with security_invoker = true.

If a secret key leaks

Supabase’s rotation guide sets the order:

  1. Fix the cause first, such as the public variable or the committed file, or the new key leaks the same way.
  2. Create a new secret key in Settings > API Keys.
  3. Replace the old key everywhere it’s used: servers, Edge Functions, CI, scheduled jobs.
  4. Delete the old key once nothing uses it, or straight away if someone is using it. Deleting a secret key can’t be undone.

A leaked legacy service_role key is a bigger job. Retiring it deactivates both legacy keys, the anon key included, so move your browser and mobile apps to a publishable key first, then revoke the legacy JWT secret too.

Then look at what the key could reach while it was out: check your tables, Storage and auth users for changes you didn’t make.

A short checklist

  • Secret keys live only in server environment variables, never in a public prefix or a mobile build.
  • Admin clients are created separately, with no user session.
  • Code that acts for a user passes their token, so RLS applies.
  • No security definer functions in public; views use security_invoker.
  • Every table the API serves has RLS on and the policies it needs.

The free checker in the Supabase RLS Generator covers the database half of that list: tables without RLS, security definer functions the API can call, views that skip RLS and more, with the SQL to fix each. It runs in your browser. To write the policies themselves, use the generator.

Free tool · No sign-up

Supabase RLS Generator

Generate row-level security policies and check a schema for tables left open.

Starts with a free 30-minute intro call, or email hello@nestedbit.com.