Public: anyone can read this; the crew can change it.
One signed-in account cannot hold more than its share of event streams #14982
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/14961-one-signed-in-account-cannot-hold-more-t → main 3 commits fork
6 files
changed +98
−15
src/http/caller.ts +10
| @@ -50,6 +50,16 @@ export const noteCaller = ( | ||
| 50 | 50 | callers.set(request, { db, userId, sessionId }); |
| 51 | 51 | }; |
| 52 | 52 | |
| 53 | /** The signed-in account behind a request, when auth found one. */ | |
| 54 | export const callerAccount = (request: Request): string | undefined => | |
| 55 | callers.get(request)?.userId; | |
| 56 | ||
| 57 | /** A request rebuilt from another (a replay) is the same caller. */ | |
| 58 | export const carryCaller = (from: Request, to: Request): void => { | |
| 59 | const caller = callers.get(from); | |
| 60 | if (caller !== undefined) callers.set(to, caller); | |
| 61 | }; | |
| 62 | ||
| 53 | 63 | function hold(held: Held, key: string, close: () => void): () => void { |
| 54 | 64 | let set = held.get(key); |
| 55 | 65 | if (set === undefined) held.set(key, (set = new Set())); |
src/http/limits.ts −3
| @@ -49,9 +49,6 @@ export const noteSignedIn = (request: Request): void => { | ||
| 49 | 49 | signedIn.add(request); |
| 50 | 50 | }; |
| 51 | 51 | |
| 52 | /** Whether the request carries a signed-in person or a token. */ | |
| 53 | export const isSignedIn = (request: Request): boolean => signedIn.has(request); | |
| 54 | ||
| 55 | 52 | /** The most a shared reader keeps of this request's body. */ |
| 56 | 53 | export const bodyCapFor = (request: Request): number => |
| 57 | 54 | signedIn.has(request) ? BODY_MAX : STRANGER_MAX; |
src/http/server.ts +2
| @@ -71,6 +71,7 @@ import { actingVia } from "../core/acting_via.ts"; | ||
| 71 | 71 | import { |
| 72 | 72 | closeStreamsOf, |
| 73 | 73 | closeStreamsOfSession, |
| 74 | carryCaller, | |
| 74 | 75 | noteCaller, |
| 75 | 76 | sweepStanding, |
| 76 | 77 | } from "./caller.ts"; |
| @@ -2255,6 +2256,7 @@ async function withIdempotency( | ||
| 2255 | 2256 | // still see what was noted of the original. |
| 2256 | 2257 | carryPeer(request, again); |
| 2257 | 2258 | carryNotes(request, again); |
| 2259 | carryCaller(request, again); | |
| 2258 | 2260 | return again; |
| 2259 | 2261 | }; |
| 2260 | 2262 |
src/http/sse.ts +4 −4
| @@ -14,8 +14,7 @@ | ||
| 14 | 14 | * timer. |
| 15 | 15 | */ |
| 16 | 16 | |
| 17 | import { holdForCaller } from "./caller.ts"; | |
| 18 | import { isSignedIn } from "./limits.ts"; | |
| 17 | import { callerAccount, holdForCaller } from "./caller.ts"; | |
| 19 | 18 | import { holdStream, tooManyStrangers } from "./strangers.ts"; |
| 20 | 19 | import type { App } from "../app.ts"; |
| 21 | 20 | import type { User } from "../core/users.ts"; |
| @@ -88,7 +87,8 @@ export type Emit = (event: string, data: unknown, id?: string) => void; | ||
| 88 | 87 | * `close` is called, or when a write fails — whichever happens first. |
| 89 | 88 | * |
| 90 | 89 | * Every stream is counted on `app` while it is open (`strangers.ts`): a |
| 91 | * stranger's against its address and every stranger's together, anyone's | |
| 90 | * stranger's against its address and every stranger's together, a | |
| 91 | * signed-in account's against the account and its address, anyone's | |
| 92 | 92 | * against the forge's whole budget. Past either, the answer is a 429 and |
| 93 | 93 | * nothing is registered. |
| 94 | 94 | */ |
| @@ -97,7 +97,7 @@ export function eventStream( | ||
| 97 | 97 | request: Request, |
| 98 | 98 | register: (emit: Emit, close: () => void, ready: () => boolean) => () => void, |
| 99 | 99 | ): Response { |
| 100 | const held = holdStream(app, request, !isSignedIn(request)); | |
| 100 | const held = holdStream(app, request, callerAccount(request)); | |
| 101 | 101 | if (!held.allowed) return tooManyStrangers(held); |
| 102 | 102 | const encoder = new TextEncoder(); |
| 103 | 103 | let cleanup = () => {}; |
src/http/strangers.ts +30 −7
| @@ -54,6 +54,19 @@ export interface StrangerLimits { | ||
| 54 | 54 | readonly streamsPerAddress: number; |
| 55 | 55 | /** Event streams strangers may hold open, every one together. */ |
| 56 | 56 | readonly strangerStreams: number; |
| 57 | /** | |
| 58 | * Event streams one signed-in account may hold open, whatever its tokens | |
| 59 | * and browsers: dozens of tabs, each with its page's streams, and an agent | |
| 60 | * or two watching runs, with room to spare — not a share of `allStreams`. | |
| 61 | */ | |
| 62 | readonly streamsPerAccount: number; | |
| 63 | /** | |
| 64 | * Event streams signed-in callers from one address may hold open together, | |
| 65 | * so one address cannot fill the pool by minting accounts. Counted apart | |
| 66 | * from strangers' `streamsPerAddress`: an office behind one address holds | |
| 67 | * several people's tabs. | |
| 68 | */ | |
| 69 | readonly signedStreamsPerAddress: number; | |
| 57 | 70 | /** Event streams anyone may hold open on this forge. */ |
| 58 | 71 | readonly allStreams: number; |
| 59 | 72 | } |
| @@ -68,6 +81,8 @@ export const STRANGER_LIMITS: StrangerLimits = { | ||
| 68 | 81 | codeAtOnce: 1, |
| 69 | 82 | streamsPerAddress: 8, |
| 70 | 83 | strangerStreams: 256, |
| 84 | streamsPerAccount: 64, | |
| 85 | signedStreamsPerAddress: 256, | |
| 71 | 86 | allStreams: 4096, |
| 72 | 87 | }; |
| 73 | 88 | |
| @@ -298,29 +313,37 @@ function codeSearchAt(app: App, bucket: string): Held | Refused { | ||
| 298 | 313 | } |
| 299 | 314 | |
| 300 | 315 | /** |
| 301 | * An event stream: every one counts toward `allStreams`; a stranger's also | |
| 302 | * toward its address and toward every stranger's together. | |
| 316 | * An event stream: every one counts toward `allStreams`. A stranger's | |
| 317 | * (`account` undefined) also toward its address and toward every stranger's | |
| 318 | * together; a signed-in caller's toward its account and toward what signed-in | |
| 319 | * callers from its address hold, so no one account — nor one address with | |
| 320 | * many — holds the pool every other account streams from. | |
| 303 | 321 | */ |
| 304 | 322 | export function holdStream( |
| 305 | 323 | app: App, |
| 306 | 324 | request: Request, |
| 307 | stranger: boolean, | |
| 325 | account: string | undefined, | |
| 308 | 326 | ): Held | Refused { |
| 309 | 327 | const state = stateOf(app); |
| 310 | 328 | const { limits } = state; |
| 329 | const bucket = clientBucket(request); | |
| 311 | 330 | const release = holdAll( |
| 312 | 331 | state, |
| 313 | stranger | |
| 332 | account === undefined | |
| 314 | 333 | ? [ |
| 315 | 334 | ["stream:*", limits.allStreams], |
| 316 | 335 | ["stream:strangers", limits.strangerStreams], |
| 317 | [`stream:${clientBucket(request)}`, limits.streamsPerAddress], | |
| 336 | [`stream:${bucket}`, limits.streamsPerAddress], | |
| 318 | 337 | ] |
| 319 | : [["stream:*", limits.allStreams]], | |
| 338 | : [ | |
| 339 | ["stream:*", limits.allStreams], | |
| 340 | [`stream:account:${account}`, limits.streamsPerAccount], | |
| 341 | [`stream:signed:${bucket}`, limits.signedStreamsPerAddress], | |
| 342 | ], | |
| 320 | 343 | ); |
| 321 | 344 | if (release === undefined) |
| 322 | 345 | return refused( |
| 323 | stranger | |
| 346 | account === undefined | |
| 324 | 347 | ? "too many open streams for visitors who are not signed in" |
| 325 | 348 | : "too many open streams", |
| 326 | 349 | 30, |
test/strangers.test.ts +52 −1
| @@ -16,7 +16,7 @@ import { createApp, type App } from "../src/app.ts"; | ||
| 16 | 16 | import { createRepo } from "../src/actions/repos.ts"; |
| 17 | 17 | import { createApp as createAppRecord } from "../src/core/apps.ts"; |
| 18 | 18 | import { createToken } from "../src/core/tokens.ts"; |
| 19 | import type { User } from "../src/core/users.ts"; | |
| 19 | import { createUser, type User } from "../src/core/users.ts"; | |
| 20 | 20 | import { transferGate } from "../src/git/env.ts"; |
| 21 | 21 | import { serve, type Servers } from "../src/http/server.ts"; |
| 22 | 22 | import { |
| @@ -441,6 +441,57 @@ describe("strangers at every door", () => { | ||
| 441 | 441 | expect(await until(() => heldNow(app, "stream:*") === 0)).toBe(true); |
| 442 | 442 | }); |
| 443 | 443 | |
| 444 | test("event streams: per signed-in account and per signed-in address, beside strangers'", async () => { | |
| 445 | setStrangerLimits(app, { streamsPerAccount: 2, signedStreamsPerAddress: 3 }); | |
| 446 | const bearer = async (username: string): Promise<string> => { | |
| 447 | const made = await createUser(app.db, { | |
| 448 | origin: "seed", | |
| 449 | trusted: true, | |
| 450 | agent: true, | |
| 451 | username, | |
| 452 | email: `${username}@f.dev`, | |
| 453 | }); | |
| 454 | if (!made.ok) throw new Error(made.error); | |
| 455 | return createToken(app.db, made.value.id, "t", "ro").token; | |
| 456 | }; | |
| 457 | const [bob, carol, dave] = [await bearer("bob"), await bearer("carol"), await bearer("dave")]; | |
| 458 | // A second token is the same account: the cap is the account's, not the token's. | |
| 459 | const ada2 = createToken(app.db, ada.id, "t2", "ro").token; | |
| 460 | const open: AbortController[] = []; | |
| 461 | const stream = async (ip: string, bearerToken: string): Promise<number> => { | |
| 462 | const abort = new AbortController(); | |
| 463 | open.push(abort); | |
| 464 | const res = await fetch(`${base}/events`, { | |
| 465 | headers: from(ip, bearerToken === "" ? {} : { authorization: `Bearer ${bearerToken}` }), | |
| 466 | signal: abort.signal, | |
| 467 | }); | |
| 468 | if (res.status !== 200) await res.arrayBuffer(); | |
| 469 | return res.status; | |
| 470 | }; | |
| 471 | try { | |
| 472 | const [a, b, c, d, e] = [address(), address(), address(), address(), address()]; | |
| 473 | expect(await stream(a, token)).toBe(200); | |
| 474 | expect(await stream(b, ada2)).toBe(200); | |
| 475 | // Ada holds her two from wherever she asks, with whichever token. | |
| 476 | expect(await stream(c, token)).toBe(429); | |
| 477 | expect(heldNow(app, `stream:account:${ada.id}`)).toBe(2); | |
| 478 | // Another account still connects. | |
| 479 | expect(await stream(c, bob)).toBe(200); | |
| 480 | // One address holds three signed-in streams, however many accounts. | |
| 481 | expect(await stream(d, bob)).toBe(200); | |
| 482 | expect(await stream(d, carol)).toBe(200); | |
| 483 | expect(await stream(d, carol)).toBe(200); | |
| 484 | expect(await stream(d, dave)).toBe(429); | |
| 485 | expect(await stream(e, dave)).toBe(200); | |
| 486 | // A stranger at that address is counted apart from who signed in there. | |
| 487 | expect(await stream(d, "")).not.toBe(429); | |
| 488 | } finally { | |
| 489 | for (const abort of open) abort.abort(); | |
| 490 | setStrangerLimits(app, STRANGER_LIMITS); | |
| 491 | } | |
| 492 | expect(await until(() => heldNow(app, "stream:*") === 0)).toBe(true); | |
| 493 | }); | |
| 494 | ||
| 444 | 495 | test("a stranger's spawn plans are counted per address", async () => { |
| 445 | 496 | setStrangerLimits(app, { perAddress: { ...STRANGER_LIMITS.perAddress, plan: 2 } }); |
| 446 | 497 | try { |
new above
offline