1
0
Fork 0

feat: add RLS policies and pgTAP tests for profiles

This commit is contained in:
Jonny 2026-07-04 11:58:09 +02:00
parent 92adebd710
commit d13994b386
2 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,38 @@
-- Hinweis (Abweichung vom Brief): Seit dem Supabase-Security-Lockdown vom
-- 2025-04-21 ("Restricting Access on Auth, Storage, and Realtime Schemas")
-- besitzt die Migrations-Rolle `postgres` kein CREATE-Recht mehr auf dem
-- Schema `auth` (nur noch USAGE). Ein `create function auth.user_role()`
-- scheitert deshalb mit "permission denied for schema auth" (42501).
-- Die offizielle Supabase-Empfehlung ist, solche Helper-Funktionen im
-- `public`-Schema anzulegen. Die Funktion heisst daher `public.user_role()`
-- statt `auth.user_role()`. Künftige Teilprojekte müssen `public.user_role()`
-- verwenden.
-- Zusaetzliche Abweichung vom Brief: Task 2 hat keine Tabellen-Grants fuer
-- die Rolle `authenticated` auf `public.profiles` gesetzt. RLS-Policies
-- wirken nur zusaetzlich zu den normalen Postgres-Privilegien, nicht
-- anstelle davon -- ohne GRANT bekommt `authenticated` weiterhin
-- "permission denied for table profiles". Deshalb hier ergaenzt.
grant select, update on public.profiles to authenticated;
create or replace function public.user_role()
returns public.app_role
language sql
stable
security definer
set search_path = public
as $$
select role from public.profiles where id = auth.uid();
$$;
create policy "Nutzer sehen ihr eigenes Profil"
on public.profiles for select
using (auth.uid() = id);
create policy "Admins sehen alle Profile"
on public.profiles for select
using (public.user_role() = 'admin');
create policy "Admins aktualisieren alle Profile"
on public.profiles for update
using (public.user_role() = 'admin');

View File

@ -0,0 +1,33 @@
begin;
select plan(3);
insert into auth.users (id, email) values
('11111111-1111-1111-1111-111111111111', 'admin@test.local'),
('22222222-2222-2222-2222-222222222222', 'fahrer@test.local');
update public.profiles set role = 'admin' where id = '11111111-1111-1111-1111-111111111111';
update public.profiles set role = 'fahrer' where id = '22222222-2222-2222-2222-222222222222';
set local role authenticated;
set local request.jwt.claims to '{"sub": "22222222-2222-2222-2222-222222222222", "role": "authenticated"}';
select results_eq(
$$ select id from public.profiles order by id $$,
$$ values ('22222222-2222-2222-2222-222222222222'::uuid) $$,
'Fahrer sieht nur das eigene Profil'
);
reset role;
set local role authenticated;
set local request.jwt.claims to '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated"}';
select results_eq(
$$ select count(*)::int from public.profiles $$,
$$ values (2) $$,
'Admin sieht alle Profile'
);
select is(public.user_role(), 'admin'::public.app_role, 'public.user_role() liefert die Rolle des eingeloggten Nutzers');
select * from finish();
rollback;