|
| 1 | +# Tenant Isolation — Architecture & Guarantees |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +NiffyInsure supports multi-tenant (white-label) deployments via **logical row-level |
| 6 | +isolation**. All tenants share the same PostgreSQL database and Soroban contract. |
| 7 | +Physical isolation (separate DB or contract per tenant) requires a separate deployment. |
| 8 | + |
| 9 | +## Isolation level |
| 10 | + |
| 11 | +| Layer | Isolation type | Notes | |
| 12 | +|---|---|---| |
| 13 | +| Database | Logical (row-level filter) | `tenantId` column on `claims` and `policies` | |
| 14 | +| Cache (Redis) | Logical (key namespace) | Cache keys prefixed with `tenantId` | |
| 15 | +| Soroban contract | None (shared) | Contract is tenant-unaware; isolation is off-chain only | |
| 16 | +| Auth (JWT) | None (shared) | JWTs are not tenant-scoped in the current implementation | |
| 17 | + |
| 18 | +**Operators must understand**: this is logical separation only. A bug in the |
| 19 | +application layer could theoretically expose cross-tenant data. For strict |
| 20 | +physical isolation, deploy separate instances per tenant. |
| 21 | + |
| 22 | +## How it works |
| 23 | + |
| 24 | +### 1. Tenant resolution (per request) |
| 25 | + |
| 26 | +`TenantMiddleware` runs on every request and populates the REQUEST-scoped |
| 27 | +`TenantContextService` with the resolved `tenantId`: |
| 28 | + |
| 29 | +1. `x-tenant-id` header (explicit — used by API integrations) |
| 30 | +2. Subdomain: `<tenantId>.niffyinsur.com` → extracted from `Host` header |
| 31 | + |
| 32 | +Tenant IDs must match `/^[a-z0-9][a-z0-9-]{1,62}[a-z0-9]$|^[a-z0-9]{3}$/`. |
| 33 | +Invalid values are silently ignored (tenantId stays null). |
| 34 | + |
| 35 | +### 2. Query scoping |
| 36 | + |
| 37 | +Every repository query on a tenant-scoped model calls `claimTenantWhere()` or |
| 38 | +`policyTenantWhere()` which merges `{ tenantId }` into the Prisma `where` clause. |
| 39 | + |
| 40 | +```typescript |
| 41 | +// Example — claims list |
| 42 | +const where = claimTenantWhere(tenantId, { status: 'PENDING' }); |
| 43 | +// → { tenantId: 'acme', status: 'PENDING' } (multi-tenant) |
| 44 | +// → { status: 'PENDING' } (single-tenant, tenantId=null) |
| 45 | +``` |
| 46 | + |
| 47 | +### 3. Ownership assertion after findUnique |
| 48 | + |
| 49 | +After fetching a record by primary key, `assertTenantOwnership()` verifies the |
| 50 | +record's `tenantId` matches the request tenant. Returns 404 (not 403) to avoid |
| 51 | +leaking resource existence to other tenants. |
| 52 | + |
| 53 | +```typescript |
| 54 | +const claim = await prisma.claim.findUnique({ where: { id } }); |
| 55 | +assertTenantOwnership(claim, tenantId, `Claim ${id}`); |
| 56 | +``` |
| 57 | + |
| 58 | +### 4. Cache namespacing |
| 59 | + |
| 60 | +Cache keys include the tenantId to prevent cross-tenant cache poisoning: |
| 61 | + |
| 62 | +``` |
| 63 | +claims:list:acme:start:20:all |
| 64 | +claims:detail:acme:42 |
| 65 | +``` |
| 66 | + |
| 67 | +## Single-tenant mode (default) |
| 68 | + |
| 69 | +When `TENANT_RESOLUTION_ENABLED=false` (the default): |
| 70 | + |
| 71 | +- `TenantMiddleware` is a no-op |
| 72 | +- `tenantId` is always `null` |
| 73 | +- `tenantFilter(null)` returns `{}` |
| 74 | +- All queries behave identically to pre-tenant code paths |
| 75 | +- No performance overhead |
| 76 | + |
| 77 | +## Enabling multi-tenant mode |
| 78 | + |
| 79 | +```env |
| 80 | +TENANT_RESOLUTION_ENABLED=true |
| 81 | +TENANT_BASE_DOMAIN=niffyinsur.com |
| 82 | +``` |
| 83 | + |
| 84 | +Run the Prisma migration to add `tenantId` columns and indexes: |
| 85 | + |
| 86 | +```bash |
| 87 | +npx prisma migrate dev --name add-tenant-id |
| 88 | +``` |
| 89 | + |
| 90 | +## Database indexes |
| 91 | + |
| 92 | +The following composite indexes are added to support tenant-scoped queries |
| 93 | +without full table scans: |
| 94 | + |
| 95 | +``` |
| 96 | +claims: (tenantId), (tenantId, status), (tenantId, createdAt, id) |
| 97 | +policies: (tenantId), (tenantId, isActive), (tenantId, createdAt, id) |
| 98 | +``` |
| 99 | + |
| 100 | +The `(tenantId, createdAt, id)` index matches the keyset pagination query shape: |
| 101 | +`WHERE tenantId = ? AND (createdAt, id) < (?, ?) ORDER BY createdAt DESC, id DESC`. |
| 102 | + |
| 103 | +## Legal considerations |
| 104 | + |
| 105 | +If handling user data on behalf of a tenant (white-label partner), a Data |
| 106 | +Processing Agreement (DPA) is required under GDPR and similar regulations. |
| 107 | +Consult qualified legal counsel before onboarding tenants who process EU/UK |
| 108 | +personal data. Each tenant's users should be informed of the sub-processor |
| 109 | +relationship in the tenant's own privacy policy. |
| 110 | + |
| 111 | +## Limitations |
| 112 | + |
| 113 | +- Soroban contract events are not tenant-scoped. The indexer assigns `tenantId` |
| 114 | + based on configuration, not on-chain data. |
| 115 | +- Votes are not tenant-scoped (the `votes` table has no `tenantId`). Votes are |
| 116 | + linked to claims which are tenant-scoped, so cross-tenant vote reads are |
| 117 | + prevented transitively. |
| 118 | +- Admin endpoints currently bypass tenant scoping. Admin operators can see all |
| 119 | + tenants' data. Restrict admin access accordingly. |
0 commit comments