🔧 Unbreak My AppAll fixesError codesFix KitsSponsor

Fixes › Database / Supabase RLS

Supabase RLS is on but every user can read every row: fix USING (true) and auth.uid() is not null policies

LovableBoltCursorReplitv0

Published

Weakness type: CWE-863 (Incorrect Authorization)

The error

Row Level Security shows as enabled, but a logged-in user (or a logged-out visitor with the anon key) can still read or edit other people's rows. The policies say USING (true), TO authenticated USING (true), USING (auth.uid() IS NOT NULL) or USING (auth.role() = 'authenticated'), or a scanner reports a table is readable by any account

Why it happens

Turning RLS on is only half the job: the policies decide who sees what. When an AI builder hits 'new row violates row-level security policy' or an empty result mid-build, the quickest change that makes the error go away is a policy that allows everything, such as USING (true). Variants that look safer have the same effect: TO authenticated USING (true), auth.uid() IS NOT NULL and auth.role() = 'authenticated' only check that someone is signed in, and because anyone can sign up for your app, every stranger with an account can read every row through the Supabase Data API using your public anon key. UPDATE policies with no WITH CHECK, or a WITH CHECK (true), also let a user rewrite rows they don't own or change user_id to someone else's. Postgres combines permissive policies with OR, so one broad policy left over from debugging cancels out the careful owner-only policy next to it. The fix is to scope every policy to the owner (or to data that is genuinely public), remove the catch-all policies, and test as a second user.

How to fix it

  1. List every policy in one place: in the Supabase SQL editor run select tablename, policyname, roles, cmd, qual, with_check from pg_policies where schemaname = 'public' order by tablename; and flag any row where qual or with_check is true, auth.uid() IS NOT NULL, or auth.role() = 'authenticated'. Also open Advisors → Security Advisor, but don't rely on it alone.
  2. Test it the way an attacker would: sign up two test accounts, create data as user A, then log in as user B and query the table with supabase-js, for example supabase.from('<TABLE>').select('*'). If B sees A's rows, the policy is too broad. Repeat logged out with only the anon key, where private tables should return [] or an error.
  3. For private, per-user tables, replace the policies with owner-scoped ones, one per operation, for example create policy "Owners can read" on public.<TABLE> for select to authenticated using ((select auth.uid()) = user_id); and do the same for update (with both USING and WITH CHECK set to (select auth.uid()) = user_id), insert (WITH CHECK) and delete (USING). Wrapping auth.uid() in select is the pattern Supabase recommends for performance.
  4. Drop the catch-all policies in the same migration (drop policy "<NAME>" on public.<TABLE>;). Because permissive policies are ORed together, a leftover USING (true) policy overrides every owner-only policy on that table.
  5. For data shared inside a team or workspace, check membership instead of login, for example using (exists (select 1 from public.members m where m.team_id = <TABLE>.team_id and m.user_id = (select auth.uid()))). For genuinely public data, limit the rows with a condition such as using (published = true) rather than true.
  6. Remember RLS works on rows, not columns. If other users may see a profile but not its email or phone, move private columns to a separate owner-only table or expose a view with only the safe columns, created with (security_invoker = on) so it still obeys RLS.
  7. Make sure user_id is set by the database (default auth.uid()) or checked by WITH CHECK, so a user can't insert or update rows claiming to be someone else.
  8. Re-run the two-user test and the logged-out test after deploying. If the table was readable by strangers while live, treat it as a data exposure: check what was in it and follow the security-report steps for telling users.

Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)

Audit the Row Level Security policies in my Supabase project. Write a SQL query against pg_policies that lists every policy in the public schema with its roles, command, USING and WITH CHECK expressions, and flag any that are USING (true), WITH CHECK (true), auth.uid() IS NOT NULL, auth.role() = 'authenticated' or otherwise let any signed-in user reach other users' rows. For each table, tell me whether it holds private per-user data, team data or public data, based on how my app code uses it. Then write one idempotent migration that drops the over-broad policies and creates least-privilege ones per operation (select, insert, update, delete), scoped with (select auth.uid()) = user_id or a team membership check, with WITH CHECK on insert and update so users can't write rows for someone else. Keep RLS enabled on every table, never use the service_role key in the browser, and give me a short test plan using two test accounts and a logged-out anon-key request to prove user B can't read or change user A's data.

Error codes this fixes

FAQ

RLS is enabled, so why can other users see my data?

Enabling RLS only blocks access that no policy allows. A policy like USING (true) or auth.uid() IS NOT NULL allows every request, or every signed-in request, so the table is effectively open.

Isn't TO authenticated enough to keep strangers out?

Only if strangers can't create accounts. In most apps anyone can sign up, so authenticated means anyone on the internet who spends ten seconds registering.

Why did my owner-only policy not help?

Postgres combines permissive policies with OR. If any one policy on the table says true, the row is visible, so you have to drop the broad policy, not just add a stricter one.

Is USING (true) ever fine?

Yes, for SELECT on data that is meant to be public to everyone, like published blog posts or a product catalogue. It is never fine for insert, update or delete, or for tables with private user data.

Sources

Based on the official documentation:

Related fixes

Want the full checklist for this area? Get a Fix Kit ($29)

Get one new fix a week

Free newsletter: the newest breakages in Lovable, Bolt, Cursor, Replit and v0 apps, with paste-in fixes.

We only use your email to send the newsletter. Unsubscribe any time. See privacy.