Architecture
ADR 0015: Row-level security in Postgres
Date: 2026-12-02. Status: accepted.
Context#
Every table carries tenant_id and every query filters on it. That rule
lives in the code. One query that forgets its filter, in a new feature or a
hurried fix, would show one tenant's decisions, approvals or evidence to
another. The threat model lists cross-tenant reads as risk 9, with
row-level security as the missing control.
Decision#
Postgres enforces the tenant boundary itself.
- Two roles. Migrations run as the owner of the tables (
riskboundon the server). The API connects asriskbound_app, which is not a superuser, has noBYPASSRLSand owns nothing. Migration 0009 makes the role without a password. Each deploy runssrc/db/migrate.tsas the owner, which sets the role's password fromPOSTGRES_APP_PASSWORDin.env. - One policy per tenant table. Every table with a
tenant_idcolumn, andtenantson itsid, has row-level security on and one policy,tenant_isolation, forriskbound_app: a row is visible and writable only if its tenant equalsapp.tenant_id. With the setting empty, the API sees no rows and can write none. New tenant tables callriskbound_secure_tenant_table('<table>')in their migration. A test fails until they do. - The tenant comes from the key. A request starts with an empty tenant
scope (middleware,
src/db/tenant-scope.ts). The auth guard binds the tenant of the API key once the key is verified, and refuses to bind a second one. Work outside a request names its tenant withrunWithTenant: the sealing schedule (per tenant), create-tenant (the new tenant's id) and a Slack button press (the tenant in the button, which the workspace check then confirms). - Set on every checkout. The pool (
src/db/connection.ts) setsapp.tenant_idon each connection it hands out, before any statement, so it holds for a whole transaction and for a single query alike. It is set at session level and always outside a transaction, so a rollback cannot undo it. The pool remembers what each connection was set to and skips the call when it already matches. Nothing else in the code may set it. - Three lookups cross tenants, before the tenant is known. Each is a
SECURITY DEFINERfunction that returns only what is needed: the key behind a bearer token, with a gateway key's bound agents (riskbound_find_api_key, ADR 0012), whether a tenant name is taken (riskbound_tenant_name_taken), and which tenants have events to seal (riskbound_tenants_due_for_seal). None returns another tenant's rows. - Append-only in the database.
riskbound_apphas no UPDATE or DELETE onevents,evidence_sealsandrisk_scores. It may DELETE only fromslack_approvers. signing_keysstays the exception (ADR 0007 addendum). It holds the server's public keys, belongs to no tenant, and has no policy.- Fail closed at startup. In production the API refuses to start if its
database role is a superuser, has
BYPASSRLS, or owns the tables. In development it warns, since a local setup often connects as the owner.
Why#
- The check sits below every query, including ones not written yet.
- The tenant still comes only from the key. Nothing in a request body can set it.
- A session setting per checkout costs nothing when the connection already
carries the tenant. Measured on a laptop against Postgres 16 in Docker
(2026-12-02, 300 refunds one after another after warm-up): median 4.0 ms
per decision as the owner with no row-level security, 4.0 ms as
riskbound_appwith it. Before the pool remembered the setting, it was about 1 ms slower. Local numbers, not production ones. - Row locks are unchanged.
FOR NO KEY UPDATEstill works under a policy, and foreign key checks bypass row-level security, so inserting a decision that references its tenant takes the same locks as before.test/concurrency.pg.spec.tsruns asriskbound_appand shows it.
Consequences#
- Tests run the way the server does. Services in tests get a database
handle that runs every query as
riskbound_appwith the tenant in scope (createAppDbintest/helpers.ts). The API test sets no fallback tenant, so only the guard scopes its requests. Tests look at and tamper with rows through the owner handle. - Scripts that bypass the API (
seed-dev,seed-demo) connect as the owner, which is fine in development.create-tenantworks asriskbound_app. - Restoring a dump into a fresh server needs the role first, since roles
are not in
pg_dump.docs/deployment.mdsays how. - A migration that adds a tenant table must secure it. The coverage test
in
test/rls.spec.tsfails until it does. - Merging migrations. Drizzle applies a migration only if its timestamp
in
drizzle/meta/_journal.jsonis newer than the last one applied. Migrations from other branches merged next to 0009 must keep the journal's timestamps in file order, or one is skipped.