Chris Aka commited on
Commit
0eb849a
·
1 Parent(s): d028369

fix(security): correctifs CORS + migration RLS multi-tenant

Browse files
app/main.py CHANGED
@@ -53,14 +53,18 @@ app = FastAPI(
53
 
54
  # ══════════════════════════════════════════════════════════════════════════════
55
  # MIDDLEWARE CORS
 
 
 
56
  # ══════════════════════════════════════════════════════════════════════════════
57
 
58
  app.add_middleware(
59
  CORSMiddleware,
60
  allow_origins=settings.cors_origins,
61
  allow_credentials=True,
62
- allow_methods=["*"],
63
- allow_headers=["*", "Authorization"],
 
64
  )
65
 
66
 
 
53
 
54
  # ══════════════════════════════════════════════════════════════════════════════
55
  # MIDDLEWARE CORS
56
+ # SECURITY FIX M1: Restreindre les méthodes et headers autorisés.
57
+ # allow_methods=["*"] et allow_headers=["*"] ouvrent la porte à des requêtes
58
+ # cross-origin non désirées (DELETE depuis n'importe quel origin, etc.)
59
  # ══════════════════════════════════════════════════════════════════════════════
60
 
61
  app.add_middleware(
62
  CORSMiddleware,
63
  allow_origins=settings.cors_origins,
64
  allow_credentials=True,
65
+ allow_methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
66
+ allow_headers=["Content-Type", "Authorization", "X-Requested-With"],
67
+ max_age=3600,
68
  )
69
 
70
 
supabase/migrations/20260321000001_rls_policies.sql ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -- ══════════════════════════════════════════════════════════════════════════════
2
+ -- DEDALYS — Migration RLS (Row Level Security)
3
+ -- SECURITY FIX C5: Isolation multi-tenant par Row Level Security
4
+ -- Date: 2026-03-21
5
+ -- Contexte: Sans ces policies, tout utilisateur authentifié peut lire/écrire
6
+ -- les données de n'importe quel cabinet.
7
+ -- ══════════════════════════════════════════════════════════════════════════════
8
+
9
+ -- ── 1. Activer RLS sur toutes les tables sensibles ────────────────────────────
10
+
11
+ ALTER TABLE espaces ENABLE ROW LEVEL SECURITY;
12
+ ALTER TABLE utilisateurs ENABLE ROW LEVEL SECURITY;
13
+ ALTER TABLE clients ENABLE ROW LEVEL SECURITY;
14
+ ALTER TABLE dossiers ENABLE ROW LEVEL SECURITY;
15
+ ALTER TABLE fichiers ENABLE ROW LEVEL SECURITY;
16
+ ALTER TABLE audiences ENABLE ROW LEVEL SECURITY;
17
+ ALTER TABLE paiements ENABLE ROW LEVEL SECURITY;
18
+ ALTER TABLE factures ENABLE ROW LEVEL SECURITY;
19
+ ALTER TABLE flash_cr ENABLE ROW LEVEL SECURITY;
20
+
21
+ -- ── 2. Fonction helper : espace_id de l'utilisateur connecté ─────────────────
22
+
23
+ CREATE OR REPLACE FUNCTION auth_user_espace_id()
24
+ RETURNS UUID
25
+ LANGUAGE sql
26
+ STABLE
27
+ AS $$
28
+ SELECT espace_id
29
+ FROM utilisateurs
30
+ WHERE id = auth.uid()
31
+ LIMIT 1;
32
+ $$;
33
+
34
+
35
+ -- ── 3. ESPACES : un utilisateur ne voit que son espace ───────────────────────
36
+
37
+ DROP POLICY IF EXISTS "espaces_select_own" ON espaces;
38
+ CREATE POLICY "espaces_select_own"
39
+ ON espaces FOR SELECT
40
+ USING (id = auth_user_espace_id());
41
+
42
+ DROP POLICY IF EXISTS "espaces_update_own" ON espaces;
43
+ CREATE POLICY "espaces_update_own"
44
+ ON espaces FOR UPDATE
45
+ USING (id = auth_user_espace_id());
46
+
47
+
48
+ -- ── 4. UTILISATEURS : accès aux membres du même espace ───────────────────────
49
+
50
+ DROP POLICY IF EXISTS "utilisateurs_select_same_espace" ON utilisateurs;
51
+ CREATE POLICY "utilisateurs_select_same_espace"
52
+ ON utilisateurs FOR SELECT
53
+ USING (espace_id = auth_user_espace_id());
54
+
55
+ -- Un utilisateur peut modifier son propre profil uniquement
56
+ DROP POLICY IF EXISTS "utilisateurs_update_own" ON utilisateurs;
57
+ CREATE POLICY "utilisateurs_update_own"
58
+ ON utilisateurs FOR UPDATE
59
+ USING (id = auth.uid());
60
+
61
+ -- Insertion réservée aux ADMIN du même espace
62
+ DROP POLICY IF EXISTS "utilisateurs_insert_admin" ON utilisateurs;
63
+ CREATE POLICY "utilisateurs_insert_admin"
64
+ ON utilisateurs FOR INSERT
65
+ WITH CHECK (
66
+ espace_id = auth_user_espace_id()
67
+ AND EXISTS (
68
+ SELECT 1 FROM utilisateurs u
69
+ WHERE u.id = auth.uid()
70
+ AND u.role_cabinet = 'associe_gerant'
71
+ )
72
+ );
73
+
74
+
75
+ -- ── 5. CLIENTS ────────────────────────────────────────────────────────────────
76
+
77
+ DROP POLICY IF EXISTS "clients_select_own_espace" ON clients;
78
+ CREATE POLICY "clients_select_own_espace"
79
+ ON clients FOR SELECT
80
+ USING (espace_id = auth_user_espace_id());
81
+
82
+ DROP POLICY IF EXISTS "clients_insert_own_espace" ON clients;
83
+ CREATE POLICY "clients_insert_own_espace"
84
+ ON clients FOR INSERT
85
+ WITH CHECK (espace_id = auth_user_espace_id());
86
+
87
+ DROP POLICY IF EXISTS "clients_update_own_espace" ON clients;
88
+ CREATE POLICY "clients_update_own_espace"
89
+ ON clients FOR UPDATE
90
+ USING (espace_id = auth_user_espace_id());
91
+
92
+ DROP POLICY IF EXISTS "clients_delete_own_espace" ON clients;
93
+ CREATE POLICY "clients_delete_own_espace"
94
+ ON clients FOR DELETE
95
+ USING (espace_id = auth_user_espace_id());
96
+
97
+
98
+ -- ── 6. DOSSIERS ───────────────────────────────────────────────────────────────
99
+
100
+ DROP POLICY IF EXISTS "dossiers_select_own_espace" ON dossiers;
101
+ CREATE POLICY "dossiers_select_own_espace"
102
+ ON dossiers FOR SELECT
103
+ USING (espace_id = auth_user_espace_id());
104
+
105
+ DROP POLICY IF EXISTS "dossiers_insert_own_espace" ON dossiers;
106
+ CREATE POLICY "dossiers_insert_own_espace"
107
+ ON dossiers FOR INSERT
108
+ WITH CHECK (espace_id = auth_user_espace_id());
109
+
110
+ DROP POLICY IF EXISTS "dossiers_update_own_espace" ON dossiers;
111
+ CREATE POLICY "dossiers_update_own_espace"
112
+ ON dossiers FOR UPDATE
113
+ USING (espace_id = auth_user_espace_id());
114
+
115
+ DROP POLICY IF EXISTS "dossiers_delete_own_espace" ON dossiers;
116
+ CREATE POLICY "dossiers_delete_own_espace"
117
+ ON dossiers FOR DELETE
118
+ USING (espace_id = auth_user_espace_id());
119
+
120
+
121
+ -- ── 7. FICHIERS ───────────────────────────────────────────────────────────────
122
+
123
+ DROP POLICY IF EXISTS "fichiers_select_own_espace" ON fichiers;
124
+ CREATE POLICY "fichiers_select_own_espace"
125
+ ON fichiers FOR SELECT
126
+ USING (espace_id = auth_user_espace_id());
127
+
128
+ DROP POLICY IF EXISTS "fichiers_insert_own_espace" ON fichiers;
129
+ CREATE POLICY "fichiers_insert_own_espace"
130
+ ON fichiers FOR INSERT
131
+ WITH CHECK (espace_id = auth_user_espace_id());
132
+
133
+ DROP POLICY IF EXISTS "fichiers_update_own_espace" ON fichiers;
134
+ CREATE POLICY "fichiers_update_own_espace"
135
+ ON fichiers FOR UPDATE
136
+ USING (espace_id = auth_user_espace_id());
137
+
138
+ DROP POLICY IF EXISTS "fichiers_delete_own_espace" ON fichiers;
139
+ CREATE POLICY "fichiers_delete_own_espace"
140
+ ON fichiers FOR DELETE
141
+ USING (espace_id = auth_user_espace_id());
142
+
143
+
144
+ -- ── 8. AUDIENCES ──────────────────────────────────────────────────────────────
145
+
146
+ DROP POLICY IF EXISTS "audiences_select_own_espace" ON audiences;
147
+ CREATE POLICY "audiences_select_own_espace"
148
+ ON audiences FOR SELECT
149
+ USING (espace_id = auth_user_espace_id());
150
+
151
+ DROP POLICY IF EXISTS "audiences_insert_own_espace" ON audiences;
152
+ CREATE POLICY "audiences_insert_own_espace"
153
+ ON audiences FOR INSERT
154
+ WITH CHECK (espace_id = auth_user_espace_id());
155
+
156
+ DROP POLICY IF EXISTS "audiences_update_own_espace" ON audiences;
157
+ CREATE POLICY "audiences_update_own_espace"
158
+ ON audiences FOR UPDATE
159
+ USING (espace_id = auth_user_espace_id());
160
+
161
+ DROP POLICY IF EXISTS "audiences_delete_own_espace" ON audiences;
162
+ CREATE POLICY "audiences_delete_own_espace"
163
+ ON audiences FOR DELETE
164
+ USING (espace_id = auth_user_espace_id());
165
+
166
+
167
+ -- ── 9. PAIEMENTS ──────────────────────────────────────────────────────────────
168
+
169
+ DROP POLICY IF EXISTS "paiements_select_own_espace" ON paiements;
170
+ CREATE POLICY "paiements_select_own_espace"
171
+ ON paiements FOR SELECT
172
+ USING (espace_id = auth_user_espace_id());
173
+
174
+ DROP POLICY IF EXISTS "paiements_insert_own_espace" ON paiements;
175
+ CREATE POLICY "paiements_insert_own_espace"
176
+ ON paiements FOR INSERT
177
+ WITH CHECK (espace_id = auth_user_espace_id());
178
+
179
+ DROP POLICY IF EXISTS "paiements_update_own_espace" ON paiements;
180
+ CREATE POLICY "paiements_update_own_espace"
181
+ ON paiements FOR UPDATE
182
+ USING (espace_id = auth_user_espace_id());
183
+
184
+
185
+ -- ── 10. FLASH_CR ──────────────────────────────────────────────────────────────
186
+
187
+ DROP POLICY IF EXISTS "flash_cr_select_own_espace" ON flash_cr;
188
+ CREATE POLICY "flash_cr_select_own_espace"
189
+ ON flash_cr FOR SELECT
190
+ USING (espace_id = auth_user_espace_id());
191
+
192
+ DROP POLICY IF EXISTS "flash_cr_insert_own_espace" ON flash_cr;
193
+ CREATE POLICY "flash_cr_insert_own_espace"
194
+ ON flash_cr FOR INSERT
195
+ WITH CHECK (espace_id = auth_user_espace_id());
196
+
197
+ DROP POLICY IF EXISTS "flash_cr_update_own_espace" ON flash_cr;
198
+ CREATE POLICY "flash_cr_update_own_espace"
199
+ ON flash_cr FOR UPDATE
200
+ USING (espace_id = auth_user_espace_id());
201
+
202
+
203
+ -- ── 11. Créer la table audit_logs si elle n'existe pas (recommandation H5) ───
204
+
205
+ CREATE TABLE IF NOT EXISTS audit_logs (
206
+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
207
+ espace_id UUID NOT NULL REFERENCES espaces(id) ON DELETE CASCADE,
208
+ utilisateur_id UUID REFERENCES utilisateurs(id) ON DELETE SET NULL,
209
+ action VARCHAR(100) NOT NULL,
210
+ resource_type VARCHAR(50),
211
+ resource_id UUID,
212
+ ip_address VARCHAR(45),
213
+ user_agent TEXT,
214
+ metadata JSONB,
215
+ created_at TIMESTAMPTZ DEFAULT NOW()
216
+ );
217
+
218
+ CREATE INDEX IF NOT EXISTS idx_audit_logs_espace ON audit_logs(espace_id);
219
+ CREATE INDEX IF NOT EXISTS idx_audit_logs_utilisateur ON audit_logs(utilisateur_id);
220
+ CREATE INDEX IF NOT EXISTS idx_audit_logs_created_at ON audit_logs(created_at DESC);
221
+
222
+ ALTER TABLE audit_logs ENABLE ROW LEVEL SECURITY;
223
+
224
+ -- Les audit logs sont en lecture seule pour les utilisateurs (INSERT uniquement via service role)
225
+ DROP POLICY IF EXISTS "audit_logs_select_own" ON audit_logs;
226
+ CREATE POLICY "audit_logs_select_own"
227
+ ON audit_logs FOR SELECT
228
+ USING (espace_id = auth_user_espace_id());
229
+
230
+ -- Note: Les INSERTs dans audit_logs doivent se faire via le service role
231
+ -- (côté backend Python ou route API sécurisée), pas directement par les clients.