Skip to content

Commit 75c1b35

Browse files
fix(db): grant supabase_auth_admin access to tenant_users + set hook SECURITY DEFINER
The custom_access_token_hook was not SECURITY DEFINER and supabase_auth_admin lacked SELECT on tenant_users/super_admins. This caused the hook to silently fail (WHEN OTHERS handler), so JWTs never got tenant_id/tenant_role claims — breaking all RLS policies for every user. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3f3ff8b commit 75c1b35

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- =============================================================================
2+
-- Fix custom_access_token_hook permissions
3+
--
4+
-- Root cause: The JWT hook queries tenant_users and super_admins but
5+
-- supabase_auth_admin had no SELECT grant on those tables, AND the hook
6+
-- was not SECURITY DEFINER so RLS blocked the queries even with grants.
7+
-- The WHEN OTHERS handler swallowed the permission error, returning the
8+
-- event unchanged — so JWTs never got tenant_id, tenant_role, or
9+
-- user_role claims. This broke all RLS policies.
10+
--
11+
-- Fix: SECURITY DEFINER makes the hook run as postgres (bypasses RLS).
12+
-- Grants are kept as a safety net.
13+
-- =============================================================================
14+
15+
GRANT SELECT ON public.tenant_users TO supabase_auth_admin;
16+
GRANT SELECT ON public.super_admins TO supabase_auth_admin;
17+
18+
-- The hook must be SECURITY DEFINER to bypass RLS on tenant_users/super_admins
19+
ALTER FUNCTION public.custom_access_token_hook(jsonb) SECURITY DEFINER;

0 commit comments

Comments
 (0)