Repository navigation
feat(db): lock down devices RLS behind definer RPCs - #48
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Locks the
devicestable down to deny-all for direct anon/authenticated access and routes all client writes throughSECURITY DEFINERRPCs.Why
Direct
USING(true)policies ondeviceslet any client read/write any device row. This moves every write behind audited definer functions and derivesuser_idfromauth.uid()server-side.Changes
supabase/schema.sql) — drop the three openUSING(true)policies (RLS stays enabled → deny-all for direct access). Addregister_device,update_notify_time, andlink_device_to_userSECURITY DEFINERRPCs as the only client write surface;link_device_to_userderivesuser_idfromauth.uid(). ExplicitlyREVOKEanonfromlink_device_to_user— Supabase default privileges auto-grantEXECUTEtoanon, soREVOKE FROM PUBLICalone is insufficient.notifications/devices.ts) — route device writes through the definer RPCs;linkDeviceToUseris now single-arg (derives user server-side)./api/accountendpoint + tests (writes go through RPCs now).API_URLconfig.docs/.Context
This is the branch that resolves the
linkDeviceToUser0-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
jestdevices + device-registration suites pass (28/28);tsc/eslint clean.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