The block check reads what the guest's log says last, at the open too
Jonas Templestein, 2026-11-26 15:29 UTC · 979160610d5fb385b6d405bece5c94e0d31a80c0 · parent bd2b13e
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
@@ -506,7 +506,13 @@ { path: `/users/${bob.userId}`, events: [{ payload: { reason: "blocked" } }] }, ]); - expect(await (await members("unblock", bob.userId)).text()).toContain("is no longer blocked"); + // the block's end is told on the guest's own context too, where the block is + const unblocked = await members("unblock", bob.userId); + expect(await unblocked.text()).toContain("is no longer blocked"); + expect(factsOf(unblocked).at(-1)).toMatchObject({ + path: `/users/${bob.userId}`, + events: [{ type: "events.iterate.com/account/person-unblocked" }], + }); expect(originAndPath(await meet.ask(bob.cookie))).toBe(`${MEET}/.iterate/callback`); });
M core/auth-worker/src/members.ts
@@ -556,6 +556,16 @@ type: "events.iterate.com/account/person-unblocked", payload: { email }, }); + // on the person's own context too, where their block is: what that log says last is whether + // an open socket of theirs ends (core/itx/src/person-context.ts `blockWatchOf`) + const person = await env.DB.prepare("select id from users where email = ?1") + .bind(email) + .first<{ id: string }>(); + if (person) + facts.add(person.id, { + type: "events.iterate.com/account/person-unblocked", + payload: { email }, + }); return { facts, notice: `${email} is no longer blocked.` }; }
M core/itx/src/person-context.test.ts
@@ -9,6 +9,7 @@ ADMITTED_GUEST_SOCKET_MS, admissionOf, BLOCK_EVERY_MS, + BLOCK_READ_FAILURES, blockWatchOf, asAFacetIsTold, asAPersonReads, @@ -902,60 +903,73 @@ 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, +// The block, and its end, are on the guest's own context, written by the auth Worker. +const BLOCKED = { type: "events.iterate.com/account/person-blocked" }; +const UNBLOCKED = { type: "events.iterate.com/account/person-unblocked" }; +/** A watch over a log that the test writes, with its clock and the offsets it read from. */ +function blockWatch(log: unknown[]) { + const world = { clock: 1_795_700_000_000, reads: [] as number[], fails: false }; + const watch = blockWatchOf({ + now: () => world.clock, read: async (afterOffset) => { - reads.push(afterOffset); + world.reads.push(afterOffset); + if (world.fails) throw new Error("no answer"); return { events: log.slice(afterOffset), scannedThroughOffset: log.length, atHead: true }; }, }); - expect(await onFrame()).toBeUndefined(); + return { world, watch }; +} + +test("an owner's block ends a guest's open socket at the next read, and it is final", async () => { + // 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" }, + BLOCKED, + UNBLOCKED, + ]; + const { world, watch } = blockWatch(log); + expect(await watch.blockedNow()).toBe(false); // frames within the minute read nothing - clock += BLOCK_EVERY_MS - 1; - expect(await onFrame()).toBeUndefined(); - expect(reads).toEqual([0]); + world.clock += BLOCK_EVERY_MS - 1; + expect(await watch.onFrame()).toBeUndefined(); + expect(world.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]); + log.push(BLOCKED); + world.clock += 1; + expect(await watch.onFrame()).toBe("your access to this context ended"); + expect(world.reads).toEqual([0, 3]); + // final: an owner who takes the block back does not bring the socket back, and nothing is read + log.push(UNBLOCKED); + world.clock += BLOCK_EVERY_MS; + expect(await watch.onFrame()).toBe("your access to this context ended"); + expect(world.reads).toEqual([0, 3]); }); -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"); +test("a guest who is blocked at the open gets no socket, whatever the time on the block", async () => { + expect(await blockWatch([BLOCKED]).watch.blockedNow()).toBe(true); + expect(await blockWatch([BLOCKED, UNBLOCKED, BLOCKED]).watch.blockedNow()).toBe(true); + expect(await blockWatch([]).watch.blockedNow()).toBe(false); +}); + +test("reads of the guest's log that fail: one says nothing, three in a row end the session", async () => { + const log: unknown[] = []; + const { world, watch } = blockWatch(log); + world.fails = true; + // the open is not refused for a read that failed (the first of the three) + expect(await watch.blockedNow()).toBe(false); + world.clock += BLOCK_EVERY_MS; + expect(await watch.onFrame()).toBeUndefined(); + // a read that answers starts the count again + world.fails = false; + world.clock += BLOCK_EVERY_MS; + expect(await watch.onFrame()).toBeUndefined(); + world.fails = true; + for (let n = 1; n < BLOCK_READ_FAILURES; n += 1) { + world.clock += BLOCK_EVERY_MS; + expect(await watch.onFrame()).toBeUndefined(); + } + world.clock += BLOCK_EVERY_MS; + expect(await watch.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
@@ -77,7 +77,10 @@ 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; +/** How many reads of the guest's log in a row may fail before the socket ends. */ +export const BLOCK_READ_FAILURES = 3; const PERSON_BLOCKED = "events.iterate.com/account/person-blocked"; +const PERSON_UNBLOCKED = "events.iterate.com/account/person-unblocked"; 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. @@ -494,10 +497,12 @@ { principal: null }, ); const block = blockWatchOf({ - since: Date.now(), read: (afterOffset) => ofThePerson.invoke(["itx", "builtins", ["readEvents", afterOffset, 200]]), }); + // a guest who is blocked gets no such socket, whatever the upgrade's stamp says + if (admittedGuest && (await block.blockedNow())) + return new Response("Your access ended\n", { status: 403 }); const onFrame = admittedGuest ? async () => (await admission.onFrame!()) || (await block.onFrame()) : admission.onFrame; @@ -541,8 +546,9 @@ * 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 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. + * whether an owner blocked the guest (`blockWatchOf`): a block ends it about a minute and a + * half later, at the latest. 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: { @@ -554,44 +560,61 @@ } /** 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. */ + * socket that outlives the sign-in asks for no token again. The auth Worker writes a block, and + * its end, on the guest's own context (`/users/<id>`, core/auth-worker/src/members.ts), and + * `read` reads that log from an offset on. Whether the guest is blocked is what the log says + * last: no clock is compared. + * - `blockedNow` reads the log at the open: a guest who is blocked gets no socket, also when the + * upgrade was stamped a moment before the block. + * - `onFrame` reads on the socket's traffic, at most every `BLOCK_EVERY_MS`. A socket has a frame + * every 30 to 40 s or is ended (./end-when-silent.ts), so a block ends the session about a + * minute and a half after it is in the log, at the latest. It is final. + * - A read that fails says nothing; `BLOCK_READ_FAILURES` of them in a row end the session, so + * a guest whose block cannot be read is not kept. The guest's page opens a socket again, and + * the sign-in refuses a blocked guest there. + * What this cannot see: a block whose fact never reached the mind (the auth Worker's facts are + * delivered best effort, iterate/facts). Such a guest's socket ends by the facet's no or the + * socket's own bound (`socketEndOf`), and they come in nowhere again. */ 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()), + events: z.array(z.object({ type: z.string() }).loose()), scannedThroughOffset: z.number(), atHead: z.boolean(), }); let after = 0; let askedAt = 0; let blocked = false; + let failures = 0; + let ended = false; const look = async () => { + askedAt = now(); // 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) { + for (let pages = 0; pages < 10; 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, - ); + if (!page.success) return void (failures += 1); + for (const event of page.data.events) { + if (event.type === PERSON_BLOCKED) blocked = true; + else if (event.type === PERSON_UNBLOCKED) blocked = false; + } after = Math.max(after, page.data.scannedThroughOffset); - if (page.data.atHead) return; + if (page.data.atHead) return void (failures = 0); } }; return { - onFrame: async () => { - if (!blocked && askedAt && now() - askedAt < BLOCK_EVERY_MS) return; - askedAt = now(); + blockedNow: async () => { await look(); - if (blocked) return ACCESS_ENDED; + return blocked; + }, + onFrame: async () => { + if (!ended && now() - askedAt >= BLOCK_EVERY_MS) { + await look(); + ended = blocked || failures >= BLOCK_READ_FAILURES; + } + if (ended) return ACCESS_ENDED; }, }; }
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, but for a block: a guest whom an owner blocks is out of an open socket within a - * minute, and comes in nowhere again. */ + * socket, but for a block: a guest whom an owner blocks is out of an open socket about a + * minute and a half later, 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