<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[TestRlsWith2Users]]></title><description><![CDATA[TestRlsWith2Users]]></description><link>https://testingrls.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 16 Nov 2026 19:00:21 GMT</lastBuildDate><atom:link href="https://testingrls.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to test Supabase RLS with two users (script included) By Shaameer]]></title><description><![CDATA[Why “it works in my app” proves nothing When you test your own app, you are always logged in as the same user, looking at your own rows. Of course it…]]></description><link>https://testingrls.hashnode.dev/how-to-test-supabase-rls-with-two-users-script-included-by-shaameer</link><guid isPermaLink="true">https://testingrls.hashnode.dev/how-to-test-supabase-rls-with-two-users-script-included-by-shaameer</guid><category><![CDATA[supabase auth]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[postrege]]></category><dc:creator><![CDATA[SYED SHAAMEER Ali]]></dc:creator><pubDate>Sun, 15 Nov 2026 12:12:37 GMT</pubDate><content:encoded><![CDATA[<p>Why “it works in my app” proves nothing When you test your own app, you are always logged in as the same user, looking at your own rows. Of course it works. A leak only shows up when a different user requests your data, and that never happens while you’re building. Supabase also supports SQL tests with pgTAP, which is great if you like writing SQL. Most JavaScript developers just want a short, readable script they can drop into their repo, so that’s what this is. What the test checks Assume a profiles table with RLS enabled, owner-only policies (auth.uid() = id), and a signup trigger that creates one row per user. The script checks that:</p>
<ol>
<li>Setup: both users actually have a profile row. If not, the other results mean nothing, so the script stops.</li>
<li>User A can read their own row.</li>
<li>User A can’t read User B’s row.</li>
<li>User A can’t update User B’s row, and B’s row is unchanged afterwards.</li>
<li>User A can’t edit a protected column on their own row (this needs the column grants below).</li>
<li>User A can still edit the columns you allow, so check 5 isn’t passing just because everything is blocked.</li>
<li>A logged-out (anon) client can’t read the table. When you add a table, copy these checks for it. Row policies don’t protect columns This is the gap that’s easy to miss. RLS decides which rows a user can touch. It doesn’t decide which columns. If authenticated has a table-wide UPDATE grant, owner-only policies stop A from editing B, but A can still edit their own row, including any column like is_admin, role, plan or email. The fix is a column-level grant. Allow only the columns users should change: revoke update on public.profiles from authenticated; grant update (full_name, avatar_url) on public.profiles to authenticated; After this, updating full_name still works, and updating anything else fails with 42501 permission denied for table profiles. Make sure your client only sends the allowed columns in updates. The script below tests this using email as the protected column. Change PROTECTED_COLUMN to whatever sensitive column your table has. The script Install the Supabase client and save the code as test-rls.mjs. It needs Node 20.6+ for the --env-file flag. npm install @supabase/supabase-js Security note: this script uses your SUPABASE_SERVICE_ROLE_KEY to create and delete test users. Never put that key in client-side code, and don’t run this against a live production database. Keep it in a local .env file or an isolated CI test project. import { createClient } from '@supabase/supabase-js';</li>
</ol>
<p>const url = process.env.SUPABASE_URL; const anonKey = process.env.SUPABASE_ANON_KEY; const serviceKey = process.env.SUPABASE_SERVICE_ROLE_KEY; // local/CI test only</p>
<p>const PROTECTED_COLUMN = 'email'; // a column users must NOT be able to edit const ALLOWED_COLUMN = 'full_name'; // a column users may edit</p>
<p>const admin = createClient(url, serviceKey, { auth: { persistSession: false } });</p>
<p>async function makeUser(label) { const email = <code>rls-test-${label}-${Date.now()}@example.com</code>; const password = 'Test-password-123!';</p>
<p>const { data, error } = await admin.auth.admin.createUser({ email, password, email_confirm: true, }); if (error) throw error;</p>
<p>const client = createClient(url, anonKey, { auth: { persistSession: false } }); const { error: signInError } = await client.auth.signInWithPassword({ email, password }); if (signInError) throw signInError;</p>
<p>return { id: <a href="http://data.user.id">data.user.id</a>, client }; }</p>
<p>let failed = 0; function check(name, ok) { console.log(ok ? <code>PASS ${name}</code> : <code>FAIL ${name}</code>); if (!ok) failed++; }</p>
<p>const a = await makeUser('a'); const b = await makeUser('b');</p>
<p>try { // Setup: both users must have a profile row, or the checks below prove nothing const aRow = await admin.from('profiles').select('<em>').eq('id',</em> <a href="http://a.id"><em>a.id</em></a><em>).single(); const bBefore = await admin.from('profiles').select('</em>').eq('id', <a href="http://b.id">b.id</a>).single(); check('Setup: A and B both have profile rows', !!aRow.data &amp;&amp; !!bBefore.data); if (!aRow.data || !bBefore.data) { throw new Error('Signup trigger did not create a profile row. Fix that before trusting any result.'); }</p>
<p>// 1. A can read their own row const own = await a.client.from('profiles').select('*').eq('id', <a href="http://a.id">a.id</a>); check('A can read own profile', !own.error &amp;&amp; own.data.length === 1);</p>
<p>// 2. A cannot read B's row const other = await a.client.from('profiles').select('*').eq('id', <a href="http://b.id">b.id</a>); check('A cannot read B', !other.error &amp;&amp; other.data.length === 0);</p>
<p>// 3. A cannot update B's row, and B is unchanged afterwards const upd = await a.client .from('profiles') .update({ [ALLOWED_COLUMN]: 'hacked' }) .eq('id', <a href="http://b.id">b.id</a>) .select(); const bAfter = await admin.from('profiles').select('*').eq('id', <a href="http://b.id">b.id</a>).single(); check( 'A cannot update B', (upd.data ?? []).length === 0 &amp;&amp; bAfter.data?.[ALLOWED_COLUMN] === bBefore.data[ALLOWED_COLUMN] );</p>
<p>// 4. A cannot edit a protected column on their own row (expects permission denied) const prot = await a.client .from('profiles') .update({ [PROTECTED_COLUMN]: '<a href="mailto:changed@example.com">changed@example.com</a>' }) .eq('id', <a href="http://a.id">a.id</a>) .select(); check('A cannot edit protected column', prot.error?.code === '42501');</p>
<p>// 5. A can still edit an allowed column (so check 4 isn't passing because everything is blocked) const ok = await a.client .from('profiles') .update({ [ALLOWED_COLUMN]: 'Alice' }) .eq('id', <a href="http://a.id">a.id</a>) .select(); check('A can edit allowed column', !ok.error &amp;&amp; (ok.data ?? []).length === 1);</p>
<p>// 6. Logged-out client cannot read the table const anon = createClient(url, anonKey, { auth: { persistSession: false } }); const anonRead = await anon.from('profiles').select('*'); check('anon cannot read profiles', !!anonRead.error || (anonRead.data ?? []).length === 0); } catch (e) { console.error(e.message); failed++; } finally { // cleanup: deleting the auth user also removes the profile (on delete cascade) await admin.auth.admin.deleteUser(<a href="http://a.id">a.id</a>); await admin.auth.admin.deleteUser(<a href="http://b.id">b.id</a>); }</p>
<p>console.log(failed === 0 ? '\nAll checks passed' : <code>\n${failed} check(s) failed</code>); process.exit(failed === 0 ? 0 : 1); Add your keys to a local .env file and run: node --env-file=.env test-rls.mjs Check 4 asserts the exact error code 42501, not just “any error”. That matters: if you point it at a column that doesn’t exist, you get a different error, and the check correctly fails instead of passing for the wrong reason. Reading the results • Setup fails: your signup trigger didn’t create the profile row. Fix that first. • Check 1 fails (A can’t read own row): you likely have a missing grant. A 42501 here usually means no GRANT SELECT ON public.profiles TO authenticated. • Check 2 or 3 fails (A can touch B): your policy is too permissive. Look for USING (true), a policy that only says to authenticated with no auth.uid() owner check, or a missing WITH CHECK on updates. • Check 4 fails (A edited a protected column): you have a table-wide UPDATE grant. Use the column-level grant above. • Check 5 fails: your column grant is too tight, or you revoked update entirely. • Check 6 fails (anon can read): you’re granting table access to anon, or a policy targets it. Revoke it and keep policies on authenticated. Verify the test can fail. Temporarily loosen a policy to USING (true) and run the script. Check 3 should fail. A security test that can’t fail isn’t testing anything. A quick version in the SQL editor To poke at a policy without a script, you can impersonate a user inside a transaction in the Supabase SQL editor: begin; set local role authenticated; select set_config('request.jwt.claims', '{"sub": ""}', true);</p>
<p>-- should return only User A's row select id from public.profiles; rollback; Replace with a real user id from your Auth dashboard. It’s fast for ad-hoc debugging, but a script you run after every migration is what catches regressions. Run it after every schema change Policies break quietly: a new table, a changed grant, a migration that drops a policy. Put the script in CI or your pre-deploy checklist, and extend it whenever you add a table. Want this set up already? My Express + Supabase starter includes a two-user RLS test like this one, plus auth, a signup trigger, and explicit grants for Supabase’s newer defaults. Disclosure: I’m the author, and it’s a paid starter, not a security audit. The tests cover the profiles table, so any tables you add need their own policies and tests. <a href="https://shaameer.gumroad.com/l/SupabaseExpressjsProductionBackend/Launch10">https://shaameer.gumroad.com/l/SupabaseExpressjsProductionBackend/Launch10</a> The script above is free to copy either way. If it catches something in your project, I’d like to hear what.</p>
]]></content:encoded></item></channel></rss>