Skip to content
All rules

★SB-EDGE-NOAUTH

Edge function uses service_role without checking the caller

Severity
High
Confidence
High
Stack
Supabase
Runs
On by default

What the CLI looks for

An edge function or API route that creates a client with the service_role key and never calls auth.getUser(), getClaims() or checks a shared secret.

Why it matters

The service_role key bypasses Row Level Security. If the function never checks who is calling, anyone who has your public anon key, which ships in your frontend, can invoke it and act with full database access.

How to fix it

Verify the caller before doing anything privileged: forward the Authorization header, call auth.getUser() and check that this user may perform the action. For server-to-server calls (webhooks, n8n, Make), require a shared secret header and compare it in constant time.

const authHeader = req.headers.get("Authorization") ?? "";
const userClient = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, {
  global: { headers: { Authorization: authHeader } },
});
const { data: { user }, error } = await userClient.auth.getUser();
if (error || !user) return new Response("Unauthorized", { status: 401 });

// Only now use the service_role client, and scope every query to user.id