feat(sessions): material downloads unlock when the session ends

Private materials bucket + RLS read gate (admins/own speakers anytime, all
attendees after the scheduled slot end, event-local Amsterdam time); always
visible uploaded-state, locked download panel, authenticated client downloads,
admin signed URLs. 12 new assertions in material_time_lock_tests.sql.

Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
This commit is contained in:
committed 2026-10-21 18:57:15 +02:00
1 parent 7776145a77
commit a8f3f5b8e1
13 files changed
+334 -30

No files matched your search

@@ -0,0 +1,67 @@
-- =============================================================================
-- Session materials unlock at the scheduled session end.
--
-- Product rule: once a speaker uploads material, attendees must ALWAYS see
-- that material exists — but the download stays locked until the session's
-- slot has ended, so slides are not a distraction mid-session. After the
-- scheduled end everyone (any signed-in attendee, attendance not required —
-- purely time-based) can download. The session's own speakers and admins keep
-- access at any time.
--
-- Enforcement is at the STORAGE layer, not the UI: the `materials` bucket
-- becomes private (the /object/public/ endpoint stops serving files) and the
-- read policy only releases an object to admins, the owning speakers, or after
-- the slot end. Knowing the exact path/URL no longer yields the bytes before
-- the session ends.
-- =============================================================================
-- Unlock = the session's slot has ended, on event-local wall time
-- (APPE events run in Amsterdam; slot_date + end_time are event-local).
create or replace function public.session_material_unlocked(p_session_id uuid)
returns boolean
language sql stable security definer set search_path = '' as $$
select exists (
select 1
from public.sessions s
join public.slots sl on sl.id = s.slot_id
where s.id = p_session_id
and (sl.slot_date + sl.end_time) <= (timezone('Europe/Amsterdam', now()))
);
$$;
-- Storage read gate for the private `materials` bucket. Object paths are
-- `sessions/<session-uuid>/<file>` (see uploadSessionMaterial); anything else
-- in that bucket, and every other bucket, is denied here (other buckets have
-- their own policies).
create or replace function public.can_read_session_material(p_bucket text, p_name text)
returns boolean
language plpgsql stable security definer set search_path = '' as $$
declare
v_session uuid;
begin
if p_bucket is distinct from 'materials' then return false; end if;
if public.is_admin() then return true; end if;
if p_name ~ '^sessions/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/[^/]+$' then
v_session := split_part(p_name, '/', 2)::uuid;
if public.owns_speaker_session(v_session) then return true; end if;
return public.session_material_unlocked(v_session);
end if;
return false;
end;
$$;
grant execute on function public.session_material_unlocked(uuid) to authenticated;
grant execute on function public.can_read_session_material(text, text) to authenticated;
-- Private bucket: kills every public (unauthenticated) material URL.
update storage.buckets set public = false where id = 'materials';
-- Replace the blanket read policy with the time/ownership gate above. The
-- Storage API evaluates this as the requesting user, so it applies both to
-- /object/materials/... fetches and to createSignedUrl issuance.
drop policy if exists "materials_read" on storage.objects;
create policy "materials_read" on storage.objects for select to authenticated
using (public.can_read_session_material(bucket_id, name));
comment on column public.sessions.material_path is
'Storage object path in the private materials bucket; downloads unlock at the scheduled session end (storage RLS), speakers/admins bypass';
+138
View File
@@ -0,0 +1,138 @@
\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;