Dev.to Security 🔐 Cybersecurity 👁 0 📖 2 min read

Supabase Edge Functions security: service_role bypasses every RLS policy

You write an Edge Function, drop SUPABASE_SERVICE_ROLE_KEY into its environment, and call Supabase from inside. The function now runs as service_role — a database superuser that ignores every Row Level Security policy. Y

You write an Edge Function, drop SUPABASE_SERVICE_ROLE_KEY into 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.

The auth check inside the function is the only control left.

What service_role actually means

service_role is not "a privileged app user". It is a superuser role that bypasses RLS entirely. A query run as service_role returns all rows — even rows your policies hide from anon and authenticated.

In an Edge Function, 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 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.

Where the leak usually hides

  • No auth check at all — the function calls Supabase with service_role and returns data without verifying who the caller is.
  • Client-supplied user_id — an IDOR. RLS cannot save you because the query runs as service_role, which sees every row.
  • JWT present but never verified — the function falls back to service_role when the header is missing.

Safe pattern: verify then query

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'

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

const supabase = createClient(
  Deno.env.get('SUPABASE_URL')!,
  Deno.env.get('SUPABASE_ANON_KEY')!,
  { global: { headers: { Authorization: authHeader } } }
)

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

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

Use service_role only for operations that genuinely need superuser access, and gate those behind a secret or internal-only trigger — never a public HTTP endpoint.

Local testing trap: the missing colon

The Authorization header needs Bearer with a space. Without it, Supabase sees no token and may fall back to service_role — your function "works" locally but behaves differently in production.

What to audit

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 is a candidate bypass. Ask: does this path verify the caller first?

Full write-up: https://cenkkurtoglu.com/blog/supabase-edge-function-service-role-rls-bypass

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.