forked from fahrPool/fahrPool
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 <noreply@anthropic.com>
This commit is contained in:
parent
b576c4e3b1
commit
55c4ac5949
|
|
@ -30,9 +30,12 @@ create policy "Sichtbarkeit von Auftraegen nach Rolle"
|
||||||
or status = 'offen'
|
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"
|
create policy "Disponent und Admin legen Auftraege an"
|
||||||
on public.auftraege for insert
|
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"
|
create policy "Disponent und Admin bearbeiten alle Auftraege"
|
||||||
on public.auftraege for update
|
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
|
-- offene Zeilen, WITH CHECK erzwingt danach, dass zugewiesen_an entweder
|
||||||
-- der eigene Nutzer ist oder wieder auf null gesetzt wird -- letzteres
|
-- der eigene Nutzer ist oder wieder auf null gesetzt wird -- letzteres
|
||||||
-- deckt den Übergang "zugewiesen -> offen" (Auftrag zuruecklegen) ab).
|
-- 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"
|
create policy "Fahrer uebernimmt offene oder bearbeitet eigene Auftraege"
|
||||||
on public.auftraege for update
|
on public.auftraege for update
|
||||||
using (zugewiesen_an = auth.uid() or status = 'offen')
|
using (public.user_role() = 'fahrer' and (zugewiesen_an = auth.uid() or status = 'offen'))
|
||||||
with check (zugewiesen_an = auth.uid() or zugewiesen_an is null);
|
with check (public.user_role() = 'fahrer' and (zugewiesen_an = auth.uid() or zugewiesen_an is null));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue