Architecture · All ADRs
ADR 0003 — Tenant isolation: RLS + composite foreign keys + explicit filters
Architecture decision record — status: Accepted.
Status: Accepted
Context#
A cross-tenant data leak is the most damaging failure a multi-tenant security product can have. One missing WHERE org_id = … must not be enough to cause it.
Decision#
Three independent layers:
- PostgreSQL row-level security on every tenant table, keyed on the
app.org_idGUC set per transaction (Database.tenant()/tenantLazy()). Services connect asgebsecure_app, which is neither table owner norBYPASSRLS. - Composite foreign keys
(org_id, parent_id)so a row can never reference another tenant's row (e.g. a project in another org's workspace). - Explicit
org_idfilters in every repository query.
Pre-tenant lookups (credential resolution, OAuth state, Slack interactions) use narrow SECURITY DEFINER functions that return only what is needed to establish the tenant. Cross-tenant jobs (worker, retention, internal admin) use the owner connection through db.system() — a deliberately greppable call site.
Consequences#
- (+) Tests prove RLS blocks cross-tenant reads and writes even when a query omits its filter.
- (−) Every new tenant table must call
gs_enable_tenant_rls(); enforced in migration review.