|
| 1 | +-- Fix remaining infinite recursion in RLS policies |
| 2 | +-- The recursion happens when: |
| 3 | +-- 1. Query sessions → "Participants can view their sessions" → EXISTS on session_participants |
| 4 | +-- 2. session_participants RLS → "Host can view session participants" → EXISTS on sessions |
| 5 | +-- This creates a circular dependency. |
| 6 | + |
| 7 | +-- The is_session_participant and is_session_host functions already exist from |
| 8 | +-- migration 20250123000002, but they haven't been applied to all problematic policies. |
| 9 | + |
| 10 | +-- ============================================================================= |
| 11 | +-- STEP 1: Fix sessions table policies that query session_participants |
| 12 | +-- ============================================================================= |
| 13 | + |
| 14 | +-- Drop the problematic policy on sessions table |
| 15 | +DROP POLICY IF EXISTS "Participants can view their sessions" ON public.sessions; |
| 16 | + |
| 17 | +-- Recreate using SECURITY DEFINER helper function |
| 18 | +CREATE POLICY "Participants can view their sessions" |
| 19 | + ON public.sessions FOR SELECT |
| 20 | + USING ( |
| 21 | + public.is_session_participant(id, auth.uid()) |
| 22 | + ); |
| 23 | + |
| 24 | +-- ============================================================================= |
| 25 | +-- STEP 2: Fix session_participants policies that query sessions |
| 26 | +-- ============================================================================= |
| 27 | + |
| 28 | +-- Drop and recreate "Host can view session participants" |
| 29 | +DROP POLICY IF EXISTS "Host can view session participants" ON public.session_participants; |
| 30 | + |
| 31 | +CREATE POLICY "Host can view session participants" |
| 32 | + ON public.session_participants FOR SELECT |
| 33 | + USING ( |
| 34 | + public.is_session_host(session_id, auth.uid()) |
| 35 | + ); |
| 36 | + |
| 37 | +-- Drop and recreate "Host can update participants" |
| 38 | +DROP POLICY IF EXISTS "Host can update participants" ON public.session_participants; |
| 39 | + |
| 40 | +CREATE POLICY "Host can update participants" |
| 41 | + ON public.session_participants FOR UPDATE |
| 42 | + USING ( |
| 43 | + public.is_session_host(session_id, auth.uid()) |
| 44 | + ); |
| 45 | + |
| 46 | +-- Drop and recreate "Host can delete participants" |
| 47 | +DROP POLICY IF EXISTS "Host can delete participants" ON public.session_participants; |
| 48 | + |
| 49 | +CREATE POLICY "Host can delete participants" |
| 50 | + ON public.session_participants FOR DELETE |
| 51 | + USING ( |
| 52 | + public.is_session_host(session_id, auth.uid()) |
| 53 | + ); |
| 54 | + |
| 55 | +-- ============================================================================= |
| 56 | +-- STEP 3: Fix media_sessions policies that query session_participants or sessions |
| 57 | +-- ============================================================================= |
| 58 | + |
| 59 | +-- Drop and recreate "Room participants can view media sessions" |
| 60 | +DROP POLICY IF EXISTS "Room participants can view media sessions" ON public.media_sessions; |
| 61 | + |
| 62 | +CREATE POLICY "Room participants can view media sessions" |
| 63 | + ON public.media_sessions |
| 64 | + FOR SELECT |
| 65 | + TO authenticated |
| 66 | + USING ( |
| 67 | + public.is_session_participant(room_id, auth.uid()) |
| 68 | + OR public.is_session_host(room_id, auth.uid()) |
| 69 | + ); |
| 70 | + |
| 71 | +-- ============================================================================= |
| 72 | +-- STEP 4: Add a helper function to check creator status |
| 73 | +-- ============================================================================= |
| 74 | + |
| 75 | +CREATE OR REPLACE FUNCTION public.is_session_creator(p_session_id UUID, p_user_id UUID) |
| 76 | +RETURNS BOOLEAN |
| 77 | +LANGUAGE sql |
| 78 | +SECURITY DEFINER |
| 79 | +SET search_path = public |
| 80 | +STABLE |
| 81 | +AS $$ |
| 82 | + SELECT EXISTS ( |
| 83 | + SELECT 1 FROM public.sessions |
| 84 | + WHERE id = p_session_id |
| 85 | + AND creator_id = p_user_id |
| 86 | + ); |
| 87 | +$$; |
| 88 | + |
| 89 | +GRANT EXECUTE ON FUNCTION public.is_session_creator TO anon, authenticated; |
| 90 | + |
| 91 | +-- Update "Room creators can manage room media sessions" to use helper |
| 92 | +DROP POLICY IF EXISTS "Room creators can manage room media sessions" ON public.media_sessions; |
| 93 | + |
| 94 | +CREATE POLICY "Room creators can manage room media sessions" |
| 95 | + ON public.media_sessions |
| 96 | + FOR ALL |
| 97 | + TO authenticated |
| 98 | + USING ( |
| 99 | + public.is_session_creator(room_id, auth.uid()) |
| 100 | + ) |
| 101 | + WITH CHECK ( |
| 102 | + public.is_session_creator(room_id, auth.uid()) |
| 103 | + ); |
0 commit comments