Skip to main content

Zero-Filter Multi-Tenancy

In most application stacks, multi-tenancy is enforced through application code: every query carries a WHERE tenant_id = ? clause, every write attaches the calling tenant's id, every join verifies that joined rows belong to the right tenant. The pattern works — until someone forgets a filter. One missing WHERE and a tenant sees another tenant's data; one forgotten join condition and you have a regulated-data incident.

Kilimanjaro's policy layer compiles tenant isolation into row-level security (RLS) at the database. Application code does not write the filter; the platform appends it as a mandatory WHERE condition on every query against the collection. A query without the filter does not exist — the database refuses to compile it.

This tutorial walks through deleting the application-layer filter code and verifying the isolation is enforced at the database.

Prerequisites​

  • A project with a posts collection that has a tenant_id column (or equivalent)
  • The SDK installed and initialised per Building Your First App Kernel
  • An authenticated user context — your auth flow puts a tenant claim on the JWT (e.g., subject.attributes.tenant_id)

Step 1 — Define the unified policy​

In the Console, open Policies → New policy (or version policy with your code via the migration file convention per Unified Policy).

Author two rules against the posts collection — one for reads, one for writes — both binding the caller's tenant claim to the row's tenant_id field. The canonical rule shape and operator syntax are in API → Control; the conceptual pattern is:

  • Read rule — allow select when the subject's tenant claim equals the row's tenant id
  • Write rule — allow insert / update / delete when the same tenant claim matches AND the calling subject is authenticated (no anon writes)

Each rule is an rls entry in the project's _sys.authorization_policies policy document. The two rules are added to a new draft version of that document; publishing it atomically archives the previously-active version and activates the new rules. The document is identified by a stable pol_<base62> policyId across its draft → active → archived lifecycle. If validation fails at publish (an operand path that does not resolve to a known field, for example, returning K9O_POLICY_UNKNOWN_CONTEXT_FIELD), the previously-active version remains in force — you cannot accidentally leave the project in a partially-applied state.

Save and publish the policy. The platform compiles it into a WHERE clause appended to every query against posts, binding the predicate to the calling subject's tenant claim at evaluation time.

JWT tenant claimsubject.attributes.tenant_idApplicationselect(), no tenant filterUnified policyrls rules on postsDatabasemandatory WHERE tenant_idbound to the tenant claimTenant A rows onlyno tenant parameter existsAudit detailcompiled SQL, matched rule
The tenant filter is added by the platform from the caller's claim, so application queries never carry it.

Step 2 — Delete the application filter code​

Find every place in your application that adds a tenant filter to a posts query. They typically look like:

// Before
const userTenant = req.user.tenantId;
const posts = await db.query(
'SELECT * FROM posts WHERE tenant_id = $1 AND published_at IS NOT NULL ORDER BY published_at DESC',
[userTenant]
);

After the policy is in place, the platform appends the tenant_id filter automatically. The application query becomes:

// After
const { data: posts } = await k9o.from('posts').select();

The platform appends the tenant filter at execution. The application code is concerned only with the business intent (published posts, newest first, which your code can filter and sort on the returned rows), not with the tenancy invariant.

Repeat for every collection's query path. The deletion is structural — any application code that was only enforcing the tenant filter can be removed entirely.

Step 3 — Verify isolation under attack​

Test that the isolation holds even when application code tries to bypass it. From a tenant A user context:

// Attempt to read posts as if from a different tenant — there is no API
// surface that takes a tenant_id parameter to override the JWT claim
const { data } = await k9o.from('posts').select();

// Tenant A user sees only tenant A posts. There is no parameter to change this.

Verify in the Console Audit → Recent events:

  • The query you submitted
  • The compiled SQL the database actually executed (visible in the verbose audit-event detail)
  • The matching policy rule that produced the appended WHERE clause

The forensic record shows the platform's enforcement decision, not just the application's intent.

Step 4 — Check the rejection counter​

The Console exposes a Policy rejections view that shows policy-denied operations over time. If an authenticated user attempted to read or write rows from a different tenant (which would happen, for example, if your application accidentally passed the wrong tenant_id in a write context), you would see a rejection here with the subject, the resource, and the rule that fired — pinned to the pol_<base62> policyId and the version that governed the rejection. If you re-publish (because the rule needed to evolve), the new version becomes active and the previously-active version moves to archived while remaining in the rejection history as the version that was active when each historical rejection occurred.

A zero rejection counter for a properly-functioning application is the goal. A non-zero counter is a signal — either there is a bug in your application code making impossible requests, or there is genuine probing activity worth investigating.

What just happened​

You moved the multi-tenancy invariant from application code into the platform's policy layer. The benefits:

  • Structural enforcement — you cannot ship a code change that breaks the isolation by forgetting a filter
  • Cross-surface consistency — the same policy covers collections, storage, real-time subscriptions, and functions; no separate authorisation paths
  • Audit visibility — every rejection is logged with the matched rule, the subject, and the resource; correlation with application logs is one requestId away
  • Code reduction — the application code is smaller, focused on business logic rather than security plumbing

Next​