From ffbe71716adcf83ed03041bf5b358da0c818738a Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 23 Nov 2026 07:17:19 -0600 Subject: [PATCH] id: static raw-binding boundary test for restricted routing (#3400) The raw USERS namespace binding is owned exclusively by the routing module; no application path may bypass userStub() to derive IDs or stubs. This enforces the design boundary (doc/2026-11-01_jurisdiction_ aware_user_store_design.md) by test: a future raw-binding bypass fails here instead of silently producing a second, empty store. Covers the raw-binding-bypass leg of #3400 acceptance with 5 tests; verified the first test fails when a stray idFromName call is added. Full hosted-worker suite 129/129 green, tsc clean. --- pkgs/id/hosted-worker/package.json | 2 +- .../test/routing-boundary.test.ts | 67 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 pkgs/id/hosted-worker/test/routing-boundary.test.ts diff --git a/pkgs/id/hosted-worker/package.json b/pkgs/id/hosted-worker/package.json index 60921f8999..a9902fb5ef 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/admission-gate.test.ts test/provisioning.test.ts test/provisioning-surface.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/export.test.ts", + "test": "node --experimental-transform-types --test test/routing.test.ts test/admission-gate.test.ts test/provisioning.test.ts test/provisioning-surface.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/export.test.ts test/routing-boundary.test.ts", "smoke": "node smoke/smoke.mjs" }, "devDependencies": { diff --git a/pkgs/id/hosted-worker/test/routing-boundary.test.ts b/pkgs/id/hosted-worker/test/routing-boundary.test.ts new file mode 100644 index 0000000000..24fe7623f1 --- /dev/null +++ b/pkgs/id/hosted-worker/test/routing-boundary.test.ts @@ -0,0 +1,67 @@ +// #3400: the raw USERS namespace binding is owned exclusively by the +// routing module (src/routing.ts). No application path may bypass it: +// object IDs and stubs must come from the SAME jurisdiction-restricted +// namespace via userStub(). This static test enforces that boundary so a +// future raw-binding bypass fails loudly here instead of silently +// producing a second, empty user store for a missing jurisdiction. +// +// Allowed touches: +// - src/routing.ts: the module itself. +// - src/worker.ts: the front door, which passes env.USERS into +// userStub() but never derives IDs or stubs itself. +// - src/test-harness.ts: the test-only entry point (wrangler.test.toml); +// it self-declares TEST-ONLY and the app wrangler.toml must not +// reference it. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { readdirSync, readFileSync, statSync } from "node:fs"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; + +const testDir = fileURLToPath(new URL(".", import.meta.url)); +const srcDir = join(testDir, "..", "src"); +const pkgDir = join(testDir, ".."); + +function collectTs(dir: string, out: string[] = []): string[] { + for (const entry of readdirSync(dir).sort()) { + const full = join(dir, entry); + if (statSync(full).isDirectory()) { + collectTs(full, out); + } else if (entry.endsWith(".ts")) { + out.push(full); + } + } + return out; +} + +function filesContaining(pattern: RegExp): string[] { + return collectTs(srcDir) + .filter((full) => pattern.test(readFileSync(full, "utf8"))) + .map((full) => full.slice(srcDir.length + 1).replace(/\\/g, "/")); +} + +test("namespace derivation exists only in the routing module and the test harness", () => { + assert.deepEqual(filesContaining(/idFromName/), ["routing.ts", "test-harness.ts"]); +}); + +test("jurisdiction restriction is selected only in the routing module and the test harness", () => { + assert.deepEqual(filesContaining(/\.jurisdiction\(/), ["routing.ts", "test-harness.ts"]); +}); + +test("the raw USERS binding is referenced only by the routing module and the front door", () => { + assert.deepEqual(filesContaining(/\bUSERS\b/), ["routing.ts", "worker.ts"]); +}); + +test("the front door never derives IDs or stubs itself", () => { + const worker = readFileSync(join(srcDir, "worker.ts"), "utf8"); + assert.ok(!/idFromName/.test(worker), "worker.ts must not call idFromName"); + assert.ok(!/\.jurisdiction\(/.test(worker), "worker.ts must not select a jurisdiction namespace"); + assert.ok(worker.includes("userStub(env.USERS,"), "worker.ts must route through userStub(env.USERS, ...)"); +}); + +test("the test harness stays test-only", () => { + const harness = readFileSync(join(srcDir, "test-harness.ts"), "utf8"); + assert.ok(harness.includes("TEST-ONLY"), "test-harness.ts must keep its TEST-ONLY declaration"); + const appConfig = readFileSync(join(pkgDir, "wrangler.toml"), "utf8"); + assert.ok(!appConfig.includes("test-harness"), "the app wrangler.toml must not reference the test harness"); +});