RiskboundDocs

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 (riskbound on the server). The API connects as riskbound_app, which is not a superuser, has no BYPASSRLS and owns nothing. Migration 0009 makes the role without a password. Each deploy runs src/db/migrate.ts as the owner, which sets the role's password from POSTGRES_APP_PASSWORD in .env.
  • One policy per tenant table. Every table with a tenant_id column, and tenants on its id, has row-level security on and one policy, tenant_isolation, for riskbound_app: a row is visible and writable only if its tenant equals app.tenant_id. With the setting empty, the API sees no rows and can write none. New tenant tables call riskbound_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 with runWithTenant: 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) sets app.tenant_id on 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 DEFINER function 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_app has no UPDATE or DELETE on events, evidence_seals and risk_scores. It may DELETE only from slack_approvers.
  • signing_keys stays 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_app with 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 UPDATE still 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.ts runs as riskbound_app and shows it.

Consequences#

  • Tests run the way the server does. Services in tests get a database handle that runs every query as riskbound_app with the tenant in scope (createAppDb in test/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-tenant works as riskbound_app.
  • Restoring a dump into a fresh server needs the role first, since roles are not in pg_dump. docs/deployment.md says how.
  • A migration that adds a tenant table must secure it. The coverage test in test/rls.spec.ts fails until it does.
  • Merging migrations. Drizzle applies a migration only if its timestamp in drizzle/meta/_journal.json is 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.