Cenk Kurtoğlu©26

Supabase security · 7 minute read

Supabase Edge Functions security: service_role bypasses every RLS policy

You write an Edge Function, drop SUPABASE_SERVICE_ROLE_KEYinto its environment, and call Supabase from inside. The function now runs as service_role — a database superuser that ignores every Row Level Security policy. Your carefully written policies are bypassed, silently, for the lifetime of that function call.

The auth check inside the function is the only control left. If that check is missing, wrong, or based on a client-supplied value, your data is exposed. Below is what I check for, the runtime query that names the role your request actually runs as, and the curl mistake that makes local testing lie to you.

What service_role actually means

service_role is not "a privileged app user". It is a superuser role that bypasses RLS entirely. PostgreSQL checks ownership and privileges before RLS, and service_roleowns or is granted everything it needs. A query run asservice_role returns all rows — even rows your policies hide from anon and authenticated.

In an Edge Function, the client key (anon orauthenticated JWT) is optional. If you initialize the Supabase client with the service_role key, every query inside that function runs as service_role. Table policies never see the call.

The one query that tells you who you are

Run this inside the function (or against a scratch project) to see which role your request actually uses:

select current_user, session_user, auth.uid() as uid,
       current_setting('request.jwt.claims', true) as claims
from pg_catalog.pg_is_in_recovery();

If current_user is service_role, RLS is off for that session. If it is anon orauthenticated, policies apply — but only if the key you passed actually maps to that role.

A safer version for production logging:

select case
  when current_user = 'service_role' then 'BYPASS_RLS_SUPERUSER'
  when current_user = 'authenticated' then 'AUTHENTICATED_RLS'
  when current_user = 'anon' then 'ANON_RLS'
  else current_user
end as effective_role;

Where the leak usually hides

Edge Function pattern: verify then query

The safe shape is: verify the caller's JWT first, then run the query as the caller (anon or authenticated), not as service_role:

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

export default async function (req) {
  const authHeader = req.headers.get('Authorization')
  if (!authHeader) {
    return new Response(JSON.stringify({ error: 'missing token' }), { status: 401 })
  }

  // Client runs as the caller — RLS applies.
  const supabase = createClient(
    Deno.env.get('SUPABASE_URL')!,
    Deno.env.get('SUPABASE_ANON_KEY')!,
    { global: { headers: { Authorization: authHeader } } }
  )

  const { data: user, error } = await supabase.auth.getUser()
  if (error || !user) {
    return new Response(JSON.stringify({ error: 'invalid token' }), { status: 401 })
  }

  // Queries below run as the authenticated user — policies are enforced.
  const { data: projects } = await supabase
    .from('projects')
    .select('*')
    .eq('org_id', user.user_metadata.org_id)

  return new Response(JSON.stringify({ projects }), { status: 200 })
}

Use service_role only for operations that genuinely need superuser access (e.g. a scheduled cleanup that runs without a user context), and gate those behind a secret or internal-only trigger, never a public HTTP endpoint.

Local testing trap: the missing colon

Testing Edge Functions locally with curl is where most people get a false sense of security. The Authorization header needsBearer with a space:

# Wrong — header is malformed, Supabase sees no token, falls back to service_role
curl http://localhost:54321/functions/v1/my-function   -H "Authorization:Bearer eyJ..."

# Right
curl http://localhost:54321/functions/v1/my-function   -H "Authorization: Bearer eyJ..."

With the missing colon, the function may run as service_role in your local environment and "work" — then fail in production with 42501 or return empty, or worse, work in production without the token because the fallback path is taken. Test with the exact header shape your clients send.

What to audit before you ship

grep -rn "service_role\|SUPABASE_SERVICE\|sb_secret_" \
  --include="*.ts" --include="*.tsx" --include="*.js" --include="*.env*" .
  | grep -v node_modules

Every hit inside an Edge Function (or any server route) is a candidate bypass. Ask: does this path verify the caller first? Is the result filtered by a policy, or does service_role return everything?

The catalog checks from the 13-point checklist cover table RLS, permissive policies, missing WITH CHECK, grants and default privileges. Edge Functions add a sixth boundary: server-side code that runs above RLS.

Honest limits

These are code patterns and documented semantics, not a live function test. I cannot tell from outside whether your function verifies the JWT before calling Supabase, or whether it falls back to service_role when the header is absent. You need to read the function source and test with and without a valid token.

A nightly read-only diff over your Edge Function directory plus the same catalog queries reports new functions and changed policies. The free starter is here:

Get the free RLS Drift Monitor (13 checks, n8n + SQL) →

If you want a person to read the function source and separate real exposure from noise, that is a fixed-scope review: Supabase RLS Spot Check. Work starts only after payment is secured.

Related: Supabase Realtime security: table RLS does not protect your channels · the first 15 minutes of a Supabase security audit.

Free entry

If you would rather run these checks yourself

Honest limit: a catalog check is not proof of a leak, and an empty log is not proof the key was never used. Zero findings does not mean safe. If you want a human: $29 self-run kit · $95 spot check (five tables, one working day) · $195 full audit — work starts only after payment is secured.

Template bundle

Review all 20 frontend starters in one bundle

20 editable frontend starters with live demos are $79 — one payment, no subscription.

View the Bundle — $79↗