# Row Level Security (RLS) RLS is the security boundary. It is **enabled on all 14 tables**, and every policy is scoped to the `authenticated` role. The `anon` role has `GRANT select` at the Postgres level, but RLS blocks it from reading anything — so an unauthenticated request sees no rows. Source of truth: [`supabase/migrations/20260824000000_redesign.sql`](../../supabase/migrations/20260824000000_redesign.sql) (RLS section). ## Which key does the app use? **The anon key — never the service-role (admin) key.** - The app ships only `VITE_SUPABASE_ANON_KEY` (baked in at build time). It's the standard local anon JWT, **public by design** — safe to be in the bundle. - Every request carries the **anon key + the user's session JWT** (from the QR+email login). PostgREST then runs as the `authenticated` role, and **RLS decides what that user can see/do**. - The **service-role key is never in the app**. It bypasses RLS entirely, so it must stay server-side (used only for admin ops / verification). > Model: *the anon key gets you to the door, the session JWT says who you are, > RLS says what you may touch.* ## Role helpers `security definer` functions drive role checks (see [rpcs.md](./rpcs.md#role-helper-functions)): - `current_email()` — `auth.jwt() ->> 'email'` (the identity; `contacts` is keyed by email) - `is_admin()` — current contact has `is_admin = true` - `is_staff_or_admin()` — = `is_admin()` (staff is merged into admin) - `is_speaker_or_admin()` — admin, or a speaker of an active-event session, or a `speaker`-type registration ## Policies by table | Table | SELECT | INSERT | UPDATE | DELETE | |---|---|---|---|---| | `contacts` | own **or** admin | — | admin only | admin only | | `events` | all authenticated | admin only | admin only | admin only | | `registrations` | own **or** admin | admin only | admin only | admin only | | `sponsors` | admin **or** `paid` | admin only | admin only | admin only | | `slots` | all authenticated | admin only | admin only | admin only | | `sessions` | all authenticated | admin only | admin only | admin only | | `session_speakers` | all authenticated | admin only | admin only | admin only | | `preferences` | own | — | — | — | | `connections` | rows you're in **or** admin | — | — | — | | `checkins` | own **or** admin | — | — | — | | `feedback` | own **or** admin | — | — | — | | `leaderboard` | all authenticated | — | — | — | | `points_log` | own **or** admin | — | — | — | **"—" (no policy)** means normal users have **no direct write access** — those tables are written only by the `security definer` RPCs, which bypass RLS. This is deliberate: a client cannot `UPDATE registrations SET points = 9999` or insert its own check-in. See [rpcs.md](./rpcs.md). > **Admin reads the full activity.** The `connections`, `checkins`, and `feedback` > select policies include `or public.is_admin()`, so the APPE Admin console's > Activity view sees every row (an admin is "involved" in none of them, so a > pure "involved/own" policy would show 0). ### The exact policies ```sql -- contacts: read own or admin; write admin only create policy "contacts_select_own_or_admin" on public.contacts for select to authenticated using (email = public.current_email() or public.is_admin()); create policy "contacts_admin_update" on public.contacts for update to authenticated using (public.is_admin()) with check (public.is_admin()); create policy "contacts_admin_delete" on public.contacts for delete to authenticated using (public.is_admin()); -- events: readable by all authenticated; writable by admin create policy "events_select_all" on public.events for select to authenticated using (true); create policy "events_admin_insert" on public.events for insert to authenticated with check (public.is_admin()); create policy "events_admin_update" on public.events for update to authenticated using (public.is_admin()) with check (public.is_admin()); create policy "events_admin_delete" on public.events for delete to authenticated using (public.is_admin()); -- registrations: own or admin create policy "registrations_select_own_or_admin" on public.registrations for select to authenticated using (contact_email = public.current_email() or public.is_admin()); -- (admin-only insert/update/delete) -- sponsors: admin sees all; non-admin sees only paid sponsors create policy "sponsors_select" on public.sponsors for select to authenticated using (public.is_admin() or paid); -- (admin-only insert/update/delete) -- slots / sessions / session_speakers: readable by any authenticated user; admin writes create policy "slots_select_all" on public.slots for select to authenticated using (true); create policy "sessions_select_all" on public.sessions for select to authenticated using (true); create policy "session_speakers_select_all" on public.session_speakers for select to authenticated using (true); -- (each has admin-only insert/update/delete) -- preferences: read own create policy "prefs_select_own" on public.preferences for select to authenticated using (contact_email = public.current_email()); -- connections: read rows you're part of (or all if admin) create policy "conn_select_involved_or_admin" on public.connections for select to authenticated using (from_email = public.current_email() or to_email = public.current_email() or public.is_admin()); -- checkins: read own, or all if admin create policy "checkins_select_own_or_admin" on public.checkins for select to authenticated using (contact_email = public.current_email() or public.is_admin()); -- feedback: read own, or all if admin create policy "feedback_select_own_or_admin" on public.feedback for select to authenticated using (contact_email = public.current_email() or public.is_admin()); -- leaderboard: public to all authenticated (Realtime source) create policy "leaderboard_select_all" on public.leaderboard for select to authenticated using (true); -- points_log: read own or admin create policy "points_log_select_own_or_admin" on public.points_log for select to authenticated using (contact_email = public.current_email() or public.is_admin()); ``` ## Storage — `materials` bucket The `materials` bucket is **private** and stores session material. Downloads unlock when the session's scheduled end has passed (event-local Amsterdam time) — until then only the session's own speakers and admins can read the object, so a known path/URL does not release the bytes early. Policies on `storage.objects`: ```sql create policy "materials_read" on storage.objects for select to authenticated using (public.can_read_session_material(bucket_id, name)); create policy "materials_write" on storage.objects for insert to authenticated with check (bucket_id = 'materials' and public.can_write_session_material(name)); create policy "materials_update" on storage.objects for update to authenticated using (bucket_id = 'materials' and public.can_write_session_material(name)) with check (bucket_id = 'materials' and public.can_write_session_material(name)); create policy "materials_delete" on storage.objects for delete to authenticated using (bucket_id = 'materials' and public.can_write_session_material(name)); ``` `can_read_session_material` (security definer, `20261021180000_material_time_lock.sql`) parses `sessions//` paths, allows `is_admin()` and `owns_speaker_session(uuid)` at any time, everyone else only once `session_material_unlocked(uuid)` holds (slot `slot_date + end_time <= timezone('Europe/Amsterdam', now())`). Anything malformed or in another bucket is denied by this policy (other buckets keep their own policies). Registered speakers can write only their own non-service session paths; admins retain material management access (the admin session editor uses these same policies for its open/download/delete/upload-on-behalf-of-speaker actions). Any authenticated attendee can read (download). Because the `sessions` table is admin-write only, a speaker attaches their uploaded file via the `set_session_material` RPC (see [rpcs.md](./rpcs.md#set_session_materialp_session_id-p_material_path--sessions)). ## Speaker Portal Authorization Source: [speaker portal migration](../../supabase/migrations/20261006175631_speaker_portal.sql), with [staff overlap](../../supabase/migrations/20261007153957_speaker_portal_staff_overlap.sql). `owns_speaker_session(session_id)` requires the JWT's `current_email()` to have both a `session_speakers` assignment and a `registrations.type` of `speaker` or `staff` for that session's event. The slot must belong to the same event and be type `session`, not `service_session`. Assignment alone, registration alone, attendee/sponsor assignments, or another speaker's registration is insufficient. Admin/organizer staff can also speak without changing category or losing staff points/check-in exclusions. There is no separate `organizer` enum value. Automatic staff links to service slots do not grant portal ownership. Admins retain global material management access, but the portal lists only their own talks. Neither event activation, event date nor `checked_in` enters this predicate. `can_write_session_material(path)` accepts speaker paths only in the form `sessions//` and checks the referenced session's ownership. UPDATE checks both old and new names, preventing reassignment to foreign paths. Public material reads are intentionally unchanged. The portal read and material setter are security-definer functions with an empty search path and qualified object names. EXECUTE is revoked from PUBLIC/anon and granted to authenticated/service_role; role membership alone does not grant speaker access. The setter checks ownership again, locks the session row and verifies a non-null object exists in `materials` under the exact session path. It cannot update registrations, points logs, check-ins or leaderboard rows. See [RPCs](./rpcs.md#speaker-portal-rpcs), [transactional tests](./tests.md#speaker-portal) and [frontend entry points](../frontend/screens.md#speaker-portal). The normal app's physical check-in gate remains separate and unchanged. ## GRANTs The new Supabase CLI does **not** auto-expose new tables to the Data API roles, so the migration grants explicitly (RLS still governs what each role can actually see): ```sql grant usage on schema public to anon, authenticated, service_role; grant select on to anon, authenticated, service_role; grant insert, update, delete on to authenticated, service_role; grant execute on function to authenticated; ``` Note `anon` gets `select` but **no** `insert/update/delete`; `authenticated` gets full DML at the Postgres level, but **RLS narrows it** to the policies above. ## Net effect by role - **Attendee** — sees their own contact/registration/prefs/connections/feedback, the full agenda, the public contact directory, and the leaderboard; can only act through the RPCs (which re-check role internally, e.g. only staff can call `checkin_attendee`). - **Staff (= admin)** — everything an attendee can do, plus can read all `checkins` / `connections` / `feedback` and call the check-in RPC. - **Admin** — full read on everything, and the only role with **direct** write access to `contacts` / `events` / `registrations` / `sponsors` / `slots` / `sessions` / `session_speakers`. This is exactly what the APPE Admin console ([../frontend/admin-app.md](../frontend/admin-app.md)) uses. ## Things to revisit later - `slots` / `sessions` / `session_speakers` are readable by *any* authenticated user (fine — it's a public agenda). If you ever want the agenda hidden until the event, that's a one-line policy change. - `leaderboard` exposes every attendee's name + points to any authenticated user. That's the intended product behavior (a public leaderboard).