1. Home
  2. Secure code review
  3. Supabase RLS and AI code mistakes

Supabase RLS and the common AI-code mistakes

If your AI-built app uses Supabase, row-level security decides whether your data is private. Left off or written too loosely, it makes your tables public. Here are the five mistakes we see and how to fix each one.

If your AI-built app uses Supabase, one setting decides whether your customers' data is private: row-level security, or RLS. When it is off or badly written, anyone who finds your public key can read your tables. This is the most common serious mistake in AI-built apps, and it is easy to fix once you know where to look.

What RLS does, in plain words

Supabase gives your browser app direct access to the database through an API. The key that app uses is public by design, since it sits in your front-end code. The only thing standing between a visitor and your data is RLS. With RLS on, each query is filtered by rules you wrote, such as "a user may read only rows where user_id matches their own login". With RLS off, there is no filter.

Mistake 1: RLS is switched off

AI tools create tables and often forget to enable RLS, or switch it off to make an error disappear. Check every table in the dashboard. Any table that holds user data needs RLS enabled.

alter table public.orders enable row level security;

Mistake 2: RLS is on, with no policies, then a policy that allows everything

Turning RLS on with no policy blocks all access, so the app breaks. The quick fix the AI reaches for is a policy that lets everyone in:

-- too open: any logged-in user reads every order
create policy "read orders" on public.orders
  for select using (auth.role() = 'authenticated');

That is not row-level at all. A correct policy ties each row to its owner:

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

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

Write a separate policy for each action you allow: select, insert, update and delete. Use with check on insert and update so users cannot write rows that belong to someone else.

Mistake 3: the service-role key in client code

Supabase issues two keys. The anon key obeys RLS. The service-role key bypasses it entirely. If the service-role key appears in your front-end code, a browser extension, a public repository or a prompt history, RLS no longer protects anything. Keep it on the server, in an environment variable, and use it only in server code. This is the hardcoded-secrets problem from the vibe coding security checklist in its most expensive form. If it ever leaked, rotate it in the dashboard.

Mistake 4: trusting the client for permissions

Hiding an admin button is not authorization. A user can call the same API without your interface. Any rule that matters, such as "only admins delete users", must be enforced by RLS or by server code that checks the user's role. The same applies to fields users should not set themselves, like a role or a price. Without a server-side check, this becomes a missing authorization flaw.

Mistake 5: storage and views left open

RLS applies to tables. File storage buckets have their own policies, and a public bucket exposes every file in it. Database views can also bypass RLS in some setups, since a view may run with the permissions of its owner rather than the caller. Check both, especially if the app stores invoices, IDs or uploaded documents.

How to test your policies

  1. Create two test users, A and B, and add a row for each.
  2. Log in as A and try to read, edit and delete B's row through the API.
  3. Log out and repeat with only the anon key.
  4. Try to insert a row with B's user_id while logged in as A.

Every one of those attempts should fail. If any succeeds, your policy has a gap.

Not sure which of your tables are exposed? A paid code review looks for tables without RLS, over-broad policies and leaked service-role keys, and tells you what to fix first. Reviews start from 500 euro (indicative).

Ask about a code review

What a scanner will and will not tell you

An automated scan can flag a table with RLS off. It cannot know whether your policy matches your business rules, for example that a team member may read the team's rows but not delete them. That judgment is what a human review adds. If you build with a browser tool, see securing apps built with Lovable, Cursor, Bolt or Replit, and for the full pre-launch list read the vibe coding security checklist. Our secure code review service covers RLS policy review and fixes as part of the paid review.

Common questions

What is Supabase RLS?

RLS stands for row-level security. It is a database feature that decides, row by row, who may read or change data. In Supabase it is the main protection for data that the browser can reach directly.

What happens if I leave RLS off in Supabase?

Anyone who has your project URL and public key can read, and often change, the rows in that table. Both values are visible in your app's code, so the data is effectively public.

Is the Supabase anon key safe to expose?

Yes, the anon key is designed to be public, but only when RLS is enabled and your policies are correct. The service-role key is different. It bypasses RLS and must never appear in client code.

Can AI write correct RLS policies?

Sometimes, but it often writes policies that are too broad, such as allowing every logged-in user to read every row. Test each policy with two separate accounts before you rely on it.