Skip to content

fix(rbac): allow app insert returning#1774

Closed
Dalanir wants to merge 3 commits into
mainfrom
rbac-cli-app-init
Closed

fix(rbac): allow app insert returning#1774
Dalanir wants to merge 3 commits into
mainfrom
rbac-cli-app-init

Conversation

@Dalanir
Copy link
Copy Markdown
Contributor

@Dalanir Dalanir commented Mar 10, 2026

Summary (AI generated)

  • Fix public.apps RLS so RBAC users can create an app through INSERT ... RETURNING
  • Add a migration that broadens app read access to org read OR app read
  • Add a pgTAP regression test for RBAC app creation via direct SQL/SDK flow

Motivation (AI generated)

RBAC organizations could fail CLI onboarding when the app creation step used a direct insert with RETURNING. The insert check passed, but the row could not be read back in the same statement snapshot, causing a generic RLS error.

Business Impact (AI generated)

This restores CLI onboarding for RBAC customers who need to create their first app, reducing support load and avoiding drop-off during activation.

Test Plan (AI generated)

  • Reproduce the RBAC failure locally with direct SQL INSERT ... RETURNING
  • Apply the migration locally and verify the same repro succeeds
  • Run bunx supabase test db --local supabase/tests/00-supabase_test_helpers.sql supabase/tests/45_test_apps_insert_returning_rbac.sql
  • Run the broader backend suite if needed

Generated with AI

Summary by CodeRabbit

  • Bug Fixes

    • Updated read access rules so newly created apps are visible immediately after insertion while preserving org- and app-level access controls.
  • Tests

    • Added end-to-end RBAC test that validates INSERT ... RETURNING visibility and access behavior in an authenticated session.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 10, 2026

Warning

Rate limit exceeded

@Dalanir has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 7 minutes and 55 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command 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: 5c29487b-5cfc-440c-821c-e21d5fb06961

📥 Commits

Reviewing files that changed from the base of the PR and between 75e09ac and f4bd9a9.

📒 Files selected for processing (2)
  • supabase/migrations/20260310103000_fix_apps_select_rls_returning.sql
  • supabase/tests/45_test_apps_insert_returning_rbac.sql
📝 Walkthrough

Walkthrough

This PR updates the SELECT RLS policy on public.apps to use public.check_min_rights (with an auth-EXISTS branch and an app-scoped branch using public.get_identity_org_appid) combined via OR to fix INSERT...RETURNING visibility, and adds a SQL test that validates INSERT ... RETURNING RBAC behavior in-session.

Changes

Cohort / File(s) Summary
RLS Policy Migration
supabase/migrations/20260310103000_fix_apps_select_rls_returning.sql
Drops and recreates the "Allow for auth, api keys (read+)" SELECT policy on public.apps. New USING clause grants SELECT when either an authenticated uid passes public.check_min_rights for org-scoped read, or public.check_min_rights succeeds for an app-scoped identity resolved via public.get_identity_org_appid(..., owner_org, app_id).
RBAC Test
supabase/tests/45_test_apps_insert_returning_rbac.sql
Adds test that starts a transaction, creates an org, sets test config, authenticates a user, performs INSERT ... RETURNING into public.apps, and asserts visibility of the inserted app_id within the same session.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 In burrows of SQL I hop and sing,
Policies joined with an OR-wing,
INSERT returns now see the light,
Auth and keys both get it right. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: fixing RBAC to allow app insertion with RETURNING, which is the core issue and solution described in the PR.
Description check ✅ Passed The PR description covers summary, motivation, and business impact, but lacks explicit sections for test plan details and testing steps matching the template structure.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch rbac-cli-app-init

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
supabase/migrations/20260310103000_fix_apps_select_rls_returning.sql (1)

36-47: OR condition has performance implications but is justified here.

The coding guidelines recommend avoiding OR conditions that hurt query performance. However, this approach is reasonable given:

  1. Short-circuit evaluation means only one branch typically evaluates fully
  2. The alternative (two policies) would have the same OR semantics implicitly
  3. The fix is necessary for INSERT...RETURNING visibility

Consider documenting this tradeoff in the migration comment block for future maintainers.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@supabase/migrations/20260310103000_fix_apps_select_rls_returning.sql` around
lines 36 - 47, Add a brief migration comment above the OR condition containing
public.check_min_rights(...) / public.get_identity_org_appid(...) that explains
the performance tradeoff: note that OR conditions can hurt query performance but
are justified here because short-circuit evaluation limits work, splitting into
two policies would preserve the same semantics, and this form is required to fix
INSERT...RETURNING visibility; mention that this is intentional to aid future
maintainers.
supabase/tests/45_test_apps_insert_returning_rbac.sql (1)

29-50: Consider adding a test for app-only permission holders.

This test validates org-level super_admin access (branch 1 of the new policy). Adding a companion test for a user with only app-scoped permissions would ensure branch 2 continues working correctly, providing full coverage of both OR branches.

Would you like me to draft an additional test case for a user with only app-level read permissions?

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@supabase/tests/45_test_apps_insert_returning_rbac.sql` around lines 29 - 50,
Add a companion test alongside the existing SELECT lives_ok block that verifies
the INSERT ... RETURNING path for a user/session that only has app-scoped
permissions: duplicate the current INSERT INTO public.apps ... RETURNING app_id
test but change the session context to the app-scoped principal (use a different
current_setting key such as
current_setting('test.apps_returning_rbac_app_holder')::uuid or the session
setup used elsewhere for app-only tests), and update the assertion message to
something like 'RBAC app-scoped permission holder can create an app with INSERT
... RETURNING' so branch 2 of the policy is covered; ensure the new test runs
under a role that has only app-level permissions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@supabase/migrations/20260310103000_fix_apps_select_rls_returning.sql`:
- Around line 26-35: The org-scoped branch currently calls
get_identity_org_allowed which ignores limited_to_apps and lets RBAC org-level
keys read all apps; change the branch to use get_identity_org_appid so app
restrictions are enforced (i.e., call
get_identity_org_appid('{read,upload,write,all}'::public.key_mode[], owner_org,
app_id_or_self) and pass the relevant app_id context), and ensure
check_min_rights is invoked with a non-NULL app_id when the row has an app_id so
limited_to_apps is applied; if INSERT...RETURNING caused the original
self-reference problem, refactor the policy to resolve the self-reference (e.g.,
compute the app_id via a stable function or use NEW.app_id in a safe way) rather
than bypassing limited_to_apps.

---

Nitpick comments:
In `@supabase/migrations/20260310103000_fix_apps_select_rls_returning.sql`:
- Around line 36-47: Add a brief migration comment above the OR condition
containing public.check_min_rights(...) / public.get_identity_org_appid(...)
that explains the performance tradeoff: note that OR conditions can hurt query
performance but are justified here because short-circuit evaluation limits work,
splitting into two policies would preserve the same semantics, and this form is
required to fix INSERT...RETURNING visibility; mention that this is intentional
to aid future maintainers.

In `@supabase/tests/45_test_apps_insert_returning_rbac.sql`:
- Around line 29-50: Add a companion test alongside the existing SELECT lives_ok
block that verifies the INSERT ... RETURNING path for a user/session that only
has app-scoped permissions: duplicate the current INSERT INTO public.apps ...
RETURNING app_id test but change the session context to the app-scoped principal
(use a different current_setting key such as
current_setting('test.apps_returning_rbac_app_holder')::uuid or the session
setup used elsewhere for app-only tests), and update the assertion message to
something like 'RBAC app-scoped permission holder can create an app with INSERT
... RETURNING' so branch 2 of the policy is covered; ensure the new test runs
under a role that has only app-level permissions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 23aedcf0-0e54-4208-9064-b868b1ac1818

📥 Commits

Reviewing files that changed from the base of the PR and between 2ec420e and c7b44bf.

📒 Files selected for processing (2)
  • supabase/migrations/20260310103000_fix_apps_select_rls_returning.sql
  • supabase/tests/45_test_apps_insert_returning_rbac.sql

Comment thread supabase/migrations/20260310103000_fix_apps_select_rls_returning.sql Outdated
@sonarqubecloud
Copy link
Copy Markdown

@Dalanir Dalanir closed this Mar 11, 2026
@Dalanir Dalanir deleted the rbac-cli-app-init branch March 11, 2026 14:31
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