Spaces:
Runtime error

ai-tester-platform / scripts /fix_rls_policies.sql
BattleFiven's picture
deploy: round 6 save_intermediate light (sha 05316bb)
ed86a97
Raw History Blame Contribute Delete
8.89 kB
-- ============================================================================
-- AI Tester Platform β€” RLS hardening
-- ============================================================================
-- Da eseguire nel Supabase SQL Editor:
-- https://supabase.com/dashboard/project/qcnwwabdftoadsxovpkg/sql/new
--
-- Cosa fa:
-- 1. ENABLE RLS su 19 tabelle attualmente UNRESTRICTED (anon poteva leggere
-- via PostgREST β€” incluso llm_keys_central e user_integration_tokens!).
-- 2. Aggiunge la policy `service_role_full_access_*` su ciascuna, in modo
-- simmetrico alle altre tabelle (bots/personas/scenarios/ecc.).
-- 3. Restringe la policy SELECT di `profiles` da `public` ad `authenticated`
-- (email + role degli utenti non piΓΉ leggibili da anon).
--
-- Effetto sull'app: nessuno. La FastAPI accede al DB via il `postgres`
-- superuser (DATABASE_URL pooler), che bypassa RLS. PostgREST con anon Γ¨
-- l'unico path che viene chiuso.
--
-- Idempotente: usa `DROP POLICY IF EXISTS` + `CREATE POLICY` + `ENABLE RLS`
-- (no-op se giΓ  abilitato). Sicuro da rieseguire.
-- ============================================================================
BEGIN;
-- ── Phase 1: 19 tabelle UNRESTRICTED β†’ RLS ON + service_role full access ────
ALTER TABLE public.audit_events ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.bug_tickets ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.evaluations ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.interactions ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.knowledge_chunks ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.knowledge_crawl_runs ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.knowledge_import_jobs ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.knowledge_pages ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.knowledge_source_assignments ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.knowledge_source_versions ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.knowledge_sources ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.llm_keys_central ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.reports ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.session_knowledge_snapshots ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.subscenarios ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.user_integration_tokens ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.workspace_members ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.workspace_settings ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.workspaces ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS service_role_full_access_audit_events ON public.audit_events;
DROP POLICY IF EXISTS service_role_full_access_bug_tickets ON public.bug_tickets;
DROP POLICY IF EXISTS service_role_full_access_evaluations ON public.evaluations;
DROP POLICY IF EXISTS service_role_full_access_interactions ON public.interactions;
DROP POLICY IF EXISTS service_role_full_access_knowledge_chunks ON public.knowledge_chunks;
DROP POLICY IF EXISTS service_role_full_access_knowledge_crawl_runs ON public.knowledge_crawl_runs;
DROP POLICY IF EXISTS service_role_full_access_knowledge_import_jobs ON public.knowledge_import_jobs;
DROP POLICY IF EXISTS service_role_full_access_knowledge_pages ON public.knowledge_pages;
DROP POLICY IF EXISTS service_role_full_access_knowledge_source_assignments ON public.knowledge_source_assignments;
DROP POLICY IF EXISTS service_role_full_access_knowledge_source_versions ON public.knowledge_source_versions;
DROP POLICY IF EXISTS service_role_full_access_knowledge_sources ON public.knowledge_sources;
DROP POLICY IF EXISTS service_role_full_access_llm_keys_central ON public.llm_keys_central;
DROP POLICY IF EXISTS service_role_full_access_reports ON public.reports;
DROP POLICY IF EXISTS service_role_full_access_session_knowledge_snapshots ON public.session_knowledge_snapshots;
DROP POLICY IF EXISTS service_role_full_access_subscenarios ON public.subscenarios;
DROP POLICY IF EXISTS service_role_full_access_user_integration_tokens ON public.user_integration_tokens;
DROP POLICY IF EXISTS service_role_full_access_workspace_members ON public.workspace_members;
DROP POLICY IF EXISTS service_role_full_access_workspace_settings ON public.workspace_settings;
DROP POLICY IF EXISTS service_role_full_access_workspaces ON public.workspaces;
CREATE POLICY service_role_full_access_audit_events ON public.audit_events FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_bug_tickets ON public.bug_tickets FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_evaluations ON public.evaluations FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_interactions ON public.interactions FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_knowledge_chunks ON public.knowledge_chunks FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_knowledge_crawl_runs ON public.knowledge_crawl_runs FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_knowledge_import_jobs ON public.knowledge_import_jobs FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_knowledge_pages ON public.knowledge_pages FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_knowledge_source_assignments ON public.knowledge_source_assignments FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_knowledge_source_versions ON public.knowledge_source_versions FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_knowledge_sources ON public.knowledge_sources FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_llm_keys_central ON public.llm_keys_central FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_reports ON public.reports FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_session_knowledge_snapshots ON public.session_knowledge_snapshots FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_subscenarios ON public.subscenarios FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_user_integration_tokens ON public.user_integration_tokens FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_workspace_members ON public.workspace_members FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_workspace_settings ON public.workspace_settings FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_role_full_access_workspaces ON public.workspaces FOR ALL TO service_role USING (true) WITH CHECK (true);
-- ── Phase 2: restrict profiles SELECT (public β†’ authenticated) ──────────────
DROP POLICY IF EXISTS "Public profiles are viewable by everyone" ON public.profiles;
DROP POLICY IF EXISTS profiles_select_authenticated ON public.profiles;
CREATE POLICY profiles_select_authenticated
ON public.profiles
FOR SELECT
TO authenticated
USING (true);
-- INSERT/UPDATE policies di profiles le lasciamo invariate (sono giΓ  OK:
-- check auth.uid()=id, e questa volta to authenticated implicito perchΓ©
-- auth.uid() restituisce NULL per anon β†’ check fallisce).
COMMIT;
-- ── Verifica finale ──────────────────────────────────────────────────────────
-- Esegui questa query subito dopo per confermare. Output atteso: 0 righe.
SELECT c.relname AS still_unrestricted
FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE n.nspname = 'public'
AND c.relkind = 'r'
AND c.relrowsecurity = false;