Guides

How to test Supabase RLS with two users

A public URL only shows the anonymous view. Sign in as two users and try to read, update, and delete each other's rows.

Updated 27 October 2026 · LIM Agentic AI, Paris

Row Level Security answers a different question than "can a stranger open the URL." A logged-out visit sends the anon key and no user JWT. Postgres treats that as the anon role, and auth.uid() is null. A policy of auth.uid() = user_id returns no rows. The screen looks empty. That result says nothing about user B.

The failure that ships in generated Supabase apps is between two signed-in users. User A creates a row. User B, with their own session, asks for that row by id and then tries to change it and delete it. If any of those calls returns the row, RLS is not holding.

What the anon request actually is

The client in the browser is created with the project URL and the anon key (or the newer publishable key, sb_publishable_...). Both are public. They do not bypass RLS. Without a signed-in session, PostgREST runs the query as anon.

auth.uid() reads the sub claim of the JWT on that request. No JWT, null sub, and null = user_id is never true in SQL. The policy fails closed for the stranger. It has not been tested for a second account.

A response of [] with no error is also what a hidden row looks like. PostgREST does not return 401 when RLS filters a row out. It returns 200 and an empty array. You have to look at the array. An error of null does not mean the write was refused.

Two users, one marker row

In the Supabase dashboard, open Authentication, then Users, then Add user. Create two users. Turn on auto-confirm so they can sign in without the email link. Use passwords you can put in environment variables. Do not commit the passwords.

Use a table you can afford to write to. A notes table with id uuid primary key default gen_random_uuid(), user_id uuid not null default auth.uid(), and body text is enough. If user_id has no default, send user A's own id from A's session. Do not point this script at a table of real customer rows.

The script signs in twice. Each createClient has its own session, so A and B do not share a token. It inserts one marker as A, then as B it selects, updates, and deletes that id. .select() after the update and the delete is required. Without it, supabase-js often gives you no row body, and you cannot tell a blocked write from a successful one.

Two sessions, one marker
import { createClient, type SupabaseClient } from "@supabase/supabase-js";

const url = process.env.SUPABASE_URL;
const anon = process.env.SUPABASE_ANON_KEY; // or the sb_publishable_ key
if (!url || !anon) throw new Error("Set SUPABASE_URL and SUPABASE_ANON_KEY");

async function signIn(email: string, password: string): Promise<SupabaseClient> {
  const client = createClient(url, anon);
  const { error } = await client.auth.signInWithPassword({ email, password });
  if (error) throw error;
  return client;
}

const a = await signIn(process.env.USER_A_EMAIL!, process.env.USER_A_PASSWORD!);
const b = await signIn(process.env.USER_B_EMAIL!, process.env.USER_B_PASSWORD!);

const marker = `rls-check-${crypto.randomUUID()}`;
const inserted = await a.from("notes").insert({ body: marker }).select("id, user_id").single();
if (inserted.error) throw inserted.error;
const id = inserted.data.id;

const read = await b.from("notes").select("id, body").eq("id", id);
const update = await b.from("notes").update({ body: "rewritten by B" }).eq("id", id).select("id");
const del = await b.from("notes").delete().eq("id", id).select("id");
const stillThere = await a.from("notes").select("id, body").eq("id", id);

console.log({
  bRead: read.data,
  bReadError: read.error,
  bUpdate: update.data,
  bUpdateError: update.error,
  bDelete: del.data,
  bDeleteError: del.error,
  aStillSeesIt: stillThere.data,
});

await a.from("notes").delete().eq("id", id);

Read the log like this.

How to read the two-user result
CallPassFail
B selectdata is []data contains the id
B updatedata is [] and error is null, or error says the new row violates RLSdata contains the id
B deletedata is []data contains the id
A select afterwardsA still sees the marker, unless B's delete failed the testA's row is gone and you did not delete it

An update error whose message says the new row violates row-level security means B matched a row (the USING side let them through) and the WITH CHECK side rejected the new values. That is a pass for "B did not change the row," and it is a different bug shape from a policy that hides the row. The insert-policy guide covers that case.

If B's delete returns the row, the marker is already gone. That is the finding. The body was a marker, so you can tell it from real data. Then stop. Do not rerun the test against a wider table to "see how bad it is."

Check it yourself

First confirm RLS is actually enabled. The table owner and the postgres role bypass RLS unless you force it, so a query from the SQL editor as the default role will show every row even when the API is locked down.

Which public tables have RLS on
select c.relname as table_name,
       c.relrowsecurity as rls_enabled,
       c.relforcerowsecurity as rls_forced
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public'
  and c.relkind = 'r'
order by c.relname;

rls_enabled should be true for every table the anon key can reach. rls_forced is optional. It makes the table owner obey RLS too. The API roles anon and authenticated already obey RLS when it is enabled.

You can also impersonate B inside a transaction. This only counts if the role change sticks for the select. Some SQL editors run each statement on its own, and set local dies at the end of the statement. If you still see every row, you are looking at the result as postgres. Trust the two-client script in that case. If the editor has a role impersonation control, set it to authenticated and to user B, then run the select.

Impersonate user B for one transaction
-- The SQL editor connects as postgres, which bypasses RLS.
-- set role, or this query proves nothing.
begin;

set local role authenticated;
select set_config(
  'request.jwt.claims',
  '{"sub":"USER_B_UUID","role":"authenticated"}',
  true
);

-- Expect zero rows. A row here means B can read A's marker.
select id, user_id, body
from public.notes
where id = 'ROW_A_ID';

rollback;

A grant error (permission denied for table) is not an RLS result. The authenticated role needs select, update, and delete grants, which Supabase adds for tables in public created by the dashboard. An empty array with no error is the RLS result you want from user B.

Policies for the same command are permissive by default, and permissive policies are combined with OR. One using (true) next to a strict owner policy still lets B read the row. Listing the policies is the next step, in the WITH CHECK guide.

Questions

Does a private window count as user B?

No. A private window with no session is still the anon role. User B has to be a second signed-in account, with their own JWT, calling the API with the same anon key the app ships.

RLS is enabled and the anon key returns nothing. Is the table safe?

Not yet. That is the logged-out view. You still need the two-user test, and you need an insert and update policy that checks auth.uid() against the row owner. Enabling RLS with no policy denies the API roles, which is safe and also means the owner cannot read their own row until you add a policy.

The update returned no error and no rows. Did it write?

Chain .select() on the update and the delete. An empty data array means zero rows were returned, which for these calls means zero rows changed. Without .select(), supabase-js can hide that distinction.

Other guides