LatchDog

← Guides

How to test Supabase RLS policies: anon, a signed-in user, and pgTAP

Short answer: prove each policy allows and blocks what you meant, as a signed-out visitor (the anon role, using your publishable or legacy anon key) and as a signed-in user (the authenticated role). In SQL, do that inside a transaction: set local role to anon or authenticated, set request.jwt.claim.sub to the user id, assert both the allows and the denies, then roll the transaction back. Automate the same checks with pgTAP files under supabase/tests/ and supabase test db. LatchDog's deep scan checks only whether the public key can read a row from up to 15 tables or views named in the JavaScript it finds; it does not test writes, signed-in users, or Storage.

What a passing test has to show

Supabase's Row Level Security guide treats tests as part of securing a table, alongside grants and policies: "Create a .sql file under supabase/tests/ that asserts allow and deny for select, insert, update, and delete, for anon and authenticated." "Run supabase test db and fix what it reports." "Until the suite passes, you don't know whether the policies do what you intended." (Supabase: Row Level Security, read Nov 15, 2026.)

Two details from that page matter when you read a result:

Write a separate policy for select, insert, update and delete, named with to authenticated (or to anon, authenticated when signed-out visitors should also run it), and use (select auth.uid()) in the expression. Leave data that is meant to be public readable. For views, Storage, or a writable role column, use the linked guides at the end rather than repeating those checks here.

Test from the API: publishable key, signed out and signed in

The same policy should also be tested through the client your app uses. Supabase's pgTAP testing page walks through that for a todos table: create a client with the publishable key (SUPABASE_PUBLISHABLE_KEY from supabase status -o env), sign in as one user and as a second user, and assert what each can read and change. Use the secret key only in test setup (for example to create users). "A secret key authorizes access through the service_role Postgres role, which has the bypassrls attribute. Never use a secret key in the browser or expose it to customers." "A secret key bypasses RLS only when the request carries no user access token." (Supabase: Test Row Level Security policies with pgTAP and the RLS guide, read Nov 15, 2026.)

On the Data API, a signed-out request with only the publishable (or legacy anon) key runs as anon. After sign-in, the user's access token is sent and the request runs as authenticated, with auth.uid() taken from that token. If a denied update returns no error, that can still be a failed write: "RLS filters out the rows, so the update succeeds but changes nothing." Sign back in as the owner and check that the row is unchanged. Application-level tests cannot wrap that work in a database transaction, so give each suite its own user ids. The full Vitest example is on the testing page linked above. For what the public keys are and are not, see is the Supabase anon key safe to expose?.

Test in SQL: set local role and request.jwt.claim.sub, then roll back

The official policy-test examples switch identity with set local role and set local request.jwt.claim.sub. "The file runs in one transaction that rolls back at the end, so the test data never persists. set local role and request.jwt.claim.sub set the role and user each assertion runs as, the way a signed-in user's request does." set local keeps both settings inside that transaction, so rollback undoes the test rows and the role change together. (Postgres also has set role without local; the official examples use the transaction-scoped form.)

The testing overview's troubleshooting list also says to "Verify JWT claims are set" with set local "request.jwt.claims" (Supabase: Testing Overview, read Nov 15, 2026). That page does not show a complete assignment, so we do not invent one. The worked RLS examples on the pages above use set local request.jwt.claim.sub = '<user uuid>'. PostgREST documents reading the JSON setting request.jwt.claims in SQL; we did not find a current official RLS test that sets that JSON value.

begin;

set local role anon;
-- assertions as a signed-out visitor (no user id)

set local role authenticated;
set local request.jwt.claim.sub = '11111111-1111-1111-1111-111111111111';
-- assertions as the owner

set local request.jwt.claim.sub = '22222222-2222-2222-2222-222222222222';
-- assertions as a second signed-in user

rollback;

Open the transaction on a connection that is allowed to become those roles (your local supabase start database, or the SQL editor as a privileged role). Then roll it back so nothing stays behind.

Automate it: supabase test new and supabase test db

From the RLS guide: "The files above live under supabase/tests/ and run through pgTAP. Create them with supabase test new <table>_rls, and run them with supabase test db." The dedicated testing page starts the local stack with supabase start, applies migrations, then runs that same command until the output ends with Result: PASS. Every test file is wrapped in begin; / select plan(N); / select * from finish(); / rollback; (Supabase: Testing Your Database, read Nov 15, 2026).

Here is the official owner-scoped profiles example: four policies, one per operation, then the test file the same page publishes for them.

create table profiles (
  id uuid primary key,
  user_id uuid references auth.users,
  avatar_url text
);

alter table profiles enable row level security;

revoke all on table profiles from anon, authenticated;
grant select, insert, update, delete on table profiles to authenticated;

create policy "Users can view their own profile."
on profiles for select
to authenticated
using ( (select auth.uid()) = user_id );

create policy "Users can create their own profile."
on profiles for insert
to authenticated
with check ( (select auth.uid()) = user_id );

create policy "Users can update their own profile."
on profiles for update
to authenticated
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id );

create policy "Users can delete their own profile."
on profiles for delete
to authenticated
using ( (select auth.uid()) = user_id );
-- File: supabase/tests/profiles_rls_test.sql
-- Create: supabase test new profiles_rls
-- Run: supabase test db
begin;
select plan(14);

insert into auth.users (id, email)
values
  ('11111111-1111-1111-1111-111111111111', 'owner@example.com'),
  ('22222222-2222-2222-2222-222222222222', 'other@example.com');

-- anon holds no grant, so the request stops before any policy runs.
set local role anon;
select throws_ok(
  $$select * from profiles$$,
  '42501',
  null,
  'anon cannot read profiles'
);
select throws_ok(
  $$insert into profiles (id, user_id, avatar_url)
    values (gen_random_uuid(), '11111111-1111-1111-1111-111111111111', 'anon.png')$$,
  '42501',
  null,
  'anon cannot create a profile'
);
select throws_ok(
  $$update profiles set avatar_url = 'anon.png'$$,
  '42501',
  null,
  'anon cannot update profiles'
);
select throws_ok(
  $$delete from profiles$$,
  '42501',
  null,
  'anon cannot delete profiles'
);

-- The owner writes their own row. returning proves the row changed.
set local role authenticated;
set local request.jwt.claim.sub = '11111111-1111-1111-1111-111111111111';
select results_eq(
  $$insert into profiles (id, user_id, avatar_url)
    values (gen_random_uuid(), '11111111-1111-1111-1111-111111111111', 'owner.png')
    returning avatar_url$$,
  array['owner.png'],
  'the owner creates their own profile'
);
select results_eq(
  $$select avatar_url from profiles where user_id = '11111111-1111-1111-1111-111111111111'$$,
  array['owner.png'],
  'the owner reads their own profile'
);
select results_eq(
  $$update profiles set avatar_url = 'updated.png'
    where user_id = '11111111-1111-1111-1111-111111111111'
    returning avatar_url$$,
  array['updated.png'],
  'the owner updates their own profile'
);

-- A signed-in stranger holds the grant, so the policy is what stops them.
set local request.jwt.claim.sub = '22222222-2222-2222-2222-222222222222';
select throws_ok(
  $$insert into profiles (id, user_id, avatar_url)
    values (gen_random_uuid(), '11111111-1111-1111-1111-111111111111', 'stolen.png')$$,
  '42501',
  null,
  'another user cannot create a profile for the owner'
);
select is_empty(
  $$select * from profiles$$,
  'another user reads no profiles'
);
select is_empty(
  $$update profiles set avatar_url = 'stolen.png' returning avatar_url$$,
  'another user updates no profiles'
);

-- Matching no rows is not proof on its own. Pair every denied write with a
-- check that the row it targeted is intact.
set local request.jwt.claim.sub = '11111111-1111-1111-1111-111111111111';
select results_eq(
  $$select avatar_url from profiles where user_id = '11111111-1111-1111-1111-111111111111'$$,
  array['updated.png'],
  'the denied update left the owner row intact'
);

set local request.jwt.claim.sub = '22222222-2222-2222-2222-222222222222';
select is_empty(
  $$delete from profiles returning avatar_url$$,
  'another user deletes no profiles'
);

set local request.jwt.claim.sub = '11111111-1111-1111-1111-111111111111';
select results_eq(
  $$select avatar_url from profiles where user_id = '11111111-1111-1111-1111-111111111111'$$,
  array['updated.png'],
  'the denied delete left the owner row intact'
);

-- Owner delete last so earlier cases still have a row to assert against.
set local request.jwt.claim.sub = '11111111-1111-1111-1111-111111111111';
select results_eq(
  $$delete from profiles where user_id = '11111111-1111-1111-1111-111111111111'
    returning avatar_url$$,
  array['updated.png'],
  'the owner deletes their own profile'
);

select * from finish();
rollback;

The official comments on two denied-write assertions were slightly shortened above; the statements and expected results are the ones published on the RLS guide. For a table people share, that page also says to assert that a member who is not the owner can perform the operations its policies allow, and that a non-member cannot.

When the data is meant to be public

Do not lock down a feed that visitors are supposed to read. The official example grants select only to anon and authenticated, uses using ( true ), and tests that both roles can read while neither can write (the writes fail with 42501 because those roles hold no write grant):

alter table public.announcements enable row level security;
revoke all on table public.announcements from anon, authenticated;
grant select on table public.announcements to anon, authenticated;

create policy "Anyone can read announcements"
on public.announcements for select
to anon, authenticated
using ( true );

The matching test file is on the same RLS page (supabase/tests/announcements_rls_test.sql). Create it with supabase test new announcements_rls.

Community helpers (optional)

The RLS guide points at supabase-test-helpers for tests.create_supabase_user(), tests.authenticate_as(), and tests.rls_enabled(). The advanced pgTAP page installs that package as basejump-supabase_test_helpers and also documents tests.get_supabase_uid() (Supabase: Advanced pgTAP Testing, read Nov 15, 2026). Those helpers are optional; the official policy tests above work without them. Follow the install steps on that page if you want them. We have not independently verified the helper functions beyond what those pages list.

Paste-in prompt for Lovable, Bolt or Cursor

I need pgTAP tests for my Supabase Row Level Security policies, following https://supabase.com/docs/guides/database/postgres/testing-row-level-security and https://supabase.com/docs/guides/database/postgres/row-level-security.
Please:
1. Before writing tests, list the access rules to prove (which roles and operations should be allowed or denied, including cross-user cases and any intentionally public reads) and confirm them with me.
2. Use policies written to authenticated (or to anon, authenticated when signed-out visitors should read), with (select auth.uid()) in the expression. Keep a separate policy per operation. Do not disable RLS. Do not put a secret or service_role key in the browser. Leave intentionally public data public.
3. Put tests under supabase/tests/, created with supabase test new, wrapped in begin / select plan(N) / finish / rollback.
4. Run each assertion as anon or authenticated with set local role and set local request.jwt.claim.sub. Assert allows and denies. For a denied write that matches zero rows, also read the target row back and show it is intact. Do not use lives_ok to prove an allowed write; use returning and assert the returned value.
5. Run supabase test db and show me the output. If a test fails, tell me which assertion and which policy disagree; do not change a policy just to make a test pass.
Show me the SQL first.

What LatchDog does here (and doesn't)

LatchDog does not replace the tests above. After you verify the domain, a deep scan sends one SELECT ... LIMIT 1 to each of up to 15 tables or views named in the JavaScript the scan finds, using the public anon or publishable key from that bundle, and records only whether a row came back. That check is deep-scan only. It tests only anonymous read access through the Data API (/rest/v1/). It does not test writes, does not sign in as a user, does not call /rest/v1/rpc/, and does not test Storage. A policy that lets one signed-in user read another user's rows will not show up in a LatchDog report. Use supabase test db and a second signed-in account for that. More on the outside check: how to monitor your Supabase app for leaked keys and open RLS. How that compares with Security Advisor: LatchDog vs Supabase Security Advisor.

LatchDog keeps watching your live app after launch. Starter ($19/month, up to 3 apps) and Pro ($39/month, up to 10 apps) re-scan it automatically every day and after every deploy (triggered by a deploy hook from your host or CI), and email you only when something changes. Nothing to install: you verify your domain once.

Related guides: Supabase RLS for vibe-coded apps, rls_disabled_in_public and other Advisor errors, views can bypass RLS, protecting role and is_admin columns, and are my Supabase Storage files public?, and Supabase RLS checker: how to tell if a table is public.

Check yours

Start the local stack, add a test file with supabase test new for one of your tables, and run supabase test db until it prints Result: PASS. Then sign out and sign in as a second user on the live app and try to read or change the first user's rows. After you publish, the free LatchDog check can look for a leaked secret key in your JavaScript; the deep scan, after you verify the domain, can tell you whether named tables or views return a row to the public key.

Keep watching your app, not just checking it once

LatchDog keeps watching your live app after launch. Starter ($19/month, up to 3 apps) and Pro ($39/month, up to 10 apps) re-scan it automatically every day and after every deploy (triggered by a deploy hook from your host or CI), and email you only when something changes. Nothing to install: you verify your domain once.

Start with the free check (headers, HTTPS, leaked keys in your JavaScript, source maps, .env/.git). Open-table checks run after you verify your domain. See plans

Free, read-only public check: no sign-up, no card. Headers, HTTPS, leaked keys in your JavaScript (always masked), public source maps and exposed .env/.git files. Supabase table checks run after you verify you own the domain.

Want the open-table check too? Get my free deep scan: sign in with your email (no password) and verify you own the domain. Your first deep scan is free and adds CORS, more of your JavaScript and, if your app uses Supabase, whether anyone can read its tables and views with your public key.