An owner's block ends a guest's open socket within a minute
Jonas Templestein, 2026-11-26 15:26 UTC · bd2b13e4edf49cdd4e8fed60a68a34754bc369bf · parent f085961
5 files changed
- M core/auth-worker/src/guest-sign-in.test.ts
- M core/auth-worker/src/members.ts
- M core/itx/src/person-context.test.ts
- M core/itx/src/person-context.ts
- M core/lib/src/sdk/auth.ts
M core/auth-worker/src/guest-sign-in.test.ts
@@ -482,7 +482,13 @@ expect(factsOf(blocked).flatMap((fact) => fact.events.map((event) => event.type))).toEqual([ "events.iterate.com/account/grant-ended", "events.iterate.com/account/person-blocked", + "events.iterate.com/account/person-blocked", ]); + // the block is told on the guest's own context too: an open socket of theirs ends by it + expect(factsOf(blocked).at(-1)).toMatchObject({ + path: `/users/${bob.userId}`, + events: [{ type: "events.iterate.com/account/person-blocked" }], + }); expect(await tokenIdentity(env, token, MEET)).toBeNull(); expect(await tokenIdentity(env, evesToken, MEET)).toMatchObject({ name: "Eve" }); // the cookie earns nothing more, and the form says so with one event on the guest's context
M core/auth-worker/src/members.ts
@@ -510,6 +510,13 @@ type: "events.iterate.com/account/person-blocked", payload: { email }, }); + // and on the blocked person's own context: a socket of theirs that is open in the mind ends by + // it (core/itx/src/person-context.ts `blockWatchOf`), since no token is asked for again there + if (person) + facts.add(person.id, { + type: "events.iterate.com/account/person-blocked", + payload: { email }, + }); console.info({ event: "auth.person-blocked", by: actor.userId, known: Boolean(person) }); return { facts,
M core/itx/src/person-context.test.ts
@@ -8,6 +8,8 @@ ADMITS_EVERY_MS, ADMITTED_GUEST_SOCKET_MS, admissionOf, + BLOCK_EVERY_MS, + blockWatchOf, asAFacetIsTold, asAPersonReads, PersonContextRpcTarget, @@ -898,6 +900,62 @@ live = true; clock += ADMITS_EVERY_MS; expect(await onFrame!()).toBe("your access to this context ended"); +}); + +// The block is on the guest's own context, written by the auth Worker. +test("an owner's block ends a guest's open socket within a minute of traffic, and an earlier block that was taken back does not", async () => { + const opened = Date.parse("2026-11-26T15:00:00.000Z"); + const blockedAt = (iso: string) => ({ + type: "events.iterate.com/account/person-blocked", + createdAt: iso, + payload: { email: "guest:user_9" }, + }); + // the guest's log at the open: the sign-in, and a block from before that an owner took back + const log: unknown[] = [ + { type: "events.iterate.com/account/guest-signed-in", createdAt: "2026-11-20T10:00:00.000Z" }, + blockedAt("2026-11-21T10:00:00.000Z"), + ]; + let clock = opened; + const reads: number[] = []; + const { onFrame } = blockWatchOf({ + since: opened, + now: () => clock, + read: async (afterOffset) => { + reads.push(afterOffset); + return { events: log.slice(afterOffset), scannedThroughOffset: log.length, atHead: true }; + }, + }); + expect(await onFrame()).toBeUndefined(); + // frames within the minute read nothing + clock += BLOCK_EVERY_MS - 1; + expect(await onFrame()).toBeUndefined(); + expect(reads).toEqual([0]); + // an owner blocks the guest: the next read, from where the last one stopped, ends the session + log.push(blockedAt("2026-11-26T15:00:30.000Z")); + clock += 1; + expect(await onFrame()).toBe("your access to this context ended"); + expect(reads).toEqual([0, 2]); + // final, with no further read + expect(await onFrame()).toBe("your access to this context ended"); + expect(reads).toEqual([0, 2]); +}); + +test("a read of the guest's log that fails, or answers something else, ends nothing", async () => { + let clock = 1; + let answer: () => Promise<unknown> = async () => Promise.reject(new Error("no answer")); + const { onFrame } = blockWatchOf({ since: 0, now: () => clock, read: () => answer() }); + expect(await onFrame()).toBeUndefined(); + answer = async () => ({ events: "none" }); + clock += BLOCK_EVERY_MS; + expect(await onFrame()).toBeUndefined(); + // the next read that answers finds the block + answer = async () => ({ + events: [{ type: "events.iterate.com/account/person-blocked", createdAt: "2026-11-26T15:00:00.000Z" }], + scannedThroughOffset: 1, + atHead: true, + }); + clock += BLOCK_EVERY_MS; + expect(await onFrame()).toBe("your access to this context ended"); }); // Who asked, as every event of theirs then names them (`source.onBehalfOf.principal`).
M core/itx/src/person-context.ts
@@ -75,6 +75,9 @@ /** How long a guest's socket lasts at most, when a facet is asked whether the guest still * reaches the context (`socketEndOf`). */ export const ADMITTED_GUEST_SOCKET_MS = 4 * 60 * 60_000; +/** How often such a socket reads whether an owner blocked its guest, at most (`blockWatchOf`). */ +export const BLOCK_EVERY_MS = 60_000; +const PERSON_BLOCKED = "events.iterate.com/account/person-blocked"; const ACCESS_ENDED = "your access to this context ended"; const FACET_NAME = /^[A-Za-z0-9_.-]+$/; // The only `invoke` texts: the seed of a live state, and a facet's state, for the chat's views. @@ -482,13 +485,29 @@ ), }); const admittedGuest = person.method === "guest" && !!admission.onFrame; + // the guest's own context, where the auth Worker writes a block of them + const ofThePerson = new IterateContextRpcTarget( + contexts(), + DurableObjectNameCodec.address(`/users/${person.userId}`), + new SessionTeardown(), + (promise) => ctx.waitUntil(promise), + { principal: null }, + ); + const block = blockWatchOf({ + since: Date.now(), + read: (afterOffset) => + ofThePerson.invoke(["itx", "builtins", ["readEvents", afterOffset, 200]]), + }); + const onFrame = admittedGuest + ? async () => (await admission.onFrame!()) || (await block.onFrame()) + : admission.onFrame; return serveSession(ctx, { caller: { principal: null }, endsAt: socketEndOf({ signInEndsAt: endsAt, admittedGuest, now: Date.now() }), endReason: admittedGuest ? "this socket was open for 4 hours: open the page again" : "the sign-in for this session ended: sign in again", - onFrame: admission.onFrame, + onFrame, root: (context, teardown) => { const reads = context.cd(path); // The kernel's own code names the person here, so no signed token is needed, and a redial @@ -521,10 +540,9 @@ * an hour, while the call's own check said that the guest was still in. For such a socket the * facet's answer is what ends it (asked on the socket's traffic at most every `ADMITS_EVERY_MS`, * and a session with no traffic is probed, which is traffic): a link that ends, ends the socket. - * The sign-in's renewal is not asked while the socket is open. So a guest whom an owner blocks - * comes in nowhere again, as before, but keeps a socket that is open until the facet says no: - * what takes a guest out of an open socket at once is the app's own act (a link's end). The - * bound is 4 hours, as long as a member's relay credentials last in a call. + * The sign-in's renewal is not asked while the socket is open, so the socket itself reads + * whether an owner blocked the guest (`blockWatchOf`): a block ends it within a minute of + * traffic. The bound is 4 hours, as long as a member's relay credentials last in a call. * A member, a person who proved an address, and a guest's socket with no facet named end with * the sign-in, as before. */ export function socketEndOf(args: { @@ -533,6 +551,49 @@ now: number; }): number { return args.admittedGuest ? args.now + ADMITTED_GUEST_SOCKET_MS : args.signInEndsAt; +} + +/** A BLOCK ENDS A GUEST'S OPEN SOCKET. An owner's block ends the guest's tokens at once, and a + * socket that outlives the sign-in asks for no token again. The auth Worker writes the block on + * the guest's own context (`/users/<id>`, core/auth-worker/src/members.ts `block`), and `read` + * reads that log from an offset on. `onFrame` reads at most every `BLOCK_EVERY_MS` of the + * socket's traffic, and a block written since the socket opened (`since`) ends the session; it + * is final. An earlier block that an owner took back counts for nothing. A read that fails ends + * nothing: the next one asks again, and the socket's own bound holds. */ +export function blockWatchOf(args: { + since: number; + read: (afterOffset: number) => Promise<unknown>; + now?: () => number; +}) { + const now = args.now || Date.now; + const Page = z.object({ + events: z.array(z.object({ type: z.string(), createdAt: z.string().optional() }).loose()), + scannedThroughOffset: z.number(), + atHead: z.boolean(), + }); + let after = 0; + let askedAt = 0; + let blocked = false; + const look = async () => { + // at most 10 pages a look: a log longer than that is read on at the next one + for (let pages = 0; pages < 10 && !blocked; pages += 1) { + const page = Page.safeParse(await args.read(after).catch(() => null)); + if (!page.success) return; + blocked = page.data.events.some( + (event) => event.type === PERSON_BLOCKED && Date.parse(event.createdAt || "") >= args.since, + ); + after = Math.max(after, page.data.scannedThroughOffset); + if (page.data.atHead) return; + } + }; + return { + onFrame: async () => { + if (!blocked && askedAt && now() - askedAt < BLOCK_EVERY_MS) return; + askedAt = now(); + await look(); + if (blocked) return ACCESS_ENDED; + }, + }; } /** ADMITS: whether a person who is no member still reaches the context, by the facet the app's
M core/lib/src/sdk/auth.ts
@@ -217,8 +217,8 @@ * member is never asked. A GUEST'S socket with `admits` does not end with the guest's 15 * minutes on your host: the facet's answer ends it, and 4 hours at the latest. So a guest stays * in a call while your facet admits them, and your `admits()` is the whole check for an open - * socket: a guest whom an owner blocks at the sign-in comes in nowhere again, and is out of - * an open socket when your facet says no. */ + * socket, but for a block: a guest whom an owner blocks is out of an open socket within a + * minute, and comes in nowhere again. */ /** AN MCP SERVER OF THIS APP, SERVED BY THE PLATFORM (./mcp.ts). Call it in `fetch` for the * app's host, before the app's own paths: it answers for `/mcp` and for the document that * tells a client where to sign in, and null for every other path. `target` names the method