Skip to content

feat(db): lock down devices RLS behind definer RPCs - #48

Merged
GJanos merged 7 commits into
developfrom
feat/devices-rls-lockdown
Jun 16, 2026
Merged

GJanos merged 7 commits into
developfrom
feat/devices-rls-lockdown

Conversation

@GJanos

@GJanos GJanos commented Jun 16, 2026

Copy link
Copy Markdown
Owner

Locks the devices table down to deny-all for direct anon/authenticated access and routes all client writes through SECURITY DEFINER RPCs.

Why

Direct USING(true) policies on devices let any client read/write any device row. This moves every write behind audited definer functions and derives user_id from auth.uid() server-side.

Changes

  • DB (supabase/schema.sql) — drop the three open USING(true) policies (RLS stays enabled → deny-all for direct access). Add register_device, update_notify_time, and link_device_to_user SECURITY DEFINER RPCs as the only client write surface; link_device_to_user derives user_id from auth.uid(). Explicitly REVOKE anon from link_device_to_user — Supabase default privileges auto-grant EXECUTE to anon, so REVOKE FROM PUBLIC alone is insufficient.
  • App (notifications/devices.ts) — route device writes through the definer RPCs; linkDeviceToUser is now single-arg (derives user server-side).
  • Cron — remove the dead /api/account endpoint + tests (writes go through RPCs now).
  • Cleanup — remove dead API_URL config.
  • Docs — design spec + implementation plan under docs/.

Context

This is the branch that resolves the linkDeviceToUser 0-row / updateNotifyTime "no device row" warnings seen during device testing: the DB lockdown is already live, so the prior build (direct .from('devices').update()) was being rejected. Merging this ships the matching RPC client code.

Verification

⚠️ Recommended before merge

Per CLAUDE.md, RLS/auth changes warrant /security-review — recommend running it on this diff (RLS policy removal + definer RPC grants are exactly where a gap would hide).

🤖 Generated with Claude Code

GJanos and others added 7 commits June 15, 2026 21:54
Drop the three open USING(true)/WITH CHECK(true) policies on devices and
route all client writes through SECURITY DEFINER RPCs (update_notify_time,
link_device_to_user) alongside the existing register_device. Closes anon
token harvesting and blind tampering via the shipped publishable key.
Deletes the now-dead cron/api/account.ts endpoint.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
updateNotifyTime now calls update_notify_time; linkDeviceToUser calls
link_device_to_user (user_id derived from auth.uid(), single-arg). Drops
the last two direct devices-table writes from the client; tests rewritten
to the RPC-based harness.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Drop the three open USING(true) policies on devices (RLS stays enabled ->
deny-all for direct anon/authenticated access). Add update_notify_time and
link_device_to_user SECURITY DEFINER RPCs as the only client write surface;
link derives user_id from auth.uid(). Explicitly revoke anon from
link_device_to_user — Supabase default privileges auto-grant EXECUTE to anon,
so REVOKE FROM PUBLIC alone is insufficient.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The useDeviceRegistration hook test still expected the old (deviceId, userId)
call; user_id is now derived server-side from auth.uid(), so only deviceId is
passed. Session still gates whether linking happens.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Its only consumer was the deleted /api/account endpoint; account ops now go
through the delete_my_account RPC and device writes through definer RPCs, so
API_URL (and the EXPO_PUBLIC_API_URL env var) are no longer read anywhere.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@GJanos
GJanos merged commit 93c98ce into develop Jun 16, 2026
5 checks passed
@GJanos
GJanos deleted the feat/devices-rls-lockdown branch June 16, 2026 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant