Row Level Security Explained for People Who Never Wrote SQL

· 8 min read ·
vibe-coding security supabase databases

A man in a green sweater acting as doorman with a guest list in front of a filing cabinet with padlocked drawers, while a small robot stamps a visitor's pass

In March 2025, security researcher Matt Palmer inspected a Lovable-built site’s network requests, modified one query, and got all the data in its users table. A scan of Lovable-built projects then found 303 endpoints across 170 of 1,645 projects (about 10.3%) with inadequate RLS (his CVE writeup).

The writeup makes a point most people miss: the problem was not a leaked key. Supabase’s public key is public by design; the tables simply had no row level security, so the public key could read and write everything.

If you build on Supabase, or on Lovable or Bolt with Supabase underneath, this setting is the difference between a private app and a public database.

The model in plain English

Your app has two doors.

The UI door is the one you see. It checks tickets: are you logged in, is this your data, should this button even show. Your AI tool writes these checks happily, and they look convincing.

The database door is the API, and it is public. Anyone can walk up to it directly with your publishable key, which is printed in your JavaScript, and ask for anything. Your UI checks do not exist at this door; they live in your React components, which the API has never seen.

Row level security is the rulebook at that door: per-row rules the database itself enforces on every request, wherever it came from. “Logged in” is a fact about your UI; the database knows nothing about it unless RLS tells it.

Without RLS, a table that the API can reach answers any request holding the publishable key, writes included, so “RLS is off” can mean “people can change my data,” not only “people can see it.”

Check your app in two minutes

Open the SQL editor in your Supabase dashboard and run:

select tablename, rowsecurity
from pg_tables
where schemaname = 'public';

Every f is a table with RLS off. The query checks the public schema; add any other exposed schemas (Settings, Data API) to the where clause. And rowsecurity = t is not the finish line: a policy of using (true) lets everyone in, so read the policies too. The dashboard’s Security Advisor lists the same tables with a fix button. A table meant to be public, like blog posts, should have RLS on plus a public-read policy instead; that opens exactly what you choose, writes included. Why your Supabase table is public has the full audit.

Your first policy, line by line

Say you have a notes table with a user_id column. Two statements protect it:

alter table public.notes enable row level security;
create policy "notes are readable by their owner"
on public.notes for select
to authenticated
using ( (select auth.uid()) = user_id );

Line by line:

  • enable row level security flips the table to default deny: the API returns nothing for this table until a policy says otherwise. If your app visibly breaks here, that is the feature: nothing is silently open.
  • for select scopes the policy to reads. Writes need their own policies, which is deliberate: reading your notes and creating notes are different decisions.
  • to authenticated means this policy applies to signed-in users only.
  • auth.uid() is who is asking, taken from their login token. For a logged-out visitor it is null, and null never equals a user_id, so the policy fails closed. Never add an or auth.uid() is null branch to “make the public case work”; that hands every row to every stranger. The other common AI slip is using (user_id = user_id), which is always true and protects nothing.
  • The extra (select ...) around auth.uid() lets Postgres compute it once per query instead of once per row. On small tables you will never notice. On large ones you will.

Add the matching write policies (insert needs with check, update needs both using and with check, delete needs using) and the table is genuinely yours. The update one, since both halves trip people up:

create policy "owners update their own notes" on public.notes
for update to authenticated
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id );

The using half decides which rows you may touch; the with check half decides what the row may look like after. Without the second, you could move a row to someone else’s user_id.

On a current Supabase project the table also needs grant select, insert, update, delete on public.notes to authenticated; and no grant for anon, so strangers are refused before any policy runs.

The five ways it still goes wrong

1. “A policy exists” is not “you are safe.” In May 2025, Palmer’s writeup called out the false comfort of Lovable’s scanner checking whether some policy exists, not whether it is correct. Today’s Lovable checks go further and auto-fix common RLS mistakes, but a select policy of using ( true ) still passes “a policy exists” and makes the table public. Read the policies a scan passes; each should answer “who can do what to which rows” in one sentence.

2. Grants are a separate gate, and the failure flipped in 2026. Policies decide which rows; grants decide whether a role can touch the table at all. Supabase used to grant everything on new tables to everyone; per its changelog, that stopped for new projects from 30 May 2026 and for all existing projects from 30 October 2026, so new tables need an explicit grant. So the error you meet now is 42501. Read the message, not just the code: permission denied for table is a grant problem, while new row violates row-level security policy is a policy problem, and both come back as 42501. The bug to fear is the AI “fixing” it by granting to anon as well. Read the grants you did not write. The one-time fix so you never have to remember: Supabase documents an event trigger that enables RLS on every new table, plus a revoke-default-privileges step for projects that still auto-grant. Dashboard-created tables get RLS on by default; tables the AI creates through SQL migrations usually do not.

3. Views ignore RLS by default. A view runs as its creator, so a view over a protected table hands out the rows the policies withheld. If your AI created views, check them. The fix is security_invoker (Postgres 15 or newer; Supabase projects are): alter view public.notes_view set (security_invoker = true); The Supabase RLS docs cover it.

4. The secret key bypasses everything. service_role and sb_secret_... keys skip RLS entirely. They exist for your server; in browser code, no policy will save you. See how to find and rotate leaked keys.

5. A policy that queries its own table recurses. A policy on profiles that SELECTs from profiles to check admin status makes Postgres answer infinite recursion detected in policy for relation "profiles", and every request to the table fails for everyone. The fix is a security definer function for the lookup. If your AI writes a policy that reads the table it protects, rewrite it before debugging anything else.

Prove it from the outside

Policies are claims. The test is what the internet gets.

Insert one test row in the dashboard’s table editor first, because an empty answer proves nothing about an empty table. Then log out and ask for the table with your publishable key, as in the 20-minute security check:

Terminal window
curl "https://YOUR_PROJECT.supabase.co/rest/v1/notes?select=*&limit=3" \
-H "apikey: YOUR_PUBLISHABLE_KEY"

Your test row in the answer means the table is open. A locked table answers [] when its grants are in place, or since the 2026 grants change a 42501 permission-denied error when they are not; both mean the stranger got nothing. A 401 means your request is wrong, not that you are safe: Supabase says publishable keys go on the apikey header, never Authorization: Bearer. Delete the test row when you are done.

How do I test row level security automatically?

With a pgTAP suite that goes red the moment a stranger can read or write your rows: strangers refused, signed-in users limited to their own data, no editing or forging someone else’s. The full walkthrough, with the suite file to download and a failing run before the fix, is in Test Your Supabase RLS Policies Before Your Users Do.

A prompt to start with

For every table in the public schema, enable row level security and write policies that limit each signed-in user to their own rows. Explain each policy in one sentence. Flag any table you believe must be publicly readable or writable, and ask me before making it so. Then tell me how to verify each policy from outside the app.

The one check an engineer does by hand

Rerun the two-minute pg_tables query after every new table the AI creates. AI tools add tables mid-prompt without mentioning it.

Using a provider for sign-in does not remove this job (auth for vibe coders). RLS is the fix for most of what the 20-minute security check finds, and one of the six things that break first in what breaks when your app gets real users.

RLS does not cover leaks through caches and shared variables. See why users see someone else’s data.

The Get AI Native ebook goes deeper on this. It is free, and the expected launch is around Christmas. Join the waitlist on the homepage.

Comments