From d13994b38687860c2381cdb218aaf631b1f3fabf Mon Sep 17 00:00:00 2001 From: Jonny Date: Sat, 4 Jul 2026 11:58:09 +0200 Subject: [PATCH] feat: add RLS policies and pgTAP tests for profiles --- .../20260704095119_rls_profiles.sql | 38 +++++++++++++++++++ supabase/tests/profiles_rls_test.sql | 33 ++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 supabase/migrations/20260704095119_rls_profiles.sql create mode 100644 supabase/tests/profiles_rls_test.sql diff --git a/supabase/migrations/20260704095119_rls_profiles.sql b/supabase/migrations/20260704095119_rls_profiles.sql new file mode 100644 index 0000000..c7cffe2 --- /dev/null +++ b/supabase/migrations/20260704095119_rls_profiles.sql @@ -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'); diff --git a/supabase/tests/profiles_rls_test.sql b/supabase/tests/profiles_rls_test.sql new file mode 100644 index 0000000..d5240a7 --- /dev/null +++ b/supabase/tests/profiles_rls_test.sql @@ -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;