Files
Convivo/docs/backend/rls.md
T
Lago 4abdc7691b docs: add /docs living documentation per AGENT.MD
- docs/README.md: navigation hub
- docs/architecture.md: system overview, tech stack, data flows, ports
- docs/backend/: supabase-core, schema, rpcs, rls, seed, docker
- docs/frontend/: app-structure, screens, api-client
- docs/next-steps.md: Phase 2/3/4 plan + open decisions
- Fix README demo-account emails to match the actual seed
2026-08-22 17:10:50 +02:00

127 lines
5.8 KiB
Markdown

# Row Level Security (RLS)
RLS is the security boundary. It is **enabled on all 9 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/20260822000000_init.sql`](../../supabase/migrations/20260822000000_init.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
Two `security definer` functions drive role checks (see [rpcs.md](./rpcs.md#role-helper-functions)):
- `is_admin()` — current user's profile has `role = 'admin'`
- `is_staff_or_admin()` — current user's profile has `role in ('staff','admin')`
## Policies by table
| Table | SELECT | INSERT | UPDATE | DELETE |
|---|---|---|---|---|
| `profiles` | own **or** admin | — | admin only | admin only |
| `slots` | all authenticated | admin only | admin only | admin only |
| `sessions` | all authenticated | admin only | admin only | admin only |
| `preferences` | own (`profile_id = auth.uid()`) | — | — | — |
| `connections` | rows you're in (`from_id` or `to_id` = you) | — | — | — |
| `checkins` | own **or** staff/admin | — | — | — |
| `feedback` | own | — | — | — |
| `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 profiles SET points = 9999` or insert its own
check-in. See [rpcs.md](./rpcs.md).
### The exact policies
```sql
-- profiles: read own or admin; write admin only
create policy "profiles_select_own_or_admin" on public.profiles for select to authenticated
using (id = auth.uid() or public.is_admin());
create policy "profiles_update_admin" on public.profiles for update to authenticated
using (public.is_admin()) with check (public.is_admin());
create policy "profiles_delete_admin" on public.profiles for delete to authenticated
using (public.is_admin());
-- slots / sessions: readable by any authenticated user (public agenda); admin writes
create policy "slots_select_all" on public.slots for select to authenticated using (true);
create policy "slots_admin_insert" on public.slots for insert to authenticated with check (public.is_admin());
create policy "slots_admin_update" on public.slots for update to authenticated using (public.is_admin()) with check (public.is_admin());
create policy "slots_admin_delete" on public.slots for delete to authenticated using (public.is_admin());
-- (sessions: identical four policies)
-- preferences: read own
create policy "prefs_select_own" on public.preferences for select to authenticated
using (profile_id = auth.uid());
-- connections: read rows you're involved in
create policy "conn_select_involved" on public.connections for select to authenticated
using (from_id = auth.uid() or to_id = auth.uid());
-- checkins: read own or staff/admin
create policy "checkins_select_own_or_staff" on public.checkins for select to authenticated
using (profile_id = auth.uid() or public.is_staff_or_admin());
-- feedback: read own
create policy "feedback_select_own" on public.feedback for select to authenticated
using (profile_id = auth.uid());
-- leaderboard: readable by any authenticated user
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 (profile_id = auth.uid() or public.is_admin());
```
## 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 <all 9 tables> to anon, authenticated, service_role;
grant insert, update, delete on <all 9 tables> to authenticated, service_role;
grant execute on function <all public RPCs + helpers> 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 profile/prefs/connections/feedback, the full
agenda, and the leaderboard; can only act through the RPCs (which re-check role
internally, e.g. only staff can call `checkin_attendee`).
- **Staff** — everything an attendee can do, plus can read all `checkins` and call
the check-in RPC.
- **Admin** — full read on everything, and the only role with **direct** write
access to `profiles` / `slots` / `sessions` (this is what the future Admin app
in Phase 3 will use).
## Things to revisit later
- `slots` / `sessions` 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).