espace-paie-odentas/scripts/fix-rls-organization-details.sql
2025-10-17 13:02:39 +02:00

74 lines
2.3 KiB
SQL
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- ==========================================
-- FIX CRITIQUE : RLS sur organization_details
-- ==========================================
-- PROBLÈME: RLS désactivé sur table contenant IBAN, BIC, emails, SIRET
-- IMPACT: Exposition de 40+ colonnes de données bancaires et personnelles
-- PRIORITÉ: CRITIQUE (violation RGPD)
-- ==========================================
-- 1⃣ ACTIVER RLS
ALTER TABLE organization_details ENABLE ROW LEVEL SECURITY;
-- 2⃣ POLITIQUE SELECT : Les membres peuvent voir leur organisation
CREATE POLICY "select_by_org_or_staff"
ON organization_details
FOR SELECT
USING (is_member_of_org(org_id));
-- 3⃣ POLITIQUE UPDATE : Les membres peuvent modifier leur organisation
-- Note: Dans le code, org_id est exclu des UPDATE via .update({ ...orgData, org_id: undefined })
CREATE POLICY "update_by_org_or_staff"
ON organization_details
FOR UPDATE
USING (is_member_of_org(org_id))
WITH CHECK (is_member_of_org(org_id));
-- 4⃣ POLITIQUE INSERT : Les membres peuvent créer leur organisation
-- Note: Utilisé lors de la création initiale d'une organisation
CREATE POLICY "insert_by_org_or_staff"
ON organization_details
FOR INSERT
WITH CHECK (is_member_of_org(org_id));
-- 5⃣ POLITIQUE DELETE : Protection supplémentaire (normalement pas utilisé)
CREATE POLICY "delete_by_org_or_staff"
ON organization_details
FOR DELETE
USING (is_member_of_org(org_id));
-- ==========================================
-- VÉRIFICATION POST-FIX
-- ==========================================
-- Vérifier que RLS est activé
SELECT
tablename,
rowsecurity AS rls_enabled
FROM pg_tables
WHERE tablename = 'organization_details';
-- Vérifier les politiques créées (devrait retourner 4 lignes)
SELECT
policyname,
cmd,
qual,
with_check
FROM pg_policies
WHERE tablename = 'organization_details'
ORDER BY cmd;
-- Vérifier les index existants (devrait retourner 3 index sur org_id)
SELECT
indexname,
indexdef
FROM pg_indexes
WHERE tablename = 'organization_details'
AND indexdef ILIKE '%org_id%';
-- ==========================================
-- RÉSULTAT ATTENDU
-- ==========================================
-- ✅ RLS activé: true
-- ✅ 4 politiques créées: SELECT, INSERT, UPDATE, DELETE
-- ✅ 3 index existants sur org_id (performance garantie)
-- ==========================================