diff --git a/pkgs/id/hosted-worker/src/account-directory.ts b/pkgs/id/hosted-worker/src/account-directory.ts index b63ca665b0..f8625c0982 100644 --- a/pkgs/id/hosted-worker/src/account-directory.ts +++ b/pkgs/id/hosted-worker/src/account-directory.ts @@ -32,6 +32,42 @@ export interface NewHostedAccount { jurisdiction?: string | null; } +/** + * Profile fields a normal update may carry. Only `jurisdiction` is + * recognised here, and only to guard it: the design + * (doc/2026-11-01_jurisdiction_aware_user_store_design.md ยง1โ€“4) says + * normal profile updates cannot change `jurisdiction`, even before + * first DO creation โ€” only the migration state machine may change it. + * Any other profile content lives outside this record. + */ +export interface HostedAccountProfilePatch { + jurisdiction?: string | null; +} + +/** + * Apply a normal profile update to a hosted account record. Fails + * closed (throws `FailClosed`) when the patch attempts to change + * `jurisdiction` to a different value. A patch with the same + * jurisdiction, no jurisdiction, or a null jurisdiction returns the + * record with `jurisdiction`, `jurisdictionPending`, `migrationId`, + * `phase`, and `routingEpoch` unchanged โ€” only the migration state + * machine may change those. The input record is never mutated. + */ +export function updateHostedAccountRecord( + record: HostedAccountRecord, + patch: HostedAccountProfilePatch, +): HostedAccountRecord { + if (patch.jurisdiction !== undefined && patch.jurisdiction !== null && patch.jurisdiction !== record.jurisdiction) { + throw new FailClosed( + 403, + "jurisdiction_immutable", + "profile updates cannot change hosted jurisdiction; only the migration state machine may change it", + ); + } + + return { ...record }; +} + /** * Validate the identity and explicit jurisdiction before constructing the * first directory row. There is deliberately no policy default: missing or diff --git a/pkgs/id/hosted-worker/test/routing.test.ts b/pkgs/id/hosted-worker/test/routing.test.ts index f9ad07a77d..a6b0b28c05 100644 --- a/pkgs/id/hosted-worker/test/routing.test.ts +++ b/pkgs/id/hosted-worker/test/routing.test.ts @@ -1,7 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { FailClosed, parseBindingId, parseJurisdiction, userStub } from "../src/routing.ts"; -import { createHostedAccountRecord } from "../src/account-directory.ts"; +import { createHostedAccountRecord, updateHostedAccountRecord } from "../src/account-directory.ts"; const BINDING = "0123456789abcdef0123456789abcdef"; @@ -62,6 +62,38 @@ test("hosted account metadata rejects invalid identity inputs", () => { ); }); +test("profile update changing jurisdiction fails closed and leaves the record unchanged", () => { + const record = createHostedAccountRecord({ + subjectKey: "provider-subject-hash", + bindingId: BINDING, + jurisdiction: "us", + }); + assert.throws( + () => updateHostedAccountRecord(record, { jurisdiction: "eu" }), + (error: unknown) => error instanceof FailClosed, + ); + assert.deepEqual(record, { + subjectKey: "provider-subject-hash", + bindingId: BINDING, + jurisdiction: "us", + jurisdictionPending: null, + migrationId: null, + phase: "stable", + routingEpoch: 0, + }); +}); + +test("profile update with the same or no jurisdiction succeeds with routing metadata unchanged", () => { + const record = createHostedAccountRecord({ + subjectKey: "provider-subject-hash", + bindingId: BINDING, + jurisdiction: "us", + }); + assert.deepEqual(updateHostedAccountRecord(record, { jurisdiction: "us" }), record); + assert.deepEqual(updateHostedAccountRecord(record, {}), record); + assert.deepEqual(updateHostedAccountRecord(record, { jurisdiction: null }), record); +}); + test("userStub takes id and stub from the restricted namespace only", () => { const calls: string[] = []; const restricted = {