Skip to content

How one organization can't see another's data

Founder, AIUBY

PostgreSQL + row-level security + tests

On this page
  1. Where isolation lives
  2. Row-level security, forced
  3. A role that cannot bypass the rule
  4. Context is per transaction
  5. Workspace: the second level
  6. What has to cross over
  7. The tests that prove it
  8. Limits

The AIUBY documentation says, in one sentence, that each organization and each workspace is isolated in the database, with row-level security. This post unpacks that sentence: what the mechanism is, why it does not depend on every query remembering to filter, and which tests keep a change from undoing it without anyone noticing.

#Where isolation lives

The usual way to separate customers is in the application: every query carries a filter on the organization. It works until the query that forgets the filter. A new listing, a report written in a hurry, and one customer's data shows up for another.

We would rather have the oversight fail safe. On the platform, the separation is enforced by PostgreSQL, with row-level security (RLS). Every customer table has a policy that compares the row's organization with the organization of the current transaction. A query with no filter at all returns only the current organization's rows. With no organization set, it returns nothing.

#Row-level security, forced

Enabling RLS on a table is not enough. In PostgreSQL, the table owner bypasses the policies unless the table is marked to force them. That is why every customer table gets both commands, and the policy comes in two halves. In simplified form, with generic names:

sql
ALTER TABLE documento ENABLE ROW LEVEL SECURITY;
ALTER TABLE documento FORCE ROW LEVEL SECURITY;

CREATE POLICY por_organizacao ON documento
  USING (organization_id = organizacao_corrente())
  WITH CHECK (organization_id = organizacao_corrente());
  • USING decides what the transaction can see, update and delete.
  • WITH CHECK decides what it can write. This is what stops an organization from inserting a row into another one, or moving one of its own rows to another one.

The function that returns the current organization returns null when there is no context. A comparison with null matches no row, so the unconfigured state is the closed one.

#A role that cannot bypass the rule

No policy holds back a superuser. So the application accesses customer data with its own database role, created without superuser privilege and without the attribute that allows bypassing RLS. The migration that creates the role reasserts those attributes even if it already exists, in case someone created it earlier with a different configuration.

This role also gets only the permissions it needs, table by table. Two examples: it cannot delete a workspace, which would take the API keys with it in a cascade, and on the audit trail it can only append.

#Context is per transaction

The current organization is a transaction setting. All access to customer data goes through a single application module, which opens the transaction, sets the organization and only then runs the query. The value is local to the transaction: when it ends, the connection goes back to the pool clean. This matters because connections are reused across requests from different customers, and a context that outlived the transaction would be exactly the leak we want to avoid.

The organization that gets set does not come from what the client asks for. It is resolved from the authenticated session or API key: a user who asks for an organization they are not a member of is refused before any query runs.

#Workspace: the second level

Inside the organization, the workspace separates areas and projects. For data that belongs to the workspace, such as Knowledge documents and conversations, the transaction also sets the workspace and who is acting (a person, a key or a system process), and the policy requires both: organization and workspace. That is why an API key, which is valid for a single workspace, cannot see the other workspaces in the same organization.

#What has to cross over

Some operations do not fit within the application role's permissions: debiting the balance when a call closes, adjusting a budget, writing certain audit events. For those there are database functions that run with the privilege of whoever defined them. It is a narrow door, with rules:

  • Each function does one thing and checks its own conditions. The one that adjusts the budget, for example, verifies the requester's role and the cap before writing.
  • The ones that serve customers read the organization from the transaction context, not from a parameter the caller chooses.
  • The name search path is fixed in the definition, so the function cannot resolve an object planted in another schema.
  • Execute permission is revoked from everyone and granted only to the role that needs it.

The trade-off is real: each of these functions is code that runs above the policies. A bug in one is not blocked by RLS, so the role and organization checks have to live inside the function itself.

#The tests that prove it

A mechanism like this can be lost to one careless migration: a new table without a policy, one extra privilege. The isolation suite runs on a disposable PostgreSQL, with the migrations applied and connected as the application role, not as an administrator. It runs in continuous integration, before the build.

TestWhat it proves
The application role is not a superuser and does not bypass RLSThe policies apply to the application
Every table with customer data has RLS enabled and forcedA new table without RLS breaks the suite
With no context, the query returns nothingThe unconfigured state is the closed one
Organization A reads the tables with no filter and gets only rows from ACross-tenant reads denied
A cannot insert into B, nor move one of its own rows to BCross-tenant writes denied by the database
Updating or deleting data from B affects zero rowsB's rows are invisible to A
Context does not leak between transactions on the same connectionThe connection pool is safe

The table test queries the database's own catalog, so it does not depend on someone remembering to register the new table. The only exceptions are the shared catalog tables (products, providers and models), which hold no customer data and are read-only for the application.

A design detail: without the test database variables, the suite fails on purpose. Skipping silently would hide the absence of the only proof of isolation.

#Limits

Limit

The isolation is logical: organizations share the same database, separated by policy. There is currently no dedicated database per customer. The platform also has no formal security certification.

  • A test proves what it tests. The suite covers the mechanism and the cases we know of; it is not a formal proof that no flaw exists.
  • The isolation applies within the platform. Model calls are sent to the providers and run on their servers, outside Brazil, after the workspace's sensitive data policy is applied.
  • The AIUBY team operates the platform through its own administration functions, separate from the customer path. Billing changes made by the team are recorded in the organization's audit log.

The summary for anyone evaluating the platform is in Security and data. For account access, see the post on password login and two-step verification.

Biweekly digest

New posts and what reached the changelog, every two weeks. No other emails.