\set ON_ERROR_STOP on begin; create function pg_temp.assert_lock(p_condition boolean, p_name text) returns void language plpgsql as $$ begin if p_condition is distinct from true then raise exception 'FAIL: %', p_name; end if; raise notice 'PASS: %', p_name; end; $$; -- Fixtures: one ended and one upcoming event/slot/session (both inactive; the -- real active event is untouched), plus identities. create temporary table lock_fixture as select kind, gen_random_uuid() as event_id, gen_random_uuid() as session_id, (select least(coalesce(min(id), 0), 0) from public.slots) - row_number() over ()::integer as slot_id, case when kind = 'ended' then current_date - 3 else current_date + 3 end as the_date from unnest(array['ended', 'upcoming']) as kind; create temporary table lock_identity as select 'lock-outsider-' || gen_random_uuid() || '@example.invalid' as outsider_email, 'lock-speaker-' || gen_random_uuid() || '@example.invalid' as speaker_email, 'lock-admin-' || gen_random_uuid() || '@appe.at' as admin_email; grant select on lock_fixture, lock_identity to authenticated; insert into public.contacts(email, full_name) select outsider_email, 'Lock Outsider' from lock_identity union all select speaker_email, 'Lock Speaker' from lock_identity union all select admin_email, 'Lock Admin' from lock_identity; update public.contacts set is_admin = true where email = (select admin_email from lock_identity); insert into public.events(id, name, start_date, end_date, is_active) select event_id, 'Lock regression ' || kind, the_date, the_date, false from lock_fixture; insert into public.slots(id, event_id, name, type, slot_date, start_time, end_time) select slot_id, event_id, 'Lock slot', 'session', the_date, '09:00', '10:00' from lock_fixture; insert into public.sessions(id, event_id, slot_id, title) select session_id, event_id, slot_id, 'Lock talk ' || kind from lock_fixture; -- The speaker is assigned (with a speaker registration) to the UPCOMING talk, -- proving the ownership bypass of the time gate. insert into public.registrations(event_id, contact_email, type, points, checked_in) select f.event_id, i.speaker_email, 'speaker', 0, false from lock_fixture f cross join lock_identity i where f.kind = 'upcoming'; insert into public.session_speakers(session_id, contact_email) select f.session_id, i.speaker_email from lock_fixture f cross join lock_identity i where f.kind = 'upcoming'; -- Storage rows (objects) — inserted as superuser; only RLS visibility is -- under test. Also one object in a foreign bucket and one malformed path. insert into storage.buckets(id, name, public) values ('lock-other', 'lock-other', false); create temporary table lock_object as select 'ended'::text as kind, 'materials'::text as bucket_id, ('sessions/' || (select session_id from lock_fixture where kind = 'ended') || '/1729000000000-slides.pdf')::text as name union all select 'upcoming', 'materials', 'sessions/' || (select session_id from lock_fixture where kind = 'upcoming') || '/1729000000000-slides.pdf' union all select 'foreign-bucket', 'lock-other', 'whatever.pdf' union all select 'malformed', 'materials', 'sessions/not-a-uuid/slides.pdf'; insert into storage.objects(bucket_id, name) select bucket_id, name from lock_object; grant select on lock_object to authenticated; select pg_temp.assert_lock( (select not public from storage.buckets where id = 'materials'), 'materials bucket is private'); -- Structural rejections hold regardless of caller (time/ownership not even -- consulted for these shapes). select pg_temp.assert_lock( not public.can_read_session_material('materials', name), 'gate rejects malformed path') from lock_object where kind = 'malformed'; select pg_temp.assert_lock( not public.can_read_session_material('lock-other', 'whatever.pdf') and not public.can_read_session_material(null, null) and not public.can_read_session_material('materials', null) and not public.can_read_session_material('materials', 'sessions/../escape.pdf'), 'gate rejects foreign bucket, null and escape-shaped paths'); -- 1) Outsider: hidden before the end, visible after it. select set_config('request.jwt.claims', json_build_object('email', outsider_email, 'role', 'authenticated')::text, true) from lock_identity; set local role authenticated; select pg_temp.assert_lock( not exists (select 1 from storage.objects o join lock_object l using (bucket_id, name) where l.kind = 'upcoming'), 'outsider cannot read material of an upcoming session'); select pg_temp.assert_lock( exists (select 1 from storage.objects o join lock_object l using (bucket_id, name) where l.kind = 'ended'), 'outsider can read material once the session has ended'); select pg_temp.assert_lock( not exists (select 1 from storage.objects o join lock_object l using (bucket_id, name) where l.kind in ('foreign-bucket', 'malformed')), 'outsider sees neither foreign-bucket nor malformed-path objects'); reset role; -- 2) Assigned speaker of the upcoming session: visible at any time. select set_config('request.jwt.claims', json_build_object('email', speaker_email, 'role', 'authenticated')::text, true) from lock_identity; set local role authenticated; select pg_temp.assert_lock( exists (select 1 from storage.objects o join lock_object l using (bucket_id, name) where l.kind = 'upcoming'), 'assigned speaker reads their upcoming session material before the end'); reset role; -- 3) Admin: visible at any time. select set_config('request.jwt.claims', json_build_object('email', admin_email, 'role', 'authenticated')::text, true) from lock_identity; set local role authenticated; select pg_temp.assert_lock( public.is_admin() and exists (select 1 from storage.objects o join lock_object l using (bucket_id, name) where l.kind = 'upcoming') and exists (select 1 from storage.objects o join lock_object l using (bucket_id, name) where l.kind = 'ended'), 'admin reads any session material at any time'); reset role; -- 4) Unlock boundary follows event-local (Amsterdam) wall time. select pg_temp.assert_lock( public.session_material_unlocked(session_id) = ((the_date + time '10:00') <= (timezone('Europe/Amsterdam', now()))), 'unlock boundary is event-local wall time: ' || kind) from lock_fixture; -- 5) No policy row grants anon the read (storage serves anonymous object -- requests as the anon role, which the policy excludes). select pg_temp.assert_lock( (select count(*) = 0 from pg_policies where schemaname = 'storage' and tablename = 'objects' and policyname = 'materials_read' and 'anon' = any (roles)), 'materials_read does not apply to anon'); -- 6) Active event untouched by the fixtures. select pg_temp.assert_lock( not exists (select 1 from public.events where is_active and name like 'Lock regression %'), 'active event untouched'); update public.contacts set is_admin = false where email = (select admin_email from lock_identity); rollback;