/claude-crown:rls-securityfrom Claude Crown
Row Level Security (RLS) in Postgres/Supabase — who may see, create, change and delete an individual ROW. Explains policies from the ground up (USING vs. WITH CHECK, one policy per operation, auth.uid(), auth.jwt()), gives ready-made patterns for owner, team and role based access, shows the performance traps (missing indexes, recursive policies) and the negative tests that make a policy proven rather than merely claimed. ALWAYS use this skill when: a table holding user data is created or changed; the user mentions "RLS", "policy", "row level security", "who can see what", "tenant", "team", "user sees someone else's data", "auth.uid", "unrestricted" or "security hole"; a query unexpectedly returns an empty result; data is read directly from the frontend; one user could see another user's data. Also trigger when multi-user data is only implicit — for example any table with a user_id, employee_id or team_id column.
Ships in Claude Crown. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
RLS answers exactly one question: which rows may this request touch?
Technically, Postgres appends an extra condition to every query automatically:
-- Policy
create policy "read own orders" on orders
for select to authenticated
using ( (select auth.uid()) = created_by );
-- Client asks: select * from orders
-- Postgres runs: select * from orders where auth.uid() = created_by
The client cannot bypass, omit or override that condition. This is the reason a browser is allowed to talk to the database directly at all.
Sources: Supabase — Row Level Security, Postgres — Row security policies
1. enable row level security ← otherwise the table is wide open
2. set/revoke grants ← otherwise the policy never even runs (→ sql-security)
3. one policy per operation ← select / insert / update / delete, separately
4. write a negative test ← otherwise the protection is only a claim
Without step 1, the table is readable by anyone holding the public key. Tables created through the SQL editor or a migration do not have RLS on by default — only those created in the dashboard's table editor do.
alter table orders enable row level security;
This is the most commonly misunderstood detail:
| Clause | Applies to | Question |
|---|---|---|
using (…) | existing rows | which rows may I see or touch? |
with check (…) | new or modified rows | what may the result look like? |
| Operation | Needs |
|---|---|
select | using |
insert | with check |
update | using and with check |
delete | using |
If update has no with check, a user can take their own row and reassign it to someone else
(setting created_by to a foreign ID). The row then becomes invisible to them — but the data
has still been moved.
Four separate policies, the shape almost every table needs:
alter table orders enable row level security;
create policy "orders_select_own"
on orders for select to authenticated
using ( (select auth.uid()) = created_by );
create policy "orders_insert_own"
on orders for insert to authenticated
with check ( (select auth.uid()) = created_by );
create policy "orders_update_own"
on orders for update to authenticated
using ( (select auth.uid()) = created_by )
with check ( (select auth.uid()) = created_by );
create policy "orders_delete_own"
on orders for delete to authenticated
using ( (select auth.uid()) = created_by );
-- Required, otherwise the policy is slow:
create index idx_orders_created_by on orders (created_by);
Three details that are not cosmetic:
(select auth.uid()) rather than auth.uid() — the wrapped form is evaluated once per
query instead of once per row. On a large table the difference is dramatic.to authenticated — restricts the policy to signed-in users. Without it, the policy is
also evaluated for anon, where it can never match.More patterns (team/tenant, roles, publicly readable, soft delete, storage):
references/policy-patterns.md
for all-- ❌ obscures which rule was meant for which operation
create policy "everything" on orders for all to authenticated
using ( auth.uid() = created_by );
With for all, the using expression also acts as with check. Sometimes that is right and
often it is an accident. Four explicit policies are longer and in exchange reviewable: you can
see at a glance whether every operation is covered.
| Mistake | Symptom | Fix |
|---|---|---|
| RLS not enabled | everyone sees everything; "Unrestricted" warning in the dashboard | enable row level security |
grant for anon left in place | a write path exists despite policies | revoke all … from anon (→ sql-security) |
with check missing on update | rows can be reassigned to strangers | set both clauses |
| policy checks a column the client sets | the client sends a foreign user_id | default auth.uid() plus with check |
| policy reads the table it protects | infinite recursion, error 42P17 | use a security definer helper function |
-- ❌ a policy on team_members queries team_members → recursion
create policy "see team" on team_members for select to authenticated
using ( team_id in (select team_id from team_members where user_id = auth.uid()) );
The fix is to move the lookup into a security definer function, which bypasses RLS and
therefore does not trigger the policy again:
create function my_team_ids() returns setof uuid
language sql security definer set search_path = '' stable
as $$ select team_id from public.team_members where user_id = auth.uid(); $$;
create policy "see team" on team_members for select to authenticated
using ( team_id in (select my_team_ids()) );
In full, including how to grant such functions: references/policy-patterns.md
service_role has BYPASSRLS and ignores
every policy. That is why the key never belongs in the client (→ sql-security).alter table … force row level security.-- act as user A (simulated)
select set_config('request.jwt.claims', '{"sub":"<uuid-a>","role":"authenticated"}', true);
set local role authenticated;
select count(*) from orders; -- expected: only their own
The test that matters is always the negative test: user B must not see and not change user A's row. A test that only proves you can see your own data proves nothing about the protection.
The full procedure with pgTAP and supabase test db: references/rls-testing.md. For a first
static pass over the migration files (RLS enabled? four policies? with check on update?
unwrapped auth.uid()?), run the sql-king skill's scripts/sql_scan.py.
Policies run on every query. Three levers:
(select auth.uid()) rather than auth.uid() — evaluated once per query, not per row.to authenticated so the policy is not evaluated for anon at all.Measure with explain (analyze) as a real user, not as an admin — as an admin the policy is
not evaluated at all. Details: references/rls-performance.md
supabase db lint, Security Advisor).references/policy-patterns.md — ready-made policies for owner, team/tenant, roles, publicly
readable, soft delete and storage buckets; plus helper functions against recursion.
Read when writing a policy.references/rls-performance.md — why policies get slow, measuring with EXPLAIN, index
strategy, rewriting slow policies. Read when a query with RLS is slow.references/rls-testing.md — manual tests, pgTAP, supabase test db, the test case
checklist. Read before declaring a table finished.At the end of every RLS task, check:
update policy have both using and with check?auth.uid() wrapped in (select …)?What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub 2fwn2rnh7v-pixel/claude-crown --plugin claude-crownOther plugins' skills for the same job
12 skills in this plugin
/api-guardAudits and hardens API usage in TypeScript web apps: rate limiting and quotas, API keys and paid calls in the backend instead of the frontend, protection against cost explosions, and lowering API consumption for the same result (caching, batching, debouncing, payload trimming, model routing). Script-backed quick scan of existing code. Use ALWAYS when: an API call, fetch(), axios or an SDK client is written; an endpoint, route, Server Action, server function or Edge Function is created; API keys, secrets, tokens, .env, NEXT_PUBLIC_/VITE_ variables appear; an external service is connected (LLM, weather, maps, payment, Supabase, Cloudflare); the user says "Rate Limit", "Quota", "Kosten", "teuer", "Rechnung", "Verbrauch", "zu viele Requests", "429", "API-Key", "Missbrauch", "cost", "usage", "throttle", "caching" or "prüf mal meine API-Nutzung". Also trigger implicitly — when the frontend calls an external service directly, or a search, autocomplete or polling loop is built without anyone mentioning cost.
/java-pioneerExpert playbook for writing production-grade Java (21/25 LTS) backends with Spring Boot 4 and connecting them to web frontends (React, Angular, Vue, plain TypeScript) with maximum safety and performance. Use this skill whenever the user writes, reviews, refactors, debugs, or designs Java code — Spring Boot services, REST APIs, JPA/Hibernate persistence, Spring Security, login/sessions/OAuth2/JWT, CORS or CSRF errors, OpenAPI contracts, TypeScript clients for a Java API, JVM or database performance, Maven/Gradle builds, or Java tests with JUnit/Mockito/Testcontainers — even if the user never says "best practices". Also use it for migrations from older Java versions or Spring Boot 2/3.