AleannLab

Supabase RLS Mistakes AI Tools Make (and How to Test for Them)

AleannLab team3 min read

Row Level Security (RLS) is the only thing standing between your Supabase database and anyone holding your public anon key, and the anon key is in every browser that loads your app. AI tools often generate RLS that is off, permissive (using (true)), missing for one operation, or bypassed by a service role key in client code. You can find all of these with a few SQL queries and prove the fix with two test accounts.

On this page
  1. Mistake 1: RLS disabled on a table
  2. Mistake 2: a policy that allows everything
  3. Mistake 3: select is protected, update is not
  4. Mistake 4: insert without with check
  5. Mistake 5: trusting a role stored where the user can edit it
  6. Mistake 6: the service role key in the browser
  7. Mistake 7: storage buckets set to public
  8. The two-account test
  9. How long does this take?

Supabase is a common backend for apps built with Lovable, Bolt, v0 and Cursor. It is a good choice. Its security model also assumes you understand one rule: the browser talks to your database directly, and RLS policies decide what each request may see.

Mistake 1: RLS disabled on a table

New tables created with SQL have RLS off. The Supabase dashboard warns about it; code generators don't always notice.

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

Every table in that list is readable and writable by anyone with your anon key, unless you revoked access another way.

Mistake 2: a policy that allows everything

SQL
create policy "Enable read access for all users"
on public.orders for select using (true);

This is the default template name in the dashboard, and AI tools copy it. RLS is "enabled", the warning disappears, and every row is public. Find them:

SQL
select tablename, policyname, cmd, qual, with_check
from pg_policies
where schemaname = 'public'
  and (qual = 'true' or with_check = 'true');

The fix compares the row owner to the caller:

SQL
create policy "Users read own orders"
on public.orders for select
using (user_id = (select auth.uid()));

Mistake 3: select is protected, update is not

Policies are per operation. A table with a correct select policy and a lax update policy lets a user modify rows they can't see, for example changing user_id on another user's record. Check every table has policies for select, insert, update and delete that match what the app needs, and nothing more.

Mistake 4: insert without with check

using controls which existing rows a user can touch. with check controls what the new row may contain. Without with check on insert or update, a user can create rows owned by someone else:

SQL
create policy "Users insert own orders"
on public.orders for insert
with check (user_id = (select auth.uid()));

Mistake 5: trusting a role stored where the user can edit it

A common generated pattern stores role = 'admin' in a profiles table and checks it in policies. If the same table lets users update their own profile row, any user can make themselves an admin. Keep roles in a table users can't write to, or in the JWT app_metadata, which only the server can set.

Mistake 6: the service role key in the browser

The service role key bypasses RLS completely. It belongs only in server code. Search the built JavaScript, not only the source:

bash
grep -r "service_role" .next/static/ dist/ build/ 2>/dev/null

In Next.js, any variable prefixed NEXT_PUBLIC_ is shipped to the client. NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY is a leaked key. Rotate it in the Supabase dashboard, then move the code that needed it into a server route.

Mistake 7: storage buckets set to public

Supabase Storage has its own policies. A bucket marked public serves every file to anyone with the URL, and file names are often guessable (/avatars/<user-id>.png, /invoices/<order-id>.pdf). Private documents belong in private buckets with policies tied to auth.uid(), served through signed URLs.

The two-account test

SQL queries show you the policies. Only a test shows they work.

  1. Create user A and user B in your app.
  2. As B, create some data: an order, a document, a message.
  3. Log in as A. In the browser console, use the Supabase client the app already loaded:
JavaScript
const { data, error } = await supabase.from('orders').select('*');
console.log(data); // should contain only A's rows
await supabase.from('orders').update({ total: 0 }).eq('id', '<B-order-id>');
// should affect 0 rows
  1. Repeat for every table with user data and every storage bucket.

If any query returns or changes B's data, you have a leak. Add the test to your CI with two seeded test users, so a future AI-generated migration can't quietly reopen it.

How long does this take?

For a typical app with 10-20 tables, finding the problems takes an hour or two with the queries above. Fixing them takes longer when the data model has no clear owner column, because the fix then needs a migration.

RLS is check number 2 in our 12-point security checklist for vibe-coded apps. If you would rather have someone run the full list and fix what it finds, see our app rescue and security audit.

All articles →

Gotowy, aby omówić swój projekt z nami?

idea
*
*
*
Interesuje mnie...
Tworzenie stron internetowych
Tworzenie aplikacji
Projektowanie UX/UI
DevOps
Sztuczna inteligencja i uczenie maszynowe
Testowanie QA
Tworzenie aplikacji mobilnych
Konsultacja
Outsourcing
Budżet projektu (USD)
≤ $5K
$10K-$20K
$30K-$40K
$50K-$60K
>$100K
Prześlij plik