Inigo AnguloContact
Work01How I think02Lab03Writing04
Writing

Row level security with AI agents: tests that run as the user who must not get in

Inigo AnguloNovember 4, 20267 min readAI agent teams

When AI agents write your database migrations, the risk is not that they forget row level security. They are diligent; they add the policy. The risk is that the policy looks right, the test is green, and the test never actually tried to cross the tenant boundary. On Acervo, a platform where each car collection is a tenant and the data is someone's private property, that failure mode was the one I designed against first.

The rule: every invariant has a home and a test

Every rule in the domain model is tagged with where it lives: in the database (a constraint, a policy, a trigger) or in the application (a server action). Nothing lives only in the frontend. Hiding a column in the interface is not a guarantee, and an agent will happily "fix" a permissions bug by hiding a button.

Every table has RLS enabled before it has data, and every policy has a test that runs as the role that must not pass. The suite is written in pgTAP and sets the JWT claims and the role to a user from another collection, then asserts zero rows. Not "the right user sees their rows": that test passes with RLS switched off. The test that matters is the negative one.

test, as user of Bset role, set jwt claimsselect * from A.carsexpect 0 rows reviewer, on purposedrop one isolation policyrun the suiteexpect red still green?the tests do not testwhat they claimPR blocked
Two tests per policy: one as the wrong user, one that proves the first can fail.

Break a policy on purpose

A negative test can still lie. If the test's setup is wrong, the user it impersonates is a member of nothing, and zero rows comes back for a reason that has nothing to do with the policy. So the rule for reviewing any change to security is: the reviewer deletes one isolation policy, runs the suite, and confirms it goes red. If it stays green, the pull request is blocked, whatever else it does. It is the cheapest proof I know that a suite tests what it says.

It caught a real one. Five new triggers written to run with the caller's privileges put an existing test in red for the wrong reason: at a lower auth level the user was a member of nothing, RLS hid the row, and the coherence check concluded "not your tenant" on perfectly coherent data, masking the real denial. The fix was not to adjust the expectation. It was to recognise that a coherence invariant is a property of the data, not of the observer, run it with definer rights, and let RLS do the denying with the right message.

Three habits that keep it true over 99 migrations

Pending users read zero rows, by the database

Registration is open; access is not. A new account is pending until a person approves it, and pending means zero rows from any table, guaranteed by RLS, with a test that proves it, never by the interface. Who approved whom, and when, goes to the audit log. This is the pattern for anything an agent might be tempted to enforce in a component: if it must be true, it is true in the database, and there is a test that runs as the person it must be true against.

Part of the series on running AI agent teams: the hub is AI agent teams for software development, the verification rules are in Why a green check is not proof, and the data model is in the Acervo case, chapter 08. If you want this discipline in your own build: AI build and deployment.

Working on something like this?

This is the method behind how I build: AI native development with human gates. Discuss a product idea → Or email me.