Repository navigation
Expand file tree
/
Copy pathsupabase_fixes.sql
More file actions
42 lines (35 loc) · 1.64 KB
/
Copy pathsupabase_fixes.sql
File metadata and controls
42 lines (35 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
-- 1. Enable RLS on analytics_esercizi
ALTER TABLE public.analytics_esercizi ENABLE ROW LEVEL SECURITY;
-- create a policy that allows everyone to read (since it seems to be public analytics)
-- OR if it's user specific, adjust accordingly. Assuming it's for admin/internal use or public stats:
DROP POLICY IF EXISTS "Allow public read access" ON public.analytics_esercizi;
CREATE POLICY "Allow public read access" ON public.analytics_esercizi FOR SELECT USING (true);
-- 2. Fix delete_user function search_path
-- This is a security best practice to prevent search_path hijacking
CREATE OR REPLACE FUNCTION public.delete_user()
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public -- Explicitly set the search_path
AS $$
BEGIN
DELETE FROM auth.users WHERE id = auth.uid();
END;
$$;
-- 3. Optimize user_progress RLS policy
-- The warning is about calling auth.uid() for every row.
-- We can wrap it in a SELECT to make it stable for the query execution.
DROP POLICY IF EXISTS "Users can view own progress" ON public.user_progress;
CREATE POLICY "Users can view own progress"
ON public.user_progress
FOR ALL
USING (
auth.uid() = user_id
);
-- Note: The warning "re-evaluates current_setting() or auth.<function>() for each row"
-- is often fixed by ensuring we compare against a stable value or using a wrapper.
-- Actually, the standard Supabase RLS `auth.uid() = user_id` is usually fine,
-- but to strictly follow the advice for performance "at scale":
-- We can use a simpler comparison if available, but auth.uid() is the standard way.
-- Providing a slightly optimized version if the above still triggers warning:
-- (select auth.uid()) = user_id