# 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 (public read) stores session material. Policies on `storage.objects`: ```sql create policy "materials_read" on storage.objects for select to authenticated using (bucket_id = 'materials'); create policy "materials_write" on storage.objects for insert to authenticated with check (bucket_id = 'materials' and public.is_speaker_or_admin()); create policy "materials_update" on storage.objects for update to authenticated using (bucket_id = 'materials' and public.is_speaker_or_admin()); create policy "materials_delete" on storage.objects for delete to authenticated using (bucket_id = 'materials' and public.is_speaker_or_admin()); ``` Speakers + admins can write; 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)). ## 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).