From 55c4ac59492774a5a8d34340be65919db0461d24 Mon Sep 17 00:00:00 2001 From: Jonny Date: Sun, 5 Jul 2026 11:27:27 +0200 Subject: [PATCH] fix: gate auftraege RLS policies on user_role() to close readonly claim gap Security review found two RLS gaps in the auftraege migration that exactly followed the (flawed) plan: - The fahrer UPDATE policy had no public.user_role() = 'fahrer' predicate, so any authenticated user -- including a readonly-role account (the default role for new signups) -- could self-assign any open order via the "status = 'offen'" branch of USING, since all app roles share the same Postgres role and only user_role() distinguishes them. - The disponent/admin INSERT policy did not bind erstellt_von to auth.uid(), allowing a spoofed creator id on insert. Adds public.user_role() = 'fahrer' to both USING and WITH CHECK on the fahrer UPDATE policy, and erstellt_von = auth.uid() to the INSERT policy's WITH CHECK. Column-level lockdown (fahrer changing abholort_id etc.) is intentionally deferred to Task 4's trigger, not addressed here. Verified behaviorally: with the pre-fix policy, a readonly-role user could claim an open order (UPDATE 1); with the fix, the same attempt affects 0 rows. Migration amended in place since it was never applied outside this local dev stack. Co-Authored-By: Claude Sonnet 5 --- .../migrations/20260705090655_auftraege.sql | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/supabase/migrations/20260705090655_auftraege.sql b/supabase/migrations/20260705090655_auftraege.sql index 6272d9e..fa02778 100644 --- a/supabase/migrations/20260705090655_auftraege.sql +++ b/supabase/migrations/20260705090655_auftraege.sql @@ -30,9 +30,12 @@ create policy "Sichtbarkeit von Auftraegen nach Rolle" or status = 'offen' ); +-- erstellt_von = auth.uid() im WITH CHECK verhindert, dass ein Disponent +-- beim Anlegen eine fremde Nutzer-ID als "erstellt_von" unterschiebt +-- (Identitaets-Bindung, nicht nur Rollen-Pruefung). create policy "Disponent und Admin legen Auftraege an" on public.auftraege for insert - with check (public.user_role() in ('admin', 'disponent')); + with check (public.user_role() in ('admin', 'disponent') and erstellt_von = auth.uid()); create policy "Disponent und Admin bearbeiten alle Auftraege" on public.auftraege for update @@ -43,7 +46,15 @@ create policy "Disponent und Admin bearbeiten alle Auftraege" -- offene Zeilen, WITH CHECK erzwingt danach, dass zugewiesen_an entweder -- der eigene Nutzer ist oder wieder auf null gesetzt wird -- letzteres -- deckt den Übergang "zugewiesen -> offen" (Auftrag zuruecklegen) ab). +-- public.user_role() = 'fahrer' ist zwingend notwendig, nicht nur +-- Dokumentation der Absicht: ohne dieses Praedikat wuerde die Policy fuer +-- JEDE eingeloggte Rolle gelten (Postgres-RLS-Policies pruefen nur die +-- USING/WITH-CHECK-Bedingung, nicht welche App-Rolle den Request stellt) -- +-- ein "readonly"-Nutzer (Standardrolle bei Neuanlage, siehe Fundament) +-- koennte sonst ueber die "status = 'offen'"-Klausel offene Auftraege an +-- sich selbst uebernehmen, obwohl readonly laut Rechtematrix nie +-- schreiben darf. create policy "Fahrer uebernimmt offene oder bearbeitet eigene Auftraege" on public.auftraege for update - using (zugewiesen_an = auth.uid() or status = 'offen') - with check (zugewiesen_an = auth.uid() or zugewiesen_an is null); + using (public.user_role() = 'fahrer' and (zugewiesen_an = auth.uid() or status = 'offen')) + with check (public.user_role() = 'fahrer' and (zugewiesen_an = auth.uid() or zugewiesen_an is null));