fix(rbac): scope forced-device policies to channels
Summary
- update
channel_devicesRLS to use request-aware channel-scoped forced-device permissions - require
channel.read_forced_devicesfor direct reads andchannel.manage_forced_devicesfor direct writes - sync
supabase/schemas/prod.sqland add a pgTAP policy regression test
Test plan
git diff --check- GitHub Actions
Run testspassed, including the Supabase pgTAP suite withsupabase/tests/55_test_channel_devices_forced_device_rbac.sql - GitHub Actions
Run Playwright testspassed - SonarCloud, dead-code, CodSpeed, benchmark, Socket, and CodeRabbit checks passed
I could not run the Supabase pgTAP suite locally because this environment does not have bun, supabase, or psql installed. The first CI run exposed a pgTAP compatibility issue in the new policy test; I fixed it and the latest CI run is green.
Summary by CodeRabbit
- Bug Fixes
- Enhanced access control and security for channel device management operations with improved permission validation across the system.
[!WARNING]
Rate limit exceeded
@sellimeneshas exceeded the limit for the number of commits that can be reviewed per hour. Please wait 6 minutes and 30 seconds before requesting another review.Youβve run out of usage credits. Purchase more in the billing tab.
β How to resolve this issue?
After the wait time has elapsed, a review can be triggered using the
@coderabbitai reviewcommand as a PR comment. Alternatively, push new commits to this PR.We recommend that you space out your commits to avoid hitting the rate limit.
π¦ How do rate limits work?
CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.
βΉοΈ Review info
βοΈ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
c6be5f47-5e26-4868-9830-e69dbf347b65π₯ Commits
Reviewing files that changed from the base of the PR and between bad06dec2586cba93b49072cd464c4df82d1d66e and 586f69fdde6895f49ed8d83d7171cd6aa8601790.
π Files selected for processing (4)
supabase/migrations/20260511090000_channel_devices_forced_device_rbac.sqlsupabase/schemas/prod.sqlsupabase/tests/26_test_rls_policies.sqlsupabase/tests/55_test_channel_devices_forced_device_rbac.sql
π Walkthrough
Walkthrough
This PR migrates public.channel_devices RLS policies from legacy check_min_rights authorization to channel-scoped RBAC checks. A new migration defines policies using rbac_check_permission_request with permission functions for forced-device management and reads. The same policies are applied to the schema snapshot. A test validates policy correctness.
Changes
Channel Devices RLS Policy RBAC Authorization
| Layer / File(s) | Summary |
|---|---|
RLS Policy Migration supabase/migrations/20260511090000_channel_devices_forced_device_rbac.sql |
New migration drops existing RLS policies for public.channel_devices and creates DELETE, INSERT, SELECT, and UPDATE policies using rbac_check_permission_request with rbac_perm_channel_manage_forced_devices (writes) and rbac_perm_channel_read_forced_devices (reads) scoped to (owner_org, app_id, channel_id). |
Policy Documentation supabase/migrations/20260511090000_channel_devices_forced_device_rbac.sql |
COMMENT ON POLICY statements document channel-scoped RBAC permission requirements for direct operations, including that UPDATE requires manage permission for both old and new target channels. |
Schema File Synchronization supabase/schemas/prod.sql |
Updates DELETE, INSERT, SELECT, and UPDATE policy expressions and comments in the schema snapshot to match the migration, replacing legacy check_min_rights calls with RBAC authorization checks scoped by (owner_org, app_id, channel_id). |
RLS Policy Test Verification supabase/tests/55_test_channel_devices_forced_device_rbac.sql |
TAP-style test validates five assertions: read policy uses channel.read_forced_devices with channel_id scoping; delete, insert, and both update clauses use channel.manage_forced_devices with channel_id scoping in their respective qual and with_check expressions. |
Estimated code review effort
π― 3 (Moderate) | β±οΈ ~25 minutes
Poem
A rabbit hops through database rows, With RLS policies in rightful flows, From legacy checks to RBAC so bright, Channel-scoped permissions: now read and write, Forced devices managed with test-proven might! π°β¨
π₯ Pre-merge checks | β 5
β Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | β Passed | The title clearly and concisely summarizes the main change: implementing channel-scoped RBAC policies for forced-device access control, which aligns with all three modified files. |
| Docstring Coverage | β Passed | No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. |
| Linked Issues check | β Passed | Check skipped because no linked issues were found for this pull request. |
| Out of Scope Changes check | β Passed | Check skipped because no linked issues were found for this pull request. |
| Description check | β Passed | The pull request description provides a clear summary, detailed test plan with CI validation, and explains local testing limitations. |
βοΈ Tip: You can configure your own custom pre-merge checks in the settings.
β¨ Finishing Touches
π§ͺ Generate unit tests (beta)
- [ ] Create PR with unit tests
[!TIP]
π¬ Introducing Slack Agent: The best way for teams to turn conversations into code.
Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
- Generate code and open pull requests
- Plan features and break down work
- Investigate incidents and troubleshoot customer tickets together
- Automate recurring tasks and respond to alerts with triggers
- Summarize progress and report instantly
Built for teams:
- Shared memory across your entire orgβno repeating context
- Per-thread sandboxes to safely plan and execute work
- Governance built-inβscoped access, auditability, and budget controls
One agent for your entire SDLC. Right inside Slack.
π Get started
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
Merging this PR will not alter performance
β
43 untouched benchmarks
β© 2 skipped benchmarks[^skipped]
Comparing sellimenes:harden-channel-devices-rbac (586f69f) with main (e85eca6)
[^skipped]: 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.
There is a policy-name mismatch that looks security-relevant here. The migration drops "Allow read for auth, api keys (read+)", but the schema snapshot shows the existing channel_devices SELECT policy is named "Allow read for auth (read+)".
If production has the schema-snapshot name, the old broad SELECT policy will not be dropped. PostgreSQL combines permissive RLS policies with OR semantics, so the new channel.read_forced_devices policy can be added while the old check_min_rights('read', get_identity_org_appid(...), ..., NULL::bigint) policy still grants app-scoped reads. That would leave the direct-read path broader than intended.
I would drop both legacy names in the migration and add a regression assertion that no channel_devices policy still references check_min_rights after the migration, e.g. checking pg_policies for tablename = 'channel_devices' and qual/with_check NOT LIKE '%check_min_rights%'.
Addressed in b424d9a.
- The migration now drops both legacy SELECT policy names:
Allow read for auth, api keys (read+)andAllow read for auth (read+). - The recreated SELECT policy now uses the schema-snapshot name
Allow read for auth (read+), so migrated databases and fresh schema state align. - Added a pgTAP regression assertion that no
channel_devicesRLS policy retainscheck_min_rightsinqualorwith_check.
git diff --check passes locally; waiting for the refreshed CI run.
Quality Gate passed
Issues
0 New issues
0 Accepted issues
Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code
I think this still needs a regression that the row's app_id / owner_org must match the submitted channel_id before the policy grants access.
The new channel_devices policies pass the row values into rbac_check_permission_request(..., owner_org, app_id, channel_id), but rbac_check_permission_direct currently resolves p_channel_id back to channels.owner_org/app_id and uses that as the effective permission scope. Since channel_devices only has independent foreign keys to apps(app_id) and channels(id), I don't see a constraint proving those row values describe the same channel.
That means a mismatched row can satisfy channel.manage_forced_devices for channel A while writing a channel_devices record whose app_id points at app B. auto_owner_org_by_app_id only normalizes owner_org from app_id; it doesn't prove the channel belongs to that app. I'd add either a composite FK/trigger enforcing channel_devices.(app_id, owner_org) matches channels.(app_id, owner_org), or an explicit helper/policy check plus a pgTAP regression for mismatched channel/app rows.