/** * `src/auth/internal-service-tenant.ts` — server-side tenant scope for the * machine-to-machine principal authenticated by `X-Internal-Token`. * * WHY THIS EXISTS (P0, 2026-11-07) * -------------------------------- * `X-Internal-Token` proves that the caller is the SIAX engine process. It * does NOT prove which tenant that process may act for. The first attempt at * fixing the "internal token bypasses the verified-tenant guard" hole copied a * CLIENT HEADER (`X-0pt-Internal-Tenant`) or fell back to a single * `INTERNAL_TENANT_ID` env var: * * const internalTenant = * req.headers.get('x-0pt-internal-tenant') ?? process.env.INTERNAL_TENANT_ID; * * That is not an authorization source. Any holder of the shared internal token * could still name any tenant in a request header, which is exactly the * cross-tenant write the fix was supposed to stop (tenant_id * 999999999999999999 returned 201 before). * * The correct model mirrors a verified Bearer token: the principal's tenant * authority is a SERVER-SIDE fact, never a request field. * * - `INTERNAL_SERVICE_TENANTS` is a comma-separated allowlist configured on * the 0pt deployment (env/secret), i.e. "the tenants this engine instance * is entitled to write for". * - When the allowlist is empty, the internal principal has NO tenant * authority: tenant-scoped writes fail closed (403). Routes that do not * carry a tenant (evidence outbox dispatch, ER candidates, search * definitions) are unaffected. * - A single allowlisted tenant is ALSO placed in `x-0pt-verified-tenant` * by middleware so existing single-tenant checks keep working; two or more * are carried in `x-0pt-verified-service-tenants` and the tenant guard * checks membership. * - Any inbound value for either header is stripped by middleware before * forwarding, so a client can never forge tenant authority. * * No client header is read here. `X-0pt-Internal-Tenant` is deliberately not a * contract. */ /** Env var: comma-separated tenants this internal service principal may assert. */ export const INTERNAL_SERVICE_TENANTS_VAR = 'INTERNAL_SERVICE_TENANTS'; /** * Request header installed by middleware.ts ONLY (never accepted from a * client, and stripped from every inbound request). Carries the full * server-configured service tenant scope; may be the empty string, which means * "service principal without tenant authority". */ export const SERVICE_TENANT_SCOPE_HEADER = 'x-0pt-verified-service-tenants'; /** * Request header installed by middleware.ts ONLY: the CL0UD workload token's * verified capability list, comma-separated. Stripped from every inbound * request like the tenant scope. */ export const SERVICE_CAPABILITIES_HEADER = 'x-0pt-verified-capabilities'; /** Parse a comma-separated tenant allowlist (trimmed, de-duplicated, ordered). */ export function parseServiceTenants(value: string | undefined | null): string[] { if (!value) return []; const tenants: string[] = []; for (const part of value.split(',')) { const tenant = part.trim(); if (tenant && !tenants.includes(tenant)) tenants.push(tenant); } return tenants; } /** Read the configured service tenant allowlist from the environment. */ export function resolveInternalServiceTenants( // @types/node 22's ProcessEnv has only an index signature, so a direct // default is not assignable to the narrow named-property shape. This is a // type-only view of the same object; runtime behaviour is unchanged. env: { INTERNAL_SERVICE_TENANTS?: string } = process.env as { INTERNAL_SERVICE_TENANTS?: string }, ): string[] { return parseServiceTenants(env.INTERNAL_SERVICE_TENANTS); } /** Read the scope middleware installed on the request (already stripped+set). */ export function readServiceTenantScope(headers: { get(name: string): string | null | undefined }): string[] { return parseServiceTenants(headers.get(SERVICE_TENANT_SCOPE_HEADER)); }