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

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13813-every-forge-one-admin-sends-shares-a-sin → main 2 commits fork

  • Merged. scout/13813-every-forge-one-admin-sends-shares-a-sin was merged into main as 8c00784.

Review

Sign in to review this change.
3 files changed +55 −10
src/spawn/host.ts +20 −2
@@ -44,6 +44,9 @@ import { during } from "../perf/jobs.ts";
4444 import { isSealed, SEALED_WORDS } from "./sealed.ts";
4545 import { readdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
4646 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";
4750
4851 interface ChildLedger {
4952 readonly name: string;
@@ -57,6 +60,17 @@ interface ChildLedger {
5760 * process meanwhile.
5861 */
5962 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);
6074 }
6175
6276 function readLedgerSync(app: App, name: string): ChildLedger | undefined {
@@ -70,6 +84,7 @@ function readLedgerSync(app: App, name: string): ChildLedger | undefined {
7084 placement?: unknown;
7185 size?: unknown;
7286 move?: unknown;
87 statusToken?: unknown;
7388 };
7489 if (typeof parsed.name !== "string" || typeof parsed.bodyId !== "string") return undefined;
7590 return {
@@ -80,6 +95,7 @@ function readLedgerSync(app: App, name: string): ChildLedger | undefined {
8095 placement: parsed.placement === "cell" ? "cell" : "hosted",
8196 size: isSize(parsed.size) ? parsed.size : DEFAULT_SIZE,
8297 moving: typeof parsed.move === "object" && parsed.move !== null,
98 statusToken: typeof parsed.statusToken === "string" ? parsed.statusToken : undefined,
8399 };
84100 } catch {
85101 return undefined;
@@ -578,7 +594,9 @@ export async function wakeGate(parent: App, request: Request): Promise<Response
578594 let people = personWakes.get(parent);
579595 if (!people) personWakes.set(parent, (people = new WakeRate()));
580596 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);
582600 if (wait > 0)
583601 return wakingResponse(parent, request, name, budgetRefusal({ name, budget, max, waitS: wait, limitsUrl: `${parentOrigin(parent.config)}/admin/limits` }), wait);
584602 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
587605 return wakingResponse(parent, request, name, stop === undefined ? why : stopSentence(stop));
588606 }
589607 if (!known) rates.note(ledger.bodyId, at);
590 people.note(budget.key, at);
608 if (!own) people.note(budget.key, at);
591609 clearStop(parent, ledger.bodyId); // the wake is granted: the stop no longer explains anything
592610 reserved.set(ledger.bodyId, Date.now());
593611 touch(parent, ledger);
src/spawn/reach.ts +13 −8
@@ -19,7 +19,8 @@
1919
2020 import { mkdirSync, readdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
2121 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";
2324 import { childPublicSlugs } from "./child_db.ts";
2425 import type { App } from "../app.ts";
2526 import { isSlug, slugFor } from "../core/apps.ts";
@@ -152,9 +153,9 @@ function withSeen(proof: Proof, headers: Headers): Proof {
152153 return { ...proof, seen };
153154 }
154155
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> {
156157 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 }) } });
158159 const body = res.headers.get("content-type")?.includes("json") ? await res.text() : (await res.body?.cancel(), "");
159160 return { status: res.status, headers: res.headers, body: body.slice(0, 4096) };
160161 } catch (error) {
@@ -163,9 +164,9 @@ async function ask(fetcher: FetchLike, url: string): Promise<{ status: number; h
163164 }
164165
165166 /** 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> {
167168 const url = homeUrlOf(child);
168 const got = await ask(fetcher, url);
169 const got = await ask(fetcher, url, token);
169170 if (typeof got === "string") return { label: "home", url, ok: false, status: null, says: `no answer (${got})` };
170171 let claimed = false;
171172 try {
@@ -207,8 +208,11 @@ export function proveBoots(app: App, child: Pick<Child, "url" | "bodyId">): Proo
207208 /** Ask every public address of a child, as a visitor does. Nothing is kept. */
208209 export async function proveChild(app: App, child: Child, home?: Proof): Promise<ChildReach> {
209210 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);
210214 const proofs = await Promise.all([
211 home ?? proveHome(fetcher, child),
215 home ?? proveHome(fetcher, child, token),
212216 ...(await slugsOf(app, child)).map((slug) => proveApp(fetcher, appUrlOf(app, child, slug), slug)),
213217 ]);
214218 const boots = proveBoots(app, child);
@@ -280,10 +284,11 @@ export async function firstProof(app: App, name: string, budgetMs?: number): Pro
280284 const o = optionsOf(app);
281285 const until = Date.now() + Math.max(0, budgetMs ?? o.homeWaitMs ?? HOME_WAIT_MS);
282286 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);
284289 while (!home.ok && Date.now() < until && !app.closing.signal.aborted) {
285290 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);
287292 }
288293 return record(app, await proveChild(app, child, home), false);
289294 }
test/wake-budget.test.ts +22
@@ -14,6 +14,7 @@ import { serve, type Servers } from "../src/http/server.ts";
1414 import { LIMITS, setLimit } from "../src/settings/limits.ts";
1515 import { childAwake, gateClock, resolveHostedChild, sleepIdleChildren, wakeGate } from "../src/spawn/host.ts";
1616 import { hostedPlacement } from "../src/spawn/placement.ts";
17 import { statusTokenOf } from "../src/actions/spawn.ts";
1718 import { budgetRefusal, waitWords, wakeBudget } from "../src/spawn/sleep.ts";
1819 import { quietLogs } from "./helpers/globals.ts";
1920
@@ -129,4 +130,25 @@ describe("whose budget a wake counts against", () => {
129130 setLimit(parent.db, "forges.wakes_per_person_hour", null, "seed");
130131 }
131132 });
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 });
132154 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs