Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions pkgs/id/hosted-worker/src/account-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 33 additions & 1 deletion pkgs/id/hosted-worker/test/routing.test.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -62,6 +62,38 @@ test("hosted account metadata rejects invalid identity inputs", () => {
);
});

test("profile update changing jurisdiction fails closed and leaves the record unchanged", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Cover profile-update immutability with property tests

The new helper enforces a routing-metadata invariant, but these tests exercise only one us record and one us→eu rejection. The testing doctrine in root AGENTS.md requires property tests for meaningful invariants; fixed examples alone do not satisfy that requirement. Add generated checks that differing non-null jurisdictions fail closed without mutating the input, while accepted patches preserve the complete record across supported jurisdictions.

AGENTS.md reference: AGENTS.md:L37-L39

Useful? React with 👍 / 👎.

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 = {
Expand Down