How to prove tenant isolation in CI: testing PostgreSQL row-level security

Mesito's CI finds every table, checks each has row-level security switched on and enforced, then tries to read and change one office's rows as another.

The short answer

Mesito's isolation test doesn't list tables by hand. It discovers every table in the database, fails if one that holds an office's data lacks enforced row-level security and a policy, then fills two offices with data and checks, table by table, that one office can't see, change, delete, copy or take over the other's rows. A new table without protection fails CI on its own.

On this page
  1. Why not list the tables in the test?
  2. What does the catalogue sweep check?
  3. What does the behaviour test do?
  4. Is the database test enough?
  5. What does it cost to run?
  6. The short version

Mesito is a CRM for estate agents. Every office’s contacts, properties, showings and documents sit in the same PostgreSQL database, separated by row-level security (RLS): the database itself refuses to show one office’s rows to another, whatever the application code does.

RLS only protects the tables it’s switched on for. The risk isn’t the tables you set up carefully on day one. It’s the table someone adds six months later and forgets. So the question isn’t “does RLS work?” but “how do we know every table has it, today and after the next migration?”

Why not list the tables in the test?

A test with a hand-written list of tables checks the tables someone remembered to add to the list. That’s the same mistake the test is meant to catch.

Mesito’s test discovers the tables itself. It reads the database’s own catalogue and finds every table that carries an office id. That’s the whole trick: a new tenant table is covered by the test the moment it exists, with nobody having to remember anything.

What does the catalogue sweep check?

Before touching any data, the test checks the set-up:

  • The app connects as a plain role: not a superuser, not the owner of the tables, and not allowed to bypass RLS. A role that can bypass RLS makes every policy pointless.
  • Every table with an office id has RLS switched on, forced, and at least one policy. “Forced” matters: without it, the table’s owner skips the policies.
  • Every table with RLS also has a policy, since RLS with no policy silently returns nothing.
  • Every policy applies to the role the app uses, not to some other role by mistake.
  • Shared reference tables are read-only for the app.

What does the behaviour test do?

Then it fills two offices, A and B, with realistic data: the development seed plus a few real API calls, such as uploading a document. For every office table, connected as the app’s role with the same settings the app uses:

Context Expected
No office selected No rows at all
Office B None of A’s rows visible, none can be updated or deleted
Office B Inserting a copy of one of A’s rows is refused
Office A Moving one of A’s rows over to B is refused

The last two catch a subtle gap. A policy that only filters what you can read still lets a user write rows into another office, or move their own rows there. The policies’ write checks close that, and the test proves it.

Mesito also has other contexts besides staff: a client portal and a public context for the website chatbot. The same discovered list is used to check that a client sees only their own rows and the public context sees only published listings.

Is the database test enough?

It proves the database layer. One more test goes through the real API: a signed-in user of office B asks for office A’s contacts and gets nothing. That checks the API sets the office context correctly, which the database test alone can’t.

What does it cost to run?

It runs against a real PostgreSQL in every CI run, not a mock. A mock of a database can’t tell you whether the database enforces a policy. These tests are on our list of checks that are never removed to save time.

The short version

  • Discover tables from the catalogue; never hand-list them.
  • Check the role can’t bypass RLS, and that RLS is forced.
  • Test read, update, delete, insert-a-copy and move-to-another-tenant, for every table.
  • Add one test through the real API on top.

Keep reading

Tell us what you want to build.

A product, a website, a search problem or a system that should work better. We reply with a plain answer and the first step.

A few sentences are enough (at least 20 characters).

We use your details only to answer your inquiry. See our privacy policy.