Supabase RLS and AI agents: a security review that catches what they miss
On this page
Supabase puts your database one HTTP call from the browser. That's the point, and it's safe when row level security (RLS) does its job. It's also where AI-written code goes wrong most expensively: one key in the wrong file, one policy of true, and another user's rows are public.
Our Supabase preset pairs rules for writing code with a supabase-security-review skill for checking it. Here's what the review looks for, and just as important, what it doesn't flag.
npx presets-dev add supabaseThe keys: what's secret and what isn't
Supabase has two key systems. The legacy anon and service_role JWTs still work until you disable them. The new keys use sb_publishable_... and sb_secret_... prefixes (Supabase docs).
- A secret or
service_rolekey bypasses RLS. In browser code or a committed env file, it's a high finding. - A publishable or
anonkey in the browser is not a finding when RLS is on. It's designed to be public.
Agents get this wrong in both directions. They paste the service key into a client component "to make the query work", and in review they flag the publishable key as a leak. The skill searches the diff for service_role, sb_secret_ and database connection strings, and stays quiet about the publishable key.
RLS policies, by command
Postgres treats each command differently, and the details are where agents (and reviewers) slip (PostgreSQL docs):
| Command | Needs | Common mistake |
|---|---|---|
SELECT | USING | A policy of using (true) on private data |
INSERT | WITH CHECK (can't use USING) | Only USING, which Postgres rejects |
UPDATE | USING and usually WITH CHECK | Flagging a missing WITH CHECK as a bug |
DELETE | USING (can't use WITH CHECK) | Adding WITH CHECK, which Postgres rejects |
That UPDATE row matters. If an UPDATE policy omits WITH CHECK, Postgres reuses the USING expression for the new row. That's valid, and a review that flags it wastes your time. Our skill is told explicitly not to.
A correct owner-only setup:
alter table notes enable row level security;
create policy "Owners read notes" on notes
for select using ((select auth.uid()) = user_id);
create policy "Owners add notes" on notes
for insert with check ((select auth.uid()) = user_id);
create policy "Owners edit notes" on notes
for update using ((select auth.uid()) = user_id);
create policy "Owners delete notes" on notes
for delete using ((select auth.uid()) = user_id);
Note the (select auth.uid()) wrapper. Supabase recommends it because Postgres can cache the result once per statement instead of calling the function on every row (Supabase docs). A bare auth.uid() still works, so the skill prefers the wrapper without calling the bare form a vulnerability.
Never trust the caller's user id
The second most common mistake: the client sends user_id or tenant_id in the request body and the server stores it.
await supabase.from('notes').insert({ body, user_id: req.body.user_id })
Read the user from the session instead, and let the RLS WITH CHECK reject anything else. A stored body field named user_id or tenant_id is a high finding in the review.
Realtime filters aren't security
A Realtime subscription with a filter reduces what the client processes. It doesn't decide what leaves the server. That's RLS or Realtime authorization (Supabase docs). The skill flags a private table with no RLS or Realtime authorization, a token in a broadcast payload, and subscriptions with no cleanup.
Prove it with two users
The skill's last step looks for a test where a second user tries to read or write the first user's row, run against a local database. Missing coverage on a new policy is a medium finding. And the review never applies a migration to production to "see if it works".
Why a skill, not just a rule
The rules tell the agent how to write Supabase code on every edit. The review is a long, step-by-step checklist you want only when checking a change, so it's a skill. Its description names the job (RLS gaps, service-role keys in clients, caller-supplied user ids) and when not to use it. The rules, in turn, are scoped to **/*supabase* and **/*.sql in Cursor and Copilot, so they load only when the agent touches Supabase code. See Pair rules and skills.
Find both halves for your tool: