diff --git a/pkgs/id/hosted-worker/package.json b/pkgs/id/hosted-worker/package.json index 4d1d860e4d..f4b6c4287e 100644 --- a/pkgs/id/hosted-worker/package.json +++ b/pkgs/id/hosted-worker/package.json @@ -6,7 +6,7 @@ "scripts": { "dev": "wrangler dev", "typecheck": "tsc --noEmit", - "test": "node --experimental-transform-types --test test/routing.test.ts test/blobs.test.ts test/canonical-cbor.test.ts test/usage.test.ts test/currency.test.ts test/front-door.test.ts test/id-uri-vectors.test.ts test/bridge-blake3.test.ts test/bridge-event.test.ts test/bridge-range.test.ts test/bridge-store.test.ts test/bridge-session.test.ts test/bridge-do.test.ts", + "test": "node --experimental-transform-types --test test/routing.test.ts test/provisioning.test.ts test/blobs.test.ts test/canonical-cbor.test.ts test/usage.test.ts test/currency.test.ts test/front-door.test.ts test/id-uri-vectors.test.ts test/bridge-blake3.test.ts test/bridge-event.test.ts test/bridge-range.test.ts test/bridge-store.test.ts test/bridge-session.test.ts test/bridge-do.test.ts", "smoke": "node smoke/smoke.mjs" }, "devDependencies": { diff --git a/pkgs/id/hosted-worker/src/provisioning.ts b/pkgs/id/hosted-worker/src/provisioning.ts new file mode 100644 index 0000000000..0cb10e892b --- /dev/null +++ b/pkgs/id/hosted-worker/src/provisioning.ts @@ -0,0 +1,197 @@ +// Provisioning gate for hosted production enablement (#3403; design +// doc/2026-11-01_jurisdiction_aware_user_store_design.md, "Provisioning +// gate" step; decision record on #3403, 2026-11-22). +// +// Production signup and migration stay disabled until a deployment +// configuration is validated against deployed-account evidence (the +// #2285 canary results and Cloudflare API read-backs). This module is +// that validator: pure, synchronous, fail-closed. It consumes +// evidence; it never probes an account itself, and it never treats +// configuration claims as evidence. The #2285 Half 1 results showed a +// plain Worker binding can reach a bucket outside its declared +// jurisdiction and that wrangler's `--jurisdiction` flag can be +// silently ignored, so a binding's declared `jurisdiction` field is +// checked here only for consistency — verification is the bucket's +// actual jurisdiction as read back from the R2 API, plus the canary's +// result for restricted Durable Object namespaces on the actual +// account, deployed runtime, and generated types. +// +// There is no unrestricted fallback: enablement is granted only for +// the jurisdiction-restricted namespace and the one verified bucket +// for the configured jurisdiction. Nothing in this module authorizes +// a deployment by itself — it returns the exact plan a deployment may +// enable, or refuses with an explicit diagnostic. + +import { FailClosed, SUPPORTED_JURISDICTIONS, type Jurisdiction } from "./routing.ts"; + +/** What the deployed-account evidence says about one capability: + * "supported" only when the canary / API read-back proved it on the + * actual account; "unavailable" when it was checked and is not there; + * "unverified" when it was not (or could not be) checked. Anything + * other than "supported" fails closed. */ +export type SupportState = "supported" | "unavailable" | "unverified"; + +export interface JurisdictionEvidence { + /** Restricted DO namespace creation and runtime-reported + * jurisdiction on the actual account (#2285 mechanics 1–4). */ + durableObjects: SupportState; + /** Deployed runtime and generated types support for the + * restriction on this account / compatibility date. */ + runtimeTypes: SupportState; +} + +export interface BucketEvidence { + bucketName: string; + /** The bucket's actual jurisdiction as read back from the R2 API. + * null when the bucket does not exist or its metadata could not be + * read — both fail closed as unverified. */ + actualJurisdiction: string | null; +} + +/** Deployed-account evidence, gathered by provisioning tooling from + * the Cloudflare API and the #2285 canary. Never derived from the + * deployment configuration being validated. */ +export interface AccountEvidence { + jurisdictions: Partial>; + buckets: BucketEvidence[]; +} + +export interface BucketConfig { + binding: string; + bucketName: string; + /** The configuration's own claim (e.g. a wrangler binding field). + * Checked for consistency only; never verification (#2285). */ + declaredJurisdiction?: string; +} + +/** A request to enable hosted production signup/migration for one + * configured jurisdiction. Every field is a claim to be validated; + * absent fields fail closed. */ +export interface EnablementConfig { + jurisdiction?: string; + /** Must be exactly true: enablement is granted only on the + * jurisdiction-restricted DO namespace. Anything else asks for an + * unrestricted fallback, which is forbidden. */ + restrictedNamespace?: boolean; + durableObjectBinding?: string; + bucket?: BucketConfig; +} + +/** The validated enablement plan: the exact jurisdiction, DO binding, + * and verified bucket a production deployment may enable. */ +export interface EnablementPlan { + jurisdiction: Jurisdiction; + durableObjectBinding: string; + bucket: { binding: string; bucketName: string; jurisdiction: Jurisdiction }; +} + +function refuse(code: string, message: string): never { + throw new FailClosed(503, code, message); +} + +export function validateEnablement(config: EnablementConfig, evidence: AccountEvidence): EnablementPlan { + // 1. The jurisdiction must be explicitly configured and supported. + // The design's policy constant is not a silent runtime fallback: an + // absent value is a configuration error, not a default. + const rawJurisdiction = config.jurisdiction; + if (rawJurisdiction === undefined || rawJurisdiction === "") { + refuse( + "missing_jurisdiction", + "no jurisdiction is configured; production enablement requires an explicit jurisdiction", + ); + } + if (!(SUPPORTED_JURISDICTIONS as readonly string[]).includes(rawJurisdiction)) { + refuse( + "unsupported_jurisdiction", + `configured jurisdiction "${rawJurisdiction}" is not supported; failing closed`, + ); + } + const jurisdiction = rawJurisdiction as Jurisdiction; + + // 2. No unrestricted fallback: enablement exists only on the + // jurisdiction-restricted namespace. + if (config.restrictedNamespace !== true) { + refuse( + "unrestricted_fallback_forbidden", + "production enablement requires the jurisdiction-restricted Durable Object namespace; unrestricted fallback is forbidden", + ); + } + + // 3. Deployed-account evidence must prove the restriction works on + // this account, runtime, and generated types. + const jx = evidence.jurisdictions[jurisdiction]; + if (!jx) { + refuse( + "jurisdiction_unverified", + `no deployed-account evidence for jurisdiction "${jurisdiction}"; failing closed`, + ); + } + if (jx.durableObjects === "unavailable") { + refuse( + "jurisdiction_unavailable", + `jurisdiction-restricted Durable Objects are unavailable for "${jurisdiction}" on the deployed account; failing closed`, + ); + } + if (jx.durableObjects !== "supported") { + refuse( + "jurisdiction_unverified", + `restricted Durable Object support for "${jurisdiction}" is unverified on the deployed account; failing closed`, + ); + } + if (jx.runtimeTypes === "unavailable") { + refuse( + "unsupported_runtime_types", + `the deployed runtime/types do not support jurisdiction restriction for "${jurisdiction}"; failing closed`, + ); + } + if (jx.runtimeTypes !== "supported") { + refuse( + "runtime_types_unverified", + `runtime/types support for jurisdiction restriction for "${jurisdiction}" is unverified; failing closed`, + ); + } + + // 4. The DO binding must be configured. + if (!config.durableObjectBinding) { + refuse( + "missing_do_binding", + "no Durable Object binding is configured for the restricted namespace; failing closed", + ); + } + + // 5. The bucket for this jurisdiction must be configured, and its + // ACTUAL jurisdiction — read back from the R2 API, never the + // binding's declared field — must match the configured one. + const bucket = config.bucket; + if (!bucket) { + refuse( + "bucket_missing", + `no R2 bucket is configured for jurisdiction "${jurisdiction}"; failing closed`, + ); + } + if (bucket.declaredJurisdiction !== undefined && bucket.declaredJurisdiction !== jurisdiction) { + refuse( + "bucket_jurisdiction_mismatch", + `bucket binding "${bucket.binding}" declares jurisdiction "${bucket.declaredJurisdiction}" but the configured jurisdiction is "${jurisdiction}"; failing closed`, + ); + } + const bucketEvidence = evidence.buckets.find((b) => b.bucketName === bucket.bucketName); + if (!bucketEvidence || bucketEvidence.actualJurisdiction === null) { + refuse( + "bucket_unverified", + `bucket "${bucket.bucketName}" has no verified jurisdiction read-back from the R2 API; a declared binding jurisdiction is not verification; failing closed`, + ); + } + if (bucketEvidence.actualJurisdiction !== jurisdiction) { + refuse( + "bucket_jurisdiction_mismatch", + `bucket "${bucket.bucketName}" actually resides in jurisdiction "${bucketEvidence.actualJurisdiction}" per the R2 API, not the configured "${jurisdiction}"; failing closed`, + ); + } + + return { + jurisdiction, + durableObjectBinding: config.durableObjectBinding, + bucket: { binding: bucket.binding, bucketName: bucket.bucketName, jurisdiction }, + }; +} diff --git a/pkgs/id/hosted-worker/test/provisioning.test.ts b/pkgs/id/hosted-worker/test/provisioning.test.ts new file mode 100644 index 0000000000..7c9f1ccd44 --- /dev/null +++ b/pkgs/id/hosted-worker/test/provisioning.test.ts @@ -0,0 +1,178 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { FailClosed } from "../src/routing.ts"; +import { + validateEnablement, + type AccountEvidence, + type EnablementConfig, +} from "../src/provisioning.ts"; + +// Configuration tests for the #3403 provisioning gate. Every refusal +// case is one of the acceptance failure modes: production must not +// enable signup or migration with a missing/unverified jurisdiction, +// unsupported runtime/types, or a missing/mismatched bucket — and +// never via an unrestricted fallback. + +const US_CONFIG: EnablementConfig = { + jurisdiction: "us", + restrictedNamespace: true, + durableObjectBinding: "USERS", + bucket: { binding: "BLOBS_US", bucketName: "user-blobs-us", declaredJurisdiction: "us" }, +}; + +const EU_CONFIG: EnablementConfig = { + jurisdiction: "eu", + restrictedNamespace: true, + durableObjectBinding: "USERS", + bucket: { binding: "BLOBS_EU", bucketName: "user-blobs-eu", declaredJurisdiction: "eu" }, +}; + +function evidence( + jurisdiction: "us" | "eu", + bucketName: string, + actualJurisdiction: string | null, +): AccountEvidence { + return { + jurisdictions: { + [jurisdiction]: { durableObjects: "supported", runtimeTypes: "supported" }, + }, + buckets: [{ bucketName, actualJurisdiction }], + }; +} + +const US_EVIDENCE = evidence("us", "user-blobs-us", "us"); +const EU_EVIDENCE = evidence("eu", "user-blobs-eu", "eu"); + +function refusalCode(fn: () => unknown): string { + try { + fn(); + } catch (e) { + if (e instanceof FailClosed) return e.code; + throw e; + } + throw new Error("expected a FailClosed refusal, but validation passed"); +} + +test("verified us configuration yields the exact enablement plan", () => { + assert.deepEqual(validateEnablement(US_CONFIG, US_EVIDENCE), { + jurisdiction: "us", + durableObjectBinding: "USERS", + bucket: { binding: "BLOBS_US", bucketName: "user-blobs-us", jurisdiction: "us" }, + }); +}); + +test("verified eu configuration yields the exact enablement plan", () => { + assert.deepEqual(validateEnablement(EU_CONFIG, EU_EVIDENCE), { + jurisdiction: "eu", + durableObjectBinding: "USERS", + bucket: { binding: "BLOBS_EU", bucketName: "user-blobs-eu", jurisdiction: "eu" }, + }); +}); + +test("missing jurisdiction fails closed (no silent policy default)", () => { + const { jurisdiction: _drop, ...noJurisdiction } = US_CONFIG; + assert.equal(refusalCode(() => validateEnablement(noJurisdiction, US_EVIDENCE)), "missing_jurisdiction"); + assert.equal( + refusalCode(() => validateEnablement({ ...US_CONFIG, jurisdiction: "" }, US_EVIDENCE)), + "missing_jurisdiction", + ); +}); + +test("unsupported jurisdiction fails closed", () => { + assert.equal( + refusalCode(() => validateEnablement({ ...US_CONFIG, jurisdiction: "apac" }, US_EVIDENCE)), + "unsupported_jurisdiction", + ); +}); + +test("no unrestricted fallback: restricted namespace must be required", () => { + assert.equal( + refusalCode(() => validateEnablement({ ...US_CONFIG, restrictedNamespace: false }, US_EVIDENCE)), + "unrestricted_fallback_forbidden", + ); + const { restrictedNamespace: _drop, ...noRestriction } = US_CONFIG; + assert.equal(refusalCode(() => validateEnablement(noRestriction, US_EVIDENCE)), "unrestricted_fallback_forbidden"); +}); + +test("jurisdiction with no deployed-account evidence fails closed as unverified", () => { + const empty: AccountEvidence = { jurisdictions: {}, buckets: US_EVIDENCE.buckets }; + assert.equal(refusalCode(() => validateEnablement(US_CONFIG, empty)), "jurisdiction_unverified"); +}); + +test("unavailable US jurisdiction fails closed with an explicit diagnostic", () => { + const usUnavailable: AccountEvidence = { + jurisdictions: { us: { durableObjects: "unavailable", runtimeTypes: "supported" } }, + buckets: US_EVIDENCE.buckets, + }; + assert.equal(refusalCode(() => validateEnablement(US_CONFIG, usUnavailable)), "jurisdiction_unavailable"); +}); + +test("unverified restricted-namespace support fails closed", () => { + const unverified: AccountEvidence = { + jurisdictions: { us: { durableObjects: "unverified", runtimeTypes: "supported" } }, + buckets: US_EVIDENCE.buckets, + }; + assert.equal(refusalCode(() => validateEnablement(US_CONFIG, unverified)), "jurisdiction_unverified"); +}); + +test("unsupported runtime/types fail closed", () => { + const unsupported: AccountEvidence = { + jurisdictions: { us: { durableObjects: "supported", runtimeTypes: "unavailable" } }, + buckets: US_EVIDENCE.buckets, + }; + assert.equal(refusalCode(() => validateEnablement(US_CONFIG, unsupported)), "unsupported_runtime_types"); +}); + +test("unverified runtime/types fail closed", () => { + const unverified: AccountEvidence = { + jurisdictions: { us: { durableObjects: "supported", runtimeTypes: "unverified" } }, + buckets: US_EVIDENCE.buckets, + }; + assert.equal(refusalCode(() => validateEnablement(US_CONFIG, unverified)), "runtime_types_unverified"); +}); + +test("missing Durable Object binding fails closed", () => { + const { durableObjectBinding: _drop, ...noBinding } = US_CONFIG; + assert.equal(refusalCode(() => validateEnablement(noBinding, US_EVIDENCE)), "missing_do_binding"); +}); + +test("missing bucket fails closed", () => { + const { bucket: _drop, ...noBucket } = US_CONFIG; + assert.equal(refusalCode(() => validateEnablement(noBucket, US_EVIDENCE)), "bucket_missing"); +}); + +test("bucket whose declared jurisdiction disagrees with the configuration fails closed", () => { + const config: EnablementConfig = { + ...US_CONFIG, + bucket: { binding: "BLOBS_EU", bucketName: "user-blobs-eu", declaredJurisdiction: "eu" }, + }; + assert.equal(refusalCode(() => validateEnablement(config, US_EVIDENCE)), "bucket_jurisdiction_mismatch"); +}); + +test("bucket with no R2 API read-back fails closed as unverified", () => { + const noReadback: AccountEvidence = { jurisdictions: US_EVIDENCE.jurisdictions, buckets: [] }; + assert.equal(refusalCode(() => validateEnablement(US_CONFIG, noReadback)), "bucket_unverified"); +}); + +test("bucket whose API metadata cannot be read fails closed as unverified", () => { + assert.equal( + refusalCode(() => validateEnablement(US_CONFIG, evidence("us", "user-blobs-us", null))), + "bucket_unverified", + ); +}); + +test("bucket actually residing in another jurisdiction fails closed (#2285 mixed R2 result)", () => { + // #2285 Half 1: a plain binding reached the EU bucket from a US + // configuration. The API read-back, not the binding, decides. + assert.equal( + refusalCode(() => validateEnablement(US_CONFIG, evidence("us", "user-blobs-us", "eu"))), + "bucket_jurisdiction_mismatch", + ); +}); + +test("a declared binding jurisdiction that agrees is still not verification (#2285)", () => { + // The config claims "us" everywhere, but there is no API read-back + // for the bucket: the declared field must not substitute for it. + const claimsOnly: AccountEvidence = { jurisdictions: US_EVIDENCE.jurisdictions, buckets: [] }; + assert.equal(refusalCode(() => validateEnablement(US_CONFIG, claimsOnly)), "bucket_unverified"); +});