Post

Check Your AI-Built App's Access Rules Before Real Users Do

Alex Garrett-SmithNov 276 min read
Check Your AI-Built App's Access Rules Before Real Users Do

Lots of apps built with AI app builders talk to the database straight from the browser. That's a normal way to build an app, but it's only safe if the rules on the database say who can read and change each row.

Researchers at Crissy Field recently found plenty of public Lovable apps where those rules weren't doing their job. Let's look at two quick checks to run on your own app before real users put real data in it, and how to turn them into tests.

This is about what your app's users can reach. For what your coding agent can reach on your machine, see How Much Access Should Your Agent Have?

What the researchers found

Crissy Field's report looked at Lovable projects that use Supabase as their database. Lots of them sent database records to the browser as soon as a page loaded, without anyone signing in. Some of that may be meant to be public, but further read-only requests with the same key brought back more.

The serious cases included forum profiles and chat messages, ticket and order records with addresses and payment information, and identity document details like passport numbers.

They're careful to say their sample isn't representative, their figures aren't a confirmed leak rate, and access control mistakes "can happen in any application, whether written by people or built with AI."

Why "it works when I'm logged in" doesn't tell you much

When you try your own app, you sign in and see your own stuff. That tells you the app asks for the right rows, but nothing about what the database would hand over if someone asked for different ones.

In an app like this, the browser holds a key that anyone can read. Supabase calls it the publishable key (the older version is the anon key), and its docs say it "only reaches what Row Level Security allows".

Row Level Security (RLS) policies work like a WHERE clause added to every query. With the usual "users see their own rows" policy, Alice's select * from orders only returns orders where user_id is hers. And it's easy for an agent to leave RLS switched off, or write a policy that lets everyone through, because the app looks and works the same for you either way.

The two checks

There are two questions to answer:

  1. Signed out, what data does the app load?
  2. Signed in as one user, can I read or change another user's rows? Do this on a copy of the app with fake data, like a staging project, never on real customer records. You'll need two test users, which I'll call Alice and Bob.

Signed out

Open the app in a private window so you're not signed in, then go to the Network tab in your browser's dev tools, reload the page and click around a few pages.

An app can fetch a whole table and only show you two columns, so the Response tab is where you'll see what was actually sent. Any names, emails, orders, messages or files in there that shouldn't be public are your first finding.

In a Supabase app, filter the requests by rest/v1 and you'll see the table names in the URLs.

Signed in as Alice

First, sign in as Bob in another browser and grab his user id, or the id of one of his rows, from his requests. Then sign in as Alice, find a request that loads one of her rows, right-click it and choose Copy > Copy as fetch. It still carries Alice's sign-in, so paste it into the Console (your browser may ask you to type allow pasting first), swap the id in the URL for Bob's, and run it.

Bob's data coming back means the rule isn't working. If the app lets Alice edit or create something, copy that request too, point it at Bob's row or user id, run it, then reload Bob's page to see if anything changed. Keep it to "can I see or change Bob's row?" and don't go hunting for anything cleverer.

Turn them into tests

Doing this by hand once is great, but the next change can undo it. So I'd turn both checks into tests that run in CI on every change, including your agent's.

The agent can write them for you. Here's the kind of prompt I'd give it:

Text
Write a test that signs in as two test users, Alice and Bob, using the
publishable key. For every table the app uses, check that a signed-out
client only gets what's meant to be public, and that Alice can't change,
create or delete any of Bob's rows, or read the private ones. Check the
rows that come back, not only the error, and read Bob's rows back at the
end to prove nothing changed.

For an orders table, with Node's test runner and supabase-js, you'd get something like this:

JavaScript
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { createClient } from '@supabase/supabase-js'

const { SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY, TEST_PASSWORD } = process.env

async function signIn(email) {
  const client = createClient(SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY)
  const { data, error } = await client.auth.signInWithPassword({ email, password: TEST_PASSWORD })
  if (error) throw error
  return { client, id: data.user.id }
}

test('a stranger gets no orders', async () => {
  const stranger = createClient(SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY)
  const { data } = await stranger.from('orders').select()
  assert.equal(data?.length ?? 0, 0)
})

test("Alice can't read or change Bob's orders", async () => {
  const alice = await signIn('[email protected]')
  const bob = await signIn('[email protected]')

  const { data: seen } = await alice.client.from('orders').select().eq('user_id', bob.id)
  assert.equal(seen?.length ?? 0, 0)

  const { data: changed } = await alice.client
    .from('orders')
    .update({ note: 'changed by Alice' })
    .eq('user_id', bob.id)
    .select()
  assert.equal(changed?.length ?? 0, 0)

  const { data: bobsOrders } = await bob.client.from('orders').select()
  assert.ok(bobsOrders.length > 0, 'Bob needs an order for this test to mean anything')
  assert.ok(bobsOrders.every((order) => order.note !== 'changed by Alice'))
})

Then read it, because a blocked request doesn't always come back as an error. If the signed-out role hasn't been granted the table at all, you'll get a permission error. But when a policy filters a row out, Postgres "raises nothing, matches zero rows" (in Supabase's words), so a blocked update just changes nothing.

That's why the test counts the rows that come back rather than checking error, and reads Bob's orders at the end. The length > 0 check makes sure Bob has an order to protect, or the test would pass on an empty table.

Run it yourself, too. If the agent wrote the rules as well, its closing message saying everything's secure doesn't prove anything, and a test that only reads Alice's own orders will keep passing whatever the rules say.

What this won't catch

These checks are only a first pass.

Views and functions

Supabase's docs point out that views bypass RLS by default, and a security definer function runs with the privileges of whoever created it. So a table can be locked down while a view over it hands everything out. If the app calls views or functions, check them the same way.

Logic mistakes

Automated scanners are good at spotting a table with no rules at all. They can't tell whether a rule that lets every signed-in user read every order is what you meant, so that still needs the Alice and Bob test.

Really sensitive data

If the app handles identity documents, payments or health data, it needs a proper security review by someone who knows what they're doing. Crissy Field say automated checks are "no substitute for a professional security assessment", and I agree.

When to check again

The tests cover every change, but I'd redo the hand check whenever a change touches sign-in, sessions or database rules, or adds a table or endpoint.

So before anyone real signs up, open the app signed out, then as Alice, and try to read one of Bob's rows.