forked from fahrPool/fahrPool
test: cover profiles UPDATE RLS boundary and privilege-escalation trigger
Final review found the RLS test suite only covered SELECT, never the actual security-relevant path: a non-admin changing a profile's role. Add pgTAP coverage for a fahrer's own-role UPDATE being blocked by RLS (0 rows affected, role unchanged), the positive admin-can-update-others case, and a regression test for handle_new_user() ignoring raw_user_meta_data.role so a future edit reintroducing that read fails loudly instead of quietly reopening the escalation hole. Also add a migration giving the admin UPDATE policy an explicit WITH CHECK (behavior unchanged, since Postgres already falls back to USING for UPDATE policies without one) to document the pattern for future sub-projects copying this RLS setup.
This commit is contained in:
parent
d812db7da3
commit
d03052ca73
|
|
@ -0,0 +1,12 @@
|
||||||
|
-- Ergaenzt eine explizite WITH CHECK-Klausel fuer die bestehende UPDATE-Policy
|
||||||
|
-- "Admins aktualisieren alle Profile" (siehe 20260704095119_rls_profiles.sql).
|
||||||
|
--
|
||||||
|
-- Ohne explizite WITH CHECK faellt Postgres bei UPDATE-Policies auf die
|
||||||
|
-- USING-Klausel zurueck -- das Verhalten war also bereits korrekt (nur
|
||||||
|
-- Admins koennen Zeilen aendern, und zwar nur solche, bei denen
|
||||||
|
-- public.user_role() = 'admin' gilt). Die explizite Klausel aendert daher
|
||||||
|
-- nichts am Verhalten, dokumentiert aber die Absicht fuer kuenftige
|
||||||
|
-- Teilprojekte, die dieses RLS-Muster fuer eigene Tabellen kopieren.
|
||||||
|
alter policy "Admins aktualisieren alle Profile"
|
||||||
|
on public.profiles
|
||||||
|
with check (public.user_role() = 'admin');
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
begin;
|
begin;
|
||||||
select plan(3);
|
select plan(8);
|
||||||
|
|
||||||
insert into auth.users (id, email) values
|
insert into auth.users (id, email) values
|
||||||
('11111111-1111-1111-1111-111111111111', 'admin@test.local'),
|
('11111111-1111-1111-1111-111111111111', 'admin@test.local'),
|
||||||
|
|
@ -17,7 +17,19 @@ select results_eq(
|
||||||
'Fahrer sieht nur das eigene Profil'
|
'Fahrer sieht nur das eigene Profil'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
select is_empty(
|
||||||
|
$$ update public.profiles set role = 'admin' where id = '22222222-2222-2222-2222-222222222222' returning id $$,
|
||||||
|
'Fahrer kann die eigene Rolle nicht per UPDATE aendern (RLS blockt, 0 Zeilen betroffen)'
|
||||||
|
);
|
||||||
|
|
||||||
reset role;
|
reset role;
|
||||||
|
|
||||||
|
select is(
|
||||||
|
(select role from public.profiles where id = '22222222-2222-2222-2222-222222222222'),
|
||||||
|
'fahrer'::public.app_role,
|
||||||
|
'Rolle des Fahrers ist nach dem geblockten Update-Versuch unveraendert'
|
||||||
|
);
|
||||||
|
|
||||||
set local role authenticated;
|
set local role authenticated;
|
||||||
set local request.jwt.claims to '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated"}';
|
set local request.jwt.claims to '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated"}';
|
||||||
|
|
||||||
|
|
@ -29,5 +41,36 @@ select results_eq(
|
||||||
|
|
||||||
select is(public.user_role(), 'admin'::public.app_role, 'public.user_role() liefert die Rolle des eingeloggten Nutzers');
|
select is(public.user_role(), 'admin'::public.app_role, 'public.user_role() liefert die Rolle des eingeloggten Nutzers');
|
||||||
|
|
||||||
|
select results_eq(
|
||||||
|
$$ update public.profiles set role = 'disponent' where id = '22222222-2222-2222-2222-222222222222' returning id $$,
|
||||||
|
$$ values ('22222222-2222-2222-2222-222222222222'::uuid) $$,
|
||||||
|
'Admin kann die Rolle eines anderen Nutzers per UPDATE aendern'
|
||||||
|
);
|
||||||
|
|
||||||
|
reset role;
|
||||||
|
|
||||||
|
select is(
|
||||||
|
(select role from public.profiles where id = '22222222-2222-2222-2222-222222222222'),
|
||||||
|
'disponent'::public.app_role,
|
||||||
|
'Rolle des Fahrers wurde durch das Admin-Update tatsaechlich geaendert'
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Regressionstest fuer Task 2 (siehe 20260704092843_profiles.sql,
|
||||||
|
-- handle_new_user()): Die Rolle eines neuen Profils darf nie aus
|
||||||
|
-- raw_user_meta_data uebernommen werden, auch wenn GoTrue genau dieses JSON
|
||||||
|
-- an auth.users durchreicht -- sonst koennte sich ein Nutzer per Signup-
|
||||||
|
-- Metadaten selbst zum Admin machen (Privilege Escalation).
|
||||||
|
insert into auth.users (id, email, raw_user_meta_data) values (
|
||||||
|
'33333333-3333-3333-3333-333333333333',
|
||||||
|
'escalation@test.local',
|
||||||
|
'{"role": "admin"}'::jsonb
|
||||||
|
);
|
||||||
|
|
||||||
|
select is(
|
||||||
|
(select role from public.profiles where id = '33333333-3333-3333-3333-333333333333'),
|
||||||
|
'readonly'::public.app_role,
|
||||||
|
'Neuer Nutzer bekommt trotz raw_user_meta_data.role=admin immer role=readonly (Privilege-Escalation-Schutz)'
|
||||||
|
);
|
||||||
|
|
||||||
select * from finish();
|
select * from finish();
|
||||||
rollback;
|
rollback;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue