Merge branch 'main' into fix/canonical-iso-timestamps-20261107
CI (SIAX Cloud) / sonar (pull_request) Skipped
masterplan-lock / check (push) Successful in 11s
masterplan-lock / check (pull_request) Successful in 9s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 14s
CI (SIAX Cloud) / Component conformance (blocking) (pull_request) Successful in 19s
CI (SIAX Cloud) / security (pull_request) Successful in 1m37s
CI (SIAX Cloud) / integration (pull_request) Successful in 1m38s
CI (SIAX Cloud) / quality (pull_request) Successful in 10m10s
CI (SIAX Cloud) / sonar (pull_request) Skipped
masterplan-lock / check (push) Successful in 11s
masterplan-lock / check (pull_request) Successful in 9s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 14s
CI (SIAX Cloud) / Component conformance (blocking) (pull_request) Successful in 19s
CI (SIAX Cloud) / security (pull_request) Successful in 1m37s
CI (SIAX Cloud) / integration (pull_request) Successful in 1m38s
CI (SIAX Cloud) / quality (pull_request) Successful in 10m10s
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* `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';
|
||||
|
||||
/** 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(
|
||||
env: { INTERNAL_SERVICE_TENANTS?: string } = process.env,
|
||||
): 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));
|
||||
}
|
||||
@@ -29,6 +29,7 @@
|
||||
* authz hole without changing every route's request contract).
|
||||
*/
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
import { readServiceTenantScope } from "@/src/auth/internal-service-tenant";
|
||||
|
||||
export const VERIFIED_TENANT_HEADER = "x-0pt-verified-tenant";
|
||||
|
||||
@@ -52,6 +53,26 @@ export function assertTenantMatchesVerified(
|
||||
tenantId: string | null | undefined,
|
||||
): NextResponse | null {
|
||||
const verified = request.headers.get(VERIFIED_TENANT_HEADER);
|
||||
// Internal service principal: tenant authority is the SERVER-CONFIGURED
|
||||
// allowlist middleware installed (never a client header). The scope header is
|
||||
// present even when the allowlist is empty, which means "no tenant
|
||||
// authority" -> fail closed. Routes that do not take a tenant never call
|
||||
// this guard, so evidence dispatch / ER / search definitions keep working.
|
||||
if (request.headers.get("x-0pt-verified-service-tenants") !== null) {
|
||||
if (!tenantId) return null; // handlers already 400 on a missing tenant_id separately
|
||||
const scope = readServiceTenantScope(request.headers);
|
||||
if (!scope.includes(tenantId)) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "OPT_ACCESS_DENIED",
|
||||
message: "tenant_id is outside the internal service principal's configured tenant scope",
|
||||
details: { requested_tenant: tenantId, service_tenant_scope: scope },
|
||||
},
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (!verified) return null;
|
||||
if (!tenantId) return null; // handlers already 400 on a missing tenant_id separately
|
||||
if (tenantId !== verified) {
|
||||
|
||||
Reference in New Issue
Block a user