MCP server (api.scribase.com) plus the scribase skills for creating a project, adding auth/storage, writing a safe RLS policy, branch-per-PR and the preview-branch-and-confirm workflow. Content ported as-is from the live Scribase agents plugin (rithub.dev/scribase/scribase-agents), packaged with a .grok-plugin/plugin.json manifest for Grok Build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5.0 KiB
5.0 KiB
name, description
| name | description |
|---|---|
| scribase-write-rls-policy | Write a correct, safe row-level-security (RLS) policy for a Postgres/Scribase table — the exact shape that keeps one user out of another user's rows — and check it before it ships. Use whenever the user asks to write, review, fix, or add an RLS policy, secure a table, lock down who can read or write rows, or when creating any new table that will hold user data. |
Scribase: write a safe RLS policy
A table with RLS disabled, or a policy that is wrong in one of the ways below, is a real data leak, not a style issue. Every table that holds data scoped to a user, team or tenant needs RLS enabled and a policy per command in the same migration that creates the table.
The shape (copy this, then change the column names)
create table public.todos (
id bigint generated always as identity primary key,
user_id uuid not null default auth.uid() references auth.users (id) on delete cascade,
title text not null check (length(title) between 1 and 500),
created_at timestamptz not null default now()
);
create index todos_user_id_idx on public.todos (user_id); -- every column a policy filters on
alter table public.todos enable row level security; -- same migration, never a follow-up
create policy "todos: owner reads" on public.todos for select to authenticated using (user_id = (select auth.uid()));
create policy "todos: owner inserts" on public.todos for insert to authenticated with check (user_id = (select auth.uid()));
create policy "todos: owner updates" on public.todos for update to authenticated
using (user_id = (select auth.uid())) with check (user_id = (select auth.uid()));
create policy "todos: owner deletes" on public.todos for delete to authenticated using (user_id = (select auth.uid()));
grant select, insert, update, delete on public.todos to authenticated;
Rules, in order of how often they cause a real leak
- RLS enabled in the same file that creates the table. A table created
without
enable row level securityright after it is open to everyone with theanon/authenticatedgrant until someone notices. - Never
using (true)(or an always-true expression like1 = 1) on a table that is not meant to be fully public. That is "anyone can read every row," spelled as a policy. insert/updateneedwith check, not justusing.usingalone on anupdatelets a user update a row into a shape they could never have inserted;inserthas nousingat all, onlywith check.- One policy per command (
select,insert,update,delete), not onefor allpolicy trying to cover every case —for allapplies the same expression to commands that need different ones (e.g. insert has no existing row to checkusingagainst). - Wrap
auth.uid()/auth.jwt()in(select ...)so Postgres evaluates it once per statement instead of once per row (a real, measurable speed difference on large tables, not just style). - Index every column a policy filters or joins on (
user_idabove,team_idfor team-scoped tables) — an unindexed policy column means a full table scan on every single query, policy or not. - Only grant to
anonwhen the data is genuinely public. Granting toanonby habit, then relying on the policy alone, means one policy bug is the only thing between the internet and the table. - A view over a table with RLS needs
security_invoker = true(create view ... with (security_invoker = true) as ...), or it runs as the view owner and silently bypasses the base table's policies. - Team/tenant-scoped tables: filter on a membership table
(
exists (select 1 from memberships m where m.team_id = teams.id and m.user_id = (select auth.uid()))), never on ateam_idcolumn the client could set at insert time without also checking the inserter's own membership inwith check.
Check it before it ships
- MCP
policy_test(full schema source): applies the schema to a scratch branch, runs the cross-user isolation matrix, then deliberately breaks each guarantee (FORCEremoved,using (true), a droppedwith check) on throwaway branches and requires every break to be caught. A pass returns an apply token; a failure lists every leak or missed mutant.schema_applyonly accepts a schema that passed it.policy_simulateis the quicker, non-mutating version of the same check. - The online checker at
scribase.com/tools/rls-checker: paste the schema, get every finding above with the exact fix SQL — useful when there is no MCP session to callpolicy_testfrom. - CLI:
aegis test/aegis test --mutateruns the same mutation harness against a preview from the terminal.
A single leak or a surviving mutation is a failure: fix the policy and
re-run, do not ship with a known leak "to fix later." For the full
preview-branch-and-confirm flow around a production schema change, use the
scribase-safe-change skill; this skill is the policy-writing step inside
it.