# ADR 0008 — Secure multi-tenant platform

**Status:** accepted. **Date:** 2026-12-03.

## Context
Isolation between tenants must not depend only on coding discipline. It needs tenant context in
HTTP, the database and background work, auditing, quotas, feature flags, retention, export and isolation tests.

## Decision
- **The tenant still comes only from the verified identity.** The tenant registry (`fellow_tenants`) adds policy
  (status, mandatory registration, domain), enabled in `app.ts` (`tenancy`), and fails closed (503) if it cannot be read.
- **Append-only auditing in the database** (a trigger that rejects update/delete), **hash-chained per tenant**, verifiable
  with `fellow audit --verify`. The trigger prevents accidents; the chaining detects tampering by anyone with database access.
  Writes use their own transaction and a per-tenant advisory lock (linear chain) and never bring down the audited operation.
- **Denials are audited by default** (`access.denied`), successes per route with `audit: true`.
- **Atomic quotas** with `insert ... on conflict ... where` (no race condition), inside the consumer's transaction.
- **Feature flags** that are global, per tenant and with deterministic percentage rollout (hash of flag + tenant); a route behind a disabled
  flag responds 404.
- **Retention declared in the manifest** and applied per tenant within that tenant's context; a module only expires its own tables.
- **Export** requires a `tenant.cross` human approval bound to the tenant id; it never exports session hashes.
- **Verification on two fronts**: `fellow doctor` reads the PostgreSQL catalog (RLS enabled, forced and with a tenant
  policy on every table with `tenant_id`); `checkIsolation` calls every route as another tenant with all permissions.
- **Outbound and secrets security**: `safeFetch` against SSRF (private addresses, metadata, redirects);
  `FELLOW_SECRET` rotation with `FELLOW_SECRET_PREVIOUS`; structured logger with redaction.

## Consequences
- Global tables with `tenant_id` (sessions, flags) must be declared with `-- fellow:global`.
- `safeFetch` does not eliminate DNS rebinding between the check and the connection; workers that fetch untrusted URLs
  must have egress filtering at the network level.
- Background jobs use `captureContext`/`runWithEnvelope`; the durable queue is described in ADR 0010.
- Retention iterates over tenants in the registry: data from unregistered tenants is not expired.
