Skip to content

docs(database): correct the RLS guide for failures - #49277

Closed
czenko wants to merge 1 commit into
docs/rls-tests-in-procedurefrom
docs/rls-guide-accuracy
Closed

czenko wants to merge 1 commit into
docs/rls-tests-in-procedurefrom
docs/rls-guide-accuracy

Conversation

@czenko

@czenko czenko commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #49276. Review that first.

Problem

A claim-by-claim check of the restructured guide against the RLS basics and RLS performance skill references, the Supacademy RLS course, and Postgres itself.

The worst finding: the team membership policy never ran. team_id in (select auth.jwt() -> 'app_metadata' -> 'teams') fails at create policy with operator does not exist: uuid = jsonb, because -> returns one jsonb value and in (select …) compares team_id against it.

Three failure modes were missing entirely — recursive policies, materialized views, and how to debug a policy that denies too much. All three are treated as core by both reference sources.

Solution

Corrections

  • Team JWT policy rewritten with jsonb containment.
  • Permissive vs restrictive — permissive policies OR together; a restrictive policy ANDs and only narrows what a permissive policy already granted. as restrictive appeared once, in the MFA example, unexplained.
  • force row level security — table owners skip RLS unless forced, and forcing still doesn't apply to superusers or bypassrls roles. On Supabase postgres isn't a superuser but does have bypassrls.
  • revoke execute on helpers — revoke from public, then grant usage and execute to the role the policy uses. Policy expressions run as the caller, so revoking from that role breaks the policy that calls the function.
  • null = user_id is unknown, not false.
  • Update needs a select policy when it reads the row, which is most updates — not only Data API ones.
  • auth.jwt() returns claims as jsonb, not the token string.
  • Views run with owner rights because security_invoker defaults to false, not because Postgres marks them security definer.
  • Composite indexes — a composite primary key leads with its first column, so a filter on a later column needs its own index. Dropped the claim that Postgres can never use a non-leading column.
  • supabase.auth.update() is not a method. It's updateUser().

New sections

  • Avoid recursive policies — two tables whose policies read each other raise 42P17. A sharing feature produces exactly that shape. Broken with a security definer helper that reads membership as its owner, so the second policy never runs.
  • Materialized views, in Expose a view safely — a materialized view stores its own rows, consults no policy, and Postgres rejects both security_invoker and enable row level security on one.
  • Debug a policy — which role ran the query, pg_policies, comparing a privileged read against the client's, and grants before rewriting anything.

Also

search_path stated as a principle — pin it to a schema callers can't write to — rather than the literal '', which contradicted the event trigger recipe in event-triggers.mdx. Custom claims and RBAC, Storage access control, and Realtime authorization added to Related content.

Verification

Every claim was executed against a local Postgres 17 before being written down.

Claim Result
Published team policy is broken ERROR: operator does not exist: uuid = jsonb, at create policy
Its replacement works Member of a team sees exactly their row; member of none sees zero
Mutually recursive policies fail ERROR: 42P17: infinite recursion detected in policy for relation "lists"
The helper breaks the cycle Member who isn't the owner reads the list; non-member reads nothing
security_invoker on a matview ERROR: unrecognized parameter "security_invoker"
RLS on a matview ERROR: ALTER action ENABLE ROW SECURITY cannot be performed
create policy on a matview ERROR: "mv2" is not a table
A granted matview leaks Non-owner reads 0 rows from the base table, 1 from the matview
search_path = '' resolves pg_catalog format() and current_setting() resolve unqualified

The SQL in Avoid recursive policies was re-extracted from the rendered MDX and run again, so the published blocks are the ones that were tested.

Manual testing

  1. Open the Row Level Security guide on the preview. Avoid recursive policies and Debug a policy appear in the table of contents under RLS reference.
  2. Select the security definer function link inside Avoid recursive policies. It jumps to that section.
  3. Read Expose a view safely. The materialized view guidance follows the Postgres 15 note.
  4. Read the auth.jwt() team policy. It uses jsonb containment and carries for select and to authenticated.

@czenko
czenko requested a review from a team as a code owner August 19, 2026 21:53
@vercel

vercel Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview, v0 Aug 20, 2026 3:14am
6 Skipped Deployments
Project Deployment Actions Updated (UTC)
studio Ignored Ignored Aug 20, 2026 3:14am
design-system Skipped Skipped Aug 20, 2026 3:14am
studio-self-hosted Skipped Skipped Aug 20, 2026 3:14am
studio-staging Skipped Skipped Aug 20, 2026 3:14am
ui-library Skipped Skipped Aug 20, 2026 3:14am
zone-www-dot-com Skipped Skipped Aug 20, 2026 3:14am

Request Review

@supabase

supabase Bot commented Aug 19, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project xguihxuzqibwxjnimxev because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@coderabbitai

coderabbitai Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 29898735-03cf-498b-b829-c065cbb843f9

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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.

…odes

A claim-by-claim check against the supabase-postgres-best-practices skill
references, the Supacademy RLS course, and Postgres itself.

The worst finding: the team membership policy never ran. `team_id in
(select auth.jwt() -> 'app_metadata' -> 'teams')` fails at create policy
with `operator does not exist: uuid = jsonb`, because -> yields one jsonb
value. Replaced with jsonb containment and verified: a member of a team
sees exactly their row, a member of none sees zero.

Also corrected: permissive policies OR together while a restrictive one
ANDs and only narrows what a permissive policy granted; force row level
security and who it does not apply to; revoking execute on helper
functions without breaking the policy that calls them; null = user_id is
unknown, not false; an update that reads the row needs a select policy;
auth.jwt() returns jsonb; views run with owner rights because
security_invoker defaults to false; a composite primary key leads with
its first column; and updateUser() is the method, not update().

Two failure modes the guide never mentioned:

- Materialized views, in Expose a view safely. One stores its own rows,
  so it consults no policy, and Postgres rejects both security_invoker
  and enable row level security on it.
- Debug a policy: which role ran the query, pg_policies, and separating
  a hidden row from an absent one.

Co-Authored-By: Claude Opus 5 <[email protected]>
@czenko
czenko force-pushed the docs/rls-guide-accuracy branch from 563def8 to 894715b Compare August 20, 2026 03:10
@czenko czenko changed the title docs(database): correct the RLS guide and cover three missing failure modes docs(database): correct the RLS guide and cover two missing failure modes Aug 20, 2026
@czenko czenko changed the title docs(database): correct the RLS guide and cover two missing failure modes docs(database): correct the RLS guide for failures Aug 20, 2026
@czenko

czenko commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

I'm going to close this for now. Unsure if I want this as there is a lot of AI specificity that is maybe not necessary.

@czenko czenko closed this Aug 20, 2026
@czenko
czenko deleted the docs/rls-guide-accuracy branch August 20, 2026 19:19
An error occurred while trying to automatically change base from docs/rls-tests-in-procedure to master August 24, 2026 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant