Skip to content

feat(docs) Add RLS Quickstart prompt - #48710

Closed
czenko wants to merge 4 commits into
masterfrom
docs/rls-ai-prompt
Closed

czenko wants to merge 4 commits into
masterfrom
docs/rls-ai-prompt

Conversation

@czenko

@czenko czenko commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Closes DOCS-1258

image-1785864013861

I have read the CONTRIBUTING.md file.

YES

Problem

The Row Level Security guide documents solid RLS performance and security recommendations, but there's nothing on the page an AI coding agent can be handed directly to make it follow those recommendations when writing or reviewing policies. Framework quickstarts already solve this with a copy-pasteable AiPrompt component — the RLS guide didn't have one.

Solution

Added an <AiPrompt id="row-level-security" /> panel at the top of the RLS guide, using the same pattern as the framework quickstarts. It hands an agent a checklist covering three things:

  • Security — enable RLS, grant least privilege, write one role-scoped policy per operation, and keep cross-table checks in a security definer function in a private schema
  • Performance — wrap helper calls in select, index the columns policies reference, avoid joins inside policies, and mirror the filter in application queries
  • Testing — write pgTAP tests that assert both allow and deny for every role and operation, and run them with supabase test db

Content was built from the doc's own performance section, the security-rls-basics.md/security-rls-performance.md references in supabase/agent-skills, and the RLS module of supabase/supacademy. Where those sources disagreed with the Supabase docs, the docs won.

Verified with evals rather than by inspection: the prompt's generated output was applied to a local stack and exercised with supabase test db. Several rules were corrected so that output runs cleanly, and the final run passes 31/31.

REFERENCE points at three page-level URLs instead of a single section anchor, matching every other entry in ai-prompts.data.ts.

Files changed:

  • apps/docs/data/ai-prompts.data.ts — new row-level-security prompt entry
  • apps/docs/content/guides/database/postgres/row-level-security.mdx — added <AiPrompt id="row-level-security" /> after frontmatter

Manual testing

  1. Open /guides/database/postgres/row-level-security on Preview.
  2. Confirm an "AI Prompt" panel appears at the top of the page, right below the subtitle, styled the same as the AiPrompt panel on e.g. /guides/getting-started/quickstarts/nextjs.
  3. Click "Show more" and confirm the checklist expands and ends with a REFERENCE block listing three .md URLs.
  4. Click the copy icon and paste somewhere to confirm the full prompt text copies correctly.
  5. Run pnpm typecheck --filter=docs and pnpm format and confirm both pass.
  6. Read the prompt and verify that the action it captures is what you would expect.

Summary by CodeRabbit

  • New Features
    • Added AI assistance to the Row Level Security guide.
    • The prompt offers guidance on secure policy design, operation-specific policies, grants, performance optimization, indexing, security-definer functions, application-side filtering, and pgTAP testing.
    • Includes references to recommended Row Level Security, database functions, and testing practices to support safer, more effective database security implementations.

@vercel

vercel Bot commented Aug 4, 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 4, 2026 6:54pm
6 Skipped Deployments
Project Deployment Actions Updated (UTC)
studio Ignored Ignored Aug 4, 2026 6:54pm
design-system Skipped Skipped Aug 4, 2026 6:54pm
studio-self-hosted Skipped Skipped Aug 4, 2026 6:54pm
studio-staging Skipped Skipped Aug 4, 2026 6:54pm
ui-library Skipped Skipped Aug 4, 2026 6:54pm
zone-www-dot-com Skipped Skipped Aug 4, 2026 6:54pm

Request Review

@coderabbitai

coderabbitai Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The Row Level Security guide now invokes a dedicated AI prompt. The prompt covers policy design, performance, grants, application-side filtering, security-definer lookups, and pgTAP testing.

Changes

Row-level security AI prompt

Layer / File(s) Summary
Add RLS prompt guidance
apps/docs/data/ai-prompts.data.ts, apps/docs/content/guides/database/postgres/row-level-security.mdx
The prompt defines secure RLS configuration, performance recommendations, grant checks, application filters, and pgTAP testing. The guide invokes the prompt by identifier.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly identifies the documentation feature: an RLS AI prompt.
Description check ✅ Passed The description explains the problem, solution, testing, changed files, and contributor confirmation, although it uses alternate section headings.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch docs/rls-ai-prompt

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


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.

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Aug 4, 2026
@supabase

supabase Bot commented Aug 4, 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 ↗︎.

@czenko
czenko marked this pull request as ready for review August 4, 2026 17:24
@czenko
czenko requested a review from a team as a code owner August 4, 2026 17:24

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🧹 Nitpick comments (1)
apps/docs/data/ai-prompts.data.ts (1)

187-190: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Target the intended roles, not only anon and authenticated.

Keep an explicit TO clause, but allow the actual role set. PostgreSQL also supports custom role names and explicit PUBLIC. Hard-coding these two Supabase roles can create policies that never apply to a valid custom role. (postgresql.org)

Suggested wording
-   and always scope it with `to authenticated` or `to anon` — never leave
-   the role unspecified.
+   and always scope it with `to <intended role(s)>`. Use `authenticated` or
+   `anon` for the corresponding Supabase Data API roles, and use custom roles
+   when the database schema defines them.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/docs/data/ai-prompts.data.ts` around lines 187 - 190, Update the
policy-writing guidance in the prompt to require an explicit TO clause while
allowing any intended PostgreSQL role, including custom roles and PUBLIC, rather
than restricting it to anon or authenticated. Preserve the existing
per-operation and using/with check guidance.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@apps/docs/data/ai-prompts.data.ts`:
- Around line 212-215: Update the pgTAP testing guidance in the prompt to avoid
presenting tests.rls_enabled() as a base pgTAP function: either remove it and
use only policies_are() and results_eq(), or explicitly require
basejump-supabase_test_helpers before invoking tests.rls_enabled().
- Around line 202-205: Update the SECURITY DEFINER guidance in the prompt text
to require an explicit safe search_path for every helper, using an empty path
with schema-qualified references or a trusted schema followed by pg_temp. Keep
the existing private-schema, revoked-public-execution, and auth.uid()
requirements, and ensure the guidance covers protection against shadowed
unqualified objects.
- Around line 202-205: Update the security-definer guidance in the prompt
content near the RLS helper-function instruction to require granting USAGE on
the private schema and EXECUTE on each helper function to every role that
evaluates the policy after revoking EXECUTE from public. Preserve the existing
requirements for a non-exposed schema and verifying auth.uid() inside the
function.
- Around line 191-198: Update the RLS performance guidance in the prompt around
items 3 and 4: remove the claim that SELECT wrappers only help stable or
immutable functions, and state that uncorrelated scalar subqueries may execute
as InitPlans but must be used only when their result and evaluation frequency
are valid for authorization. Revise the indexing rule to recommend indexing
policy columns only when they lack an existing index, with workload-impact
validation before retaining each index.
- Around line 187-190: Update the operation checklist in the prompt text to
explicitly require checking the necessary SELECT policy when defining UPDATE or
DELETE policies, including read visibility for affected rows alongside USING and
WITH CHECK requirements.

---

Nitpick comments:
In `@apps/docs/data/ai-prompts.data.ts`:
- Around line 187-190: Update the policy-writing guidance in the prompt to
require an explicit TO clause while allowing any intended PostgreSQL role,
including custom roles and PUBLIC, rather than restricting it to anon or
authenticated. Preserve the existing per-operation and using/with check
guidance.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 962cc97e-c5b5-45e6-8e8b-074e9bb6fca6

📥 Commits

Reviewing files that changed from the base of the PR and between 4a9b5a5 and 8a25c87.

📒 Files selected for processing (2)
  • apps/docs/content/guides/database/postgres/row-level-security.mdx
  • apps/docs/data/ai-prompts.data.ts

Comment thread apps/docs/data/ai-prompts.data.ts Resolved
Comment thread apps/docs/data/ai-prompts.data.ts Resolved
Comment thread apps/docs/data/ai-prompts.data.ts Resolved
Comment thread apps/docs/data/ai-prompts.data.ts Resolved
czenko and others added 3 commits August 4, 2026 11:33
Reworks the row-level-security AI prompt after verifying each rule against
the docs and running a head-to-head eval of the old and new text.

- Drop `revoke execute` from the security definer rule. Every function the
  docs revoke EXECUTE on is RPC- or hook-called; the one policy-called
  helper (public.authorize) is deliberately not revoked. Policy expressions
  run with the calling role's privileges, so revoking from `authenticated`
  breaks every query on the table.
- Require `set search_path = ''` on security definer helpers, per
  functions.mdx and the function_search_path_mutable advisor lint.
- Correct the `force row level security` claim: it subjects the table owner
  to RLS but not superusers or `bypassrls` roles, and it silently breaks
  definer helpers that read a forced lookup table as its owner.
- Split the pgTAP helper functions out of the core pgTAP rule.
  tests.rls_enabled() and friends ship with basejump-supabase_test_helpers
  via dbdev, so the previous wording produced test files that failed on
  first run. Plain pgTAP is now the default path.
- Note that a denied update or delete raises nothing rather than 42501,
  since `using` filters the rows out.
- Drop the stable/immutable/volatile wording, unsupported by the docs.
- Point REFERENCE at three page-level URLs instead of a single anchor,
  matching the convention of the other 16 entries.

Co-Authored-By: Claude Opus 5 <[email protected]>
Executing a generated suite against a local stack surfaced this: rule 10
said to "assert that it changed no rows" for a denied update or delete
without saying how, and the natural reading produced invalid SQL.

  ERROR: WITH clause containing a data-modifying statement must be at the
  top level

A data-modifying CTE cannot be nested inside is(), so the generated file
aborted after 15 of 27 assertions. Rule 10 now prescribes lives_ok()
followed by results_eq() on the unchanged row, which avoids CTE placement
entirely. Re-running with that pattern passes 31/31.

Every policy the prompt generated was already correct: anon denial via
42501, member and admin visibility, insert with check, and column-grant
blocking all passed. The defect was only in the assertion shape.

Co-Authored-By: Claude Opus 5 <[email protected]>
Rule 9 said to "begin the file with create extension ..., wrap it in
begin;", which is ambiguous about ordering. A generated file put the
create extension before begin;, so the extension fell outside the
transaction and was never rolled back. Harmless in practice, but the
testing docs put begin; first, so spell the order out explicitly.

Co-Authored-By: Claude Opus 5 <[email protected]>

@nrichers nrichers left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if we actually need to add this prompt to the docs rather than tracking it in evals somewhere? The underlying issue is coding hardness task completion. If the instructions in the docs are off, adding a prompt won't help and if the prompt is right we should be able to trust our docs.

To illustrate, there's a gap between the RLS guide and the prompt I noticed:

  • Rules 1–8 of the prompt restate the guide.
  • Rules 9–11 (the pgTAP testing walkthrough) only appear as an external link in the guide but the prompt actually walks through the steps it seems. Is that essential info? If so, why include only in the prompt and not in the guide?

Note also that AiPrompt is deliberately excluded from the markdown-export pipeline, so adding one as-is likely doesn't actually make a difference. Which gets us back to the question whether these prompts actually belong in the docs.

From the RFC:

"This project should not add agent-facing prompts by default... the relevant gaps will be addressed with clearer guide-local runnable instructions rather than with extra layers outside the guide."

(And my apologies, it slipped by me that the issue mentions "quick start", which draws a similarity to the AI prompts we added to our framework quickstarts recently. Those are for humans. The coding harness issue we're trying to solve is for agents).

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.

2 participants