Vibe Code Guardian
← All posts

Supabase RLS Mistakes That Let AI-Built Apps Leak Data

Row Level Security is the only thing between your users' data and the open internet. AI coding tools set it up fast and get it wrong in predictable ways. Here are the specific patterns that cause leaks, and how to verify yours before launch.

Aaron Allen·

Most vibe coders hit the same wall. The app works locally. The AI wrote the Supabase client, the tables, the policies. You run the free surface check and it flags "RLS policy may allow cross-user access." You open the policy, it looks correct, and you ship anyway.

Two weeks later a user reports they can see someone else's orders.

Row Level Security is the only layer that stops that. Not your frontend checks. Not your API validation. The database itself must refuse the query. AI tools are good at writing policies that look correct for the happy path. They are bad at the edge cases that attackers and bugs exploit.

Here are the five mistake patterns we see most often in AI-generated Supabase apps, with the exact SQL to test each one.

1. Policies that only check ownership on SELECT

The AI writes a policy like this:

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

That works for SELECT. It does nothing for UPDATE, DELETE, or INSERT. A user who knows another user's order ID can cancel it, change the shipping address, or insert a fake order with someone else's user_id.

The fix: write separate policies for each operation, or use for all with explicit checks.

create policy "Users can manage their own orders"
on orders for all
using (auth.uid() = user_id)
with check (auth.uid() = user_id);

The using clause controls what rows are visible. The with check clause controls what rows can be written. Both must match.

Test it: log in as user A, try to update user B's order. The database should return zero rows affected.

2. Policies that trust the client to send the right user_id

The AI creates an INSERT policy:

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

This looks correct. The problem: the client sends user_id in the request body. A malicious user changes that value to another user's ID. The policy checks auth.uid() = user_id but user_id is now the victim's ID, so the check passes.

The fix: never trust client-sent IDs for ownership. Use auth.uid() directly in the insert.

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

-- In your application code, never accept user_id from the client
insert into orders (user_id, ...)
values (auth.uid(), ...);  -- server-side only

If you use the Supabase client on the server, call supabase.auth.getUser() and use that user's ID. Never read user_id from the request body for ownership decisions.

3. Missing policies on joined tables

Your orders table has RLS. Your order_items table has RLS. A query joins them:

select * from orders
join order_items on order_items.order_id = orders.id
where orders.user_id = auth.uid();

This works. But what if the attacker queries order_items directly?

select * from order_items
join orders on orders.id = order_items.order_id
where orders.user_id = 'victim-user-id';

If order_items has no policy, or a policy that only checks order_items.user_id (which doesn't exist), the join bypasses the orders policy entirely.

The fix: every table in a join chain needs its own policy that enforces the same ownership logic. For order_items, the policy must traverse the relationship:

create policy "Users can view their own order items"
on order_items for select
using (
  exists (
    select 1 from orders
    where orders.id = order_items.order_id
    and orders.user_id = auth.uid()
  )
);

Same for UPDATE, DELETE, INSERT. Every table that holds user data needs a policy, even if no one queries it directly.

4. Service role key used in client code

The AI needs to do something admin-only: delete a user, send a webhook, run a migration. It grabs the service role key because that's what the docs say for admin operations. The key ends up in a client component, an Edge Function without auth verification, or a GitHub Actions secret that gets printed in logs.

The service role key bypasses all RLS. Anyone who gets it has full database access.

The fix: the service role key never leaves your server. Not in client bundles. Not in Edge Functions that accept unauthenticated requests. Not in CI logs.

If you need admin operations, create a database function with SECURITY DEFINER that does exactly one thing, then call that function from your authenticated code:

create function delete_user_account(target_user_id uuid)
returns void
language plpgsql
security definer
as $$
begin
  -- Only the function owner (you) can create this
  -- It runs with your privileges but only does this one thing
  delete from auth.users where id = target_user_id;
end;
$$;

Then from your authenticated server code:

await supabase.rpc('delete_user_account', { target_user_id: userId });

The caller needs no special privileges. The function does one verified action.

5. Realtime publications that leak private data

You enabled Realtime for live updates. The AI added your tables to the supabase_realtime publication:

alter publication supabase_realtime add table orders, order_items, profiles;

Realtime sends row changes to every subscribed client. It does not respect RLS for subscriptions. If a user subscribes to orders, they get every insert, update, delete on that table regardless of policy.

The fix: only publish tables that are genuinely public. For private data, use one of these approaches:

Option A: Database functions with SECURITY DEFINER

create function get_user_orders()
returns setof orders
language sql
security definer
as $$
  select * from orders where user_id = auth.uid();
$$;

Call this via RPC instead of subscribing to the table.

Option B: Filter on the client, but accept the risk

If you must use Realtime on private data, add a filter on the client and accept that a determined attacker can bypass it. This is not a security control.

Option C: Separate public tables for real-time features

Create a notifications or activity_feed table with only the data meant to be public. Publish that table. Keep sensitive data in tables without Realtime.

How to verify your RLS before launch

Run these checks in the Supabase SQL editor. Do them as an authenticated user (use the "Run as user" dropdown or set request.jwt.claims).

Test 1: Cross-user SELECT

-- As user A
set role authenticated;
set request.jwt.claims = '{"sub": "user-a-id"}';
select * from orders;  -- should only return user A's orders

Test 2: Cross-user UPDATE

-- As user A, try to update user B's order
update orders set status = 'cancelled'
where id = 'user-b-order-id';
-- should return UPDATE 0

Test 3: Cross-user INSERT with forged user_id

-- As user A, try to insert order for user B
insert into orders (user_id, total, status)
values ('user-b-id', 100, 'pending');
-- should fail or insert with user A's ID only

Test 4: Direct access to joined tables

-- As user A, query order_items directly
select * from order_items
join orders on orders.id = order_items.order_id
where orders.user_id = 'user-b-id';
-- should return zero rows

Test 5: Service role key not in client bundle

# In your built output
grep -r "service_role" .next/ dist/ build/ --include="*.js" --include="*.mjs"
# should return nothing

Test 6: Realtime publication audit

select * from pg_publication_tables
where pubname = 'supabase_realtime';
-- every table listed must be safe for public subscription

What to do when you find a gap

Fix the policy. Test again. Then run the free surface check at vibecodeguardian.co.uk/free-check. It scans for these exact patterns and flags the ones that pass manual review but still leak.

If you're handling payments, health data, or other people's money, the launch-readiness audit includes a manual RLS review with a developer who writes Supabase policies daily. They catch the logic errors that automated tools miss: the policy that looks correct but has a boolean operator in the wrong place, the function that returns more columns than it should, the migration that dropped a policy and didn't recreate it.

RLS is not optional. It is not a nice to have. It is the only thing that actually enforces your data boundaries. The AI will not get it right on the first try. Check it before you ship.

Want a developer to look at your app?

Built your app with AI tools? Run the free surface check yourself in a few minutes. Nothing to buy, and we never look at your app unless you run it.