Mirrors agents/plugin from caelum0x/scribase: five new one-task skills covering the most common agent requests, a bumped plugin version, a license and privacy policy URL for directory submission, and a sync of the general scribase skill with the X-launch-post addition it was missing. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
89 lines
5.0 KiB
Markdown
89 lines
5.0 KiB
Markdown
---
|
|
name: scribase-write-rls-policy
|
|
description: 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)
|
|
|
|
```sql
|
|
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
|
|
|
|
1. **RLS enabled in the same file that creates the table.** A table created
|
|
without `enable row level security` right after it is open to everyone
|
|
with the `anon`/`authenticated` grant until someone notices.
|
|
2. **Never `using (true)` (or an always-true expression like `1 = 1`) on a
|
|
table that is not meant to be fully public.** That is "anyone can read
|
|
every row," spelled as a policy.
|
|
3. **`insert`/`update` need `with check`, not just `using`.** `using` alone
|
|
on an `update` lets a user update a row into a shape they could never
|
|
have inserted; `insert` has no `using` at all, only `with check`.
|
|
4. **One policy per command** (`select`, `insert`, `update`, `delete`), not
|
|
one `for all` policy trying to cover every case — `for all` applies the
|
|
same expression to commands that need different ones (e.g. insert has no
|
|
existing row to check `using` against).
|
|
5. **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).
|
|
6. **Index every column a policy filters or joins on** (`user_id` above,
|
|
`team_id` for team-scoped tables) — an unindexed policy column means a
|
|
full table scan on every single query, policy or not.
|
|
7. **Only grant to `anon` when the data is genuinely public.** Granting to
|
|
`anon` by habit, then relying on the policy alone, means one policy bug
|
|
is the only thing between the internet and the table.
|
|
8. **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.
|
|
9. **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 a `team_id` column the
|
|
client could set at insert time without also checking the inserter's own
|
|
membership in `with 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 (`FORCE` removed, `using (true)`, a dropped `with 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.apply`
|
|
only accepts a schema that passed it. `policy.simulate` is 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 call `policy.test` from.
|
|
- **CLI**: `aegis test` / `aegis test --mutate` runs 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.
|