Skip to content
Merged
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
35 changes: 35 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,41 @@ export interface NewHostedAccount {
jurisdiction?: string | null;
}

/**
* In-memory hosted account directory keyed by the unique pseudonymous
* subject key (design doc/2026-11-01_jurisdiction_aware_user_store_design.md:
* "Duplicate signup reads the existing row; it never overwrites it or
* resets the policy").
*
* This is the contract the persistent unique-subject transaction slice
* will implement against durable storage; it holds no production state
* and enables no signup or migration path on its own.
*/
export class HostedAccountDirectory {
private readonly rows = new Map<string, HostedAccountRecord>();

get size(): number {
return this.rows.size;
}

signup(input: NewHostedAccount): HostedAccountRecord {
const existing = this.rows.get(input.subjectKey);
if (existing !== undefined) {
if (existing.bindingId !== input.bindingId) {
throw new FailClosed(
409,
"duplicate_subject_conflict",
"subject key is already bound to a different binding id; failing closed",
);
}
return existing;
}
const record = createHostedAccountRecord(input);
this.rows.set(record.subjectKey, record);
return record;
Comment on lines +65 to +66

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Prevent returned records from mutating directory state

Every successful signup returns the mutable object stored in rows. A caller can therefore assign record.jurisdiction = "us" and silently change the policy returned by subsequent duplicate signups, or change record.bindingId and alter which binding passes the conflict check. This bypasses the preservation and immutable-binding contract without another directory operation. Although current callers are tests only, the new API already permits this invariant violation; return detached snapshots or enforce runtime immutability on stored records.

Useful? React with 👍 / 👎.

}
}

/**
* Validate the identity and explicit jurisdiction before constructing the
* first directory row. There is deliberately no policy default: missing or
Expand Down
38 changes: 37 additions & 1 deletion pkgs/id/hosted-worker/test/routing.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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, HostedAccountDirectory } from "../src/account-directory.ts";

const BINDING = "0123456789abcdef0123456789abcdef";
const BINDING_OTHER = "fedcba9876543210fedcba9876543210";

test("binding id must be a minted 32-hex id", () => {
assert.equal(parseBindingId(BINDING), BINDING);
Expand Down Expand Up @@ -62,6 +63,41 @@ test("hosted account metadata rejects invalid identity inputs", () => {
);
});

test("duplicate signup reads the preserved record instead of overwriting it", () => {
const directory = new HostedAccountDirectory();
const first = directory.signup({
subjectKey: "provider-subject-hash",
bindingId: BINDING,
jurisdiction: "eu",
});
const second = directory.signup({
subjectKey: "provider-subject-hash",
bindingId: BINDING,
jurisdiction: "eu",
});
assert.deepEqual(second, first);
Comment on lines +74 to +78

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 directory invariants with generated state-machine tests

The new directory introduces uniqueness and policy-preservation invariants, but its coverage consists only of fixed examples, contrary to the repository's requirement for property/model-based invariant tests. In particular, this preservation test repeats the same jurisdiction and compares against the returned record rather than an independent snapshot, so reconstructing and overwriting the row on every duplicate would still pass. Add generated signup sequences checked against a named reference model, including changed jurisdictions, conflicting bindings, and distinct subjects.

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

Useful? React with 👍 / 👎.

assert.equal(directory.size, 1);
});

test("duplicate signup with a conflicting binding id fails closed", () => {
const directory = new HostedAccountDirectory();
directory.signup({
subjectKey: "provider-subject-hash",
bindingId: BINDING,
jurisdiction: "eu",
});
assert.throws(
() =>
directory.signup({
subjectKey: "provider-subject-hash",
bindingId: BINDING_OTHER,
jurisdiction: "eu",
}),
(error: unknown) => error instanceof FailClosed && error.code === "duplicate_subject_conflict",
);
assert.equal(directory.size, 1);
});

test("userStub takes id and stub from the restricted namespace only", () => {
const calls: string[] = [];
const restricted = {
Expand Down