Public: anyone can read this; the crew can change it.
Every forge one admin sends shares a single budget of 30 wakes an hour #13920
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13813-every-forge-one-admin-sends-shares-a-sin → main 2 commits fork
3 files
changed +55
−10
src/spawn/host.ts +20 −2
| @@ -44,6 +44,9 @@ import { during } from "../perf/jobs.ts"; | ||
| 44 | 44 | import { isSealed, SEALED_WORDS } from "./sealed.ts"; |
| 45 | 45 | import { readdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; |
| 46 | 46 | import { join } from "node:path"; |
| 47 | import { timingSafeEqual } from "node:crypto"; | |
| 48 | import { sha256 } from "../kernel/hash.ts"; | |
| 49 | import { PARENT_HEADER } from "./clients.ts"; | |
| 47 | 50 | |
| 48 | 51 | interface ChildLedger { |
| 49 | 52 | readonly name: string; |
| @@ -57,6 +60,17 @@ interface ChildLedger { | ||
| 57 | 60 | * process meanwhile. |
| 58 | 61 | */ |
| 59 | 62 | readonly moving: boolean; |
| 63 | /** What the parent shows the child (`x-forge-parent`); its own questions carry it. */ | |
| 64 | readonly statusToken: string | undefined; | |
| 65 | } | |
| 66 | ||
| 67 | /** Whether the request is this forge's own question to the child: it shows the child's secret. */ | |
| 68 | function isParentsOwn(ledger: ChildLedger, request: Request): boolean { | |
| 69 | const shown = request.headers.get(PARENT_HEADER); | |
| 70 | if (shown === null || ledger.statusToken === undefined) return false; | |
| 71 | const a = Buffer.from(sha256(shown)); | |
| 72 | const b = Buffer.from(sha256(ledger.statusToken)); | |
| 73 | return timingSafeEqual(a, b); | |
| 60 | 74 | } |
| 61 | 75 | |
| 62 | 76 | function readLedgerSync(app: App, name: string): ChildLedger | undefined { |
| @@ -70,6 +84,7 @@ function readLedgerSync(app: App, name: string): ChildLedger | undefined { | ||
| 70 | 84 | placement?: unknown; |
| 71 | 85 | size?: unknown; |
| 72 | 86 | move?: unknown; |
| 87 | statusToken?: unknown; | |
| 73 | 88 | }; |
| 74 | 89 | if (typeof parsed.name !== "string" || typeof parsed.bodyId !== "string") return undefined; |
| 75 | 90 | return { |
| @@ -80,6 +95,7 @@ function readLedgerSync(app: App, name: string): ChildLedger | undefined { | ||
| 80 | 95 | placement: parsed.placement === "cell" ? "cell" : "hosted", |
| 81 | 96 | size: isSize(parsed.size) ? parsed.size : DEFAULT_SIZE, |
| 82 | 97 | moving: typeof parsed.move === "object" && parsed.move !== null, |
| 98 | statusToken: typeof parsed.statusToken === "string" ? parsed.statusToken : undefined, | |
| 83 | 99 | }; |
| 84 | 100 | } catch { |
| 85 | 101 | return undefined; |
| @@ -578,7 +594,9 @@ export async function wakeGate(parent: App, request: Request): Promise<Response | ||
| 578 | 594 | let people = personWakes.get(parent); |
| 579 | 595 | if (!people) personWakes.set(parent, (people = new WakeRate())); |
| 580 | 596 | const max = limitValue(parent.db, "forges.wakes_per_person_hour"); |
| 581 | const wait = people.waitS(budget.key, max, at); | |
| 597 | // The parent's own questions (its reach proofs) spend no one's wakes (#13813). | |
| 598 | const own = isParentsOwn(ledger, request); | |
| 599 | const wait = own ? 0 : people.waitS(budget.key, max, at); | |
| 582 | 600 | if (wait > 0) |
| 583 | 601 | return wakingResponse(parent, request, name, budgetRefusal({ name, budget, max, waitS: wait, limitsUrl: `${parentOrigin(parent.config)}/admin/limits` }), wait); |
| 584 | 602 | const why = awakeRefusal(awakeCount(parent)) ?? (known ? undefined : rates.refusal(ledger.bodyId, at)); |
| @@ -587,7 +605,7 @@ export async function wakeGate(parent: App, request: Request): Promise<Response | ||
| 587 | 605 | return wakingResponse(parent, request, name, stop === undefined ? why : stopSentence(stop)); |
| 588 | 606 | } |
| 589 | 607 | if (!known) rates.note(ledger.bodyId, at); |
| 590 | people.note(budget.key, at); | |
| 608 | if (!own) people.note(budget.key, at); | |
| 591 | 609 | clearStop(parent, ledger.bodyId); // the wake is granted: the stop no longer explains anything |
| 592 | 610 | reserved.set(ledger.bodyId, Date.now()); |
| 593 | 611 | touch(parent, ledger); |
src/spawn/reach.ts +13 −8
| @@ -19,7 +19,8 @@ | ||
| 19 | 19 | |
| 20 | 20 | import { mkdirSync, readdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; |
| 21 | 21 | import { join } from "node:path"; |
| 22 | import { childDirOf, listChildren, type Child } from "../actions/spawn.ts"; | |
| 22 | import { childDirOf, listChildren, statusTokenOf, type Child } from "../actions/spawn.ts"; | |
| 23 | import { PARENT_HEADER } from "./clients.ts"; | |
| 23 | 24 | import { childPublicSlugs } from "./child_db.ts"; |
| 24 | 25 | import type { App } from "../app.ts"; |
| 25 | 26 | import { isSlug, slugFor } from "../core/apps.ts"; |
| @@ -152,9 +153,9 @@ function withSeen(proof: Proof, headers: Headers): Proof { | ||
| 152 | 153 | return { ...proof, seen }; |
| 153 | 154 | } |
| 154 | 155 | |
| 155 | async function ask(fetcher: FetchLike, url: string): Promise<{ status: number; headers: Headers; body: string } | string> { | |
| 156 | async function ask(fetcher: FetchLike, url: string, token?: string): Promise<{ status: number; headers: Headers; body: string } | string> { | |
| 156 | 157 | try { |
| 157 | const res = await fetcher(url, { redirect: "manual", signal: AbortSignal.timeout(PROBE_MS), headers: { accept: "*/*" } }); | |
| 158 | const res = await fetcher(url, { redirect: "manual", signal: AbortSignal.timeout(PROBE_MS), headers: { accept: "*/*", ...(token === undefined ? {} : { [PARENT_HEADER]: token }) } }); | |
| 158 | 159 | const body = res.headers.get("content-type")?.includes("json") ? await res.text() : (await res.body?.cancel(), ""); |
| 159 | 160 | return { status: res.status, headers: res.headers, body: body.slice(0, 4096) }; |
| 160 | 161 | } catch (error) { |
| @@ -163,9 +164,9 @@ async function ask(fetcher: FetchLike, url: string): Promise<{ status: number; h | ||
| 163 | 164 | } |
| 164 | 165 | |
| 165 | 166 | /** The child's home answers as the forge it is. */ |
| 166 | export async function proveHome(fetcher: FetchLike, child: Pick<Child, "url">): Promise<Proof> { | |
| 167 | export async function proveHome(fetcher: FetchLike, child: Pick<Child, "url">, token?: string): Promise<Proof> { | |
| 167 | 168 | const url = homeUrlOf(child); |
| 168 | const got = await ask(fetcher, url); | |
| 169 | const got = await ask(fetcher, url, token); | |
| 169 | 170 | if (typeof got === "string") return { label: "home", url, ok: false, status: null, says: `no answer (${got})` }; |
| 170 | 171 | let claimed = false; |
| 171 | 172 | try { |
| @@ -207,8 +208,11 @@ export function proveBoots(app: App, child: Pick<Child, "url" | "bodyId">): Proo | ||
| 207 | 208 | /** Ask every public address of a child, as a visitor does. Nothing is kept. */ |
| 208 | 209 | export async function proveChild(app: App, child: Child, home?: Proof): Promise<ChildReach> { |
| 209 | 210 | const fetcher = optionsOf(app).fetch ?? fetch; |
| 211 | // Shown to the home only (never to an app's code) so the wake gate knows these are the parent's | |
| 212 | // own questions and counts none (#13813). | |
| 213 | const token = await statusTokenOf(app, child.name); | |
| 210 | 214 | const proofs = await Promise.all([ |
| 211 | home ?? proveHome(fetcher, child), | |
| 215 | home ?? proveHome(fetcher, child, token), | |
| 212 | 216 | ...(await slugsOf(app, child)).map((slug) => proveApp(fetcher, appUrlOf(app, child, slug), slug)), |
| 213 | 217 | ]); |
| 214 | 218 | const boots = proveBoots(app, child); |
| @@ -280,10 +284,11 @@ export async function firstProof(app: App, name: string, budgetMs?: number): Pro | ||
| 280 | 284 | const o = optionsOf(app); |
| 281 | 285 | const until = Date.now() + Math.max(0, budgetMs ?? o.homeWaitMs ?? HOME_WAIT_MS); |
| 282 | 286 | const fetcher = o.fetch ?? fetch; |
| 283 | let home = await proveHome(fetcher, child); | |
| 287 | const token = await statusTokenOf(app, child.name); | |
| 288 | let home = await proveHome(fetcher, child, token); | |
| 284 | 289 | while (!home.ok && Date.now() < until && !app.closing.signal.aborted) { |
| 285 | 290 | await Bun.sleep(Math.min(o.pollMs ?? SETTLE_POLL_MS, 2000, Math.max(0, until - Date.now()))); |
| 286 | home = await proveHome(fetcher, child); | |
| 291 | home = await proveHome(fetcher, child, token); | |
| 287 | 292 | } |
| 288 | 293 | return record(app, await proveChild(app, child, home), false); |
| 289 | 294 | } |
test/wake-budget.test.ts +22
| @@ -14,6 +14,7 @@ import { serve, type Servers } from "../src/http/server.ts"; | ||
| 14 | 14 | import { LIMITS, setLimit } from "../src/settings/limits.ts"; |
| 15 | 15 | import { childAwake, gateClock, resolveHostedChild, sleepIdleChildren, wakeGate } from "../src/spawn/host.ts"; |
| 16 | 16 | import { hostedPlacement } from "../src/spawn/placement.ts"; |
| 17 | import { statusTokenOf } from "../src/actions/spawn.ts"; | |
| 17 | 18 | import { budgetRefusal, waitWords, wakeBudget } from "../src/spawn/sleep.ts"; |
| 18 | 19 | import { quietLogs } from "./helpers/globals.ts"; |
| 19 | 20 | |
| @@ -129,4 +130,25 @@ describe("whose budget a wake counts against", () => { | ||
| 129 | 130 | setLimit(parent.db, "forges.wakes_per_person_hour", null, "seed"); |
| 130 | 131 | } |
| 131 | 132 | }); |
| 133 | test("the parent's own questions (its status token) spend no wake", async () => { | |
| 134 | const t0 = Date.now() + 20_000_000; | |
| 135 | const token = await statusTokenOf(parent, "unclaimed"); | |
| 136 | expect(token).toBeDefined(); | |
| 137 | expect(setLimit(parent.db, "forges.wakes_per_person_hour", 1, "seed").ok).toBe(true); | |
| 138 | const ask = (headers: Record<string, string>) => fetch(`${servers.url}/healthz`, { headers: { host: hostOf("unclaimed"), ...headers } }); | |
| 139 | try { | |
| 140 | gateClock.now = () => t0; | |
| 141 | for (let i = 0; i < 3; i++) { | |
| 142 | expect((await ask({ "x-forge-parent": token! })).status).toBe(200); | |
| 143 | await allAsleep(); | |
| 144 | } | |
| 145 | // A wrong secret is a stranger's request: it spends the one wake, and the next is refused. | |
| 146 | expect((await ask({ "x-forge-parent": "nope" })).status).toBe(200); | |
| 147 | await allAsleep(); | |
| 148 | expect((await ask({})).status).toBe(429); | |
| 149 | } finally { | |
| 150 | gateClock.now = () => Date.now(); | |
| 151 | setLimit(parent.db, "forges.wakes_per_person_hour", null, "seed"); | |
| 152 | } | |
| 153 | }); | |
| 132 | 154 | }); |
new above
offline