Public: anyone can read this; the crew can change it.

The four src/spawn deepsec fixes land from a branch the reviewers can read #14946

Merged opened this

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

Merged

scout/forge:scout/14931-the-four-src-spawn-deepsec-fixes-land-fr → main 1 commit fork

  • Merged. scout/14931-the-four-src-spawn-deepsec-fixes-land-fr was merged into main as d64d452.

Review

Sign in to review this change.
15 files changed +281 −27
AGENTS.md +1
@@ -48,6 +48,7 @@ The check queue hands work to a runner through `src/checks/runner.ts` (#13652, R
4848 `src/actions/signup-window.ts`: `pendings`, per forge, the sign-up slots handed out and not yet spent, counted against the hourly ceilings so sign-ups in flight at once cannot all pass on one count (#14779); each slot leaves when spent or a minute after it was taken, so at most the forge-wide per-minute limit; gone on restart (the made accounts are rows, `signup_made`).
4949
5050 `src/spawn/funding.ts`: `askedAt`, when a run last asked the host for the lent credit (`refreshFundingOnce`, 15 s per data directory); gone on restart.
51 `src/spawn/held_closed.ts`: `held`, per forge, the body ids of the children an archive is copying (#14783); `startRefused` refuses every wake of one while it is held; at most one per archive in flight, released in its `finally`, gone on restart.
5152 `src/system/footing.ts`: `footing`, the last reading of what the forge stands on from the kube API (#11685), held 60 s so /system, its terminal screen (`src/tui/screens/footing.ts`, palette entry for admins) and the MCP `system` tool (`kind: footing`) share one read; a probe that throws is not retried for 60 s; one reading, gone on restart.
5253 `src/system/largest_dirs.ts`: `largestDirs`, the last measurement of the five largest directories under the forge's data (#11800), held 60 s so /system, its terminal screen (`src/tui/screens/system.ts`, `largestLines`) and the MCP `system` tool share one asynchronous walk; a walk that fails is not retried for 60 s; one reading, gone on restart.
5354 `src/system/db_size.ts`: `dbSize`, the last walk of the database file's pages (#12010: the ten largest tables and indexes), held an hour so /system and the MCP `system` tool (`kind: size`) share one asynchronous walk; a walk that fails is not retried for an hour; one reading, gone on restart. The file's bytes and its growth are read fresh each time; growth comes from `db_size_day`, one row a UTC day, 60 kept.
src/snapshot/hosted.ts +2 −1
@@ -77,7 +77,8 @@ export const PARENT_FILES: readonly string[] = [`${PARENT_SIDE}credit.json`, `${
7777 export const HISTORY_DIRS = ["runs", "drop", "ledger"] as const;
7878 export const HISTORY_FILES = ["parent.json", "plan.json"] as const;
7979
80 const BODY_ID = /^[A-Za-z0-9_-]{1,64}$/;
80 // A letter or digit first, as `spawn/archive.ts` holds: an id never reads as an option (#14783).
81 const BODY_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
8182
8283 /** The data directory name a forge's ledger text names, or undefined when it names none. */
8384 export function bodyIdOf(ledger: string): string | undefined {
src/spawn/archive.ts +34 −4
@@ -22,7 +22,8 @@ import { childPerson } from "./child_db.ts";
2222 import { dropServed, type ClientRow } from "./clients.ts";
2323 import { needsNotice } from "./archive_proposal.ts";
2424 import { removeForge, type Managed } from "./manage.ts";
25 import { sleepChild } from "./host.ts";
25 import { holdClosed } from "./held_closed.ts";
26 import { isAwakeBody, openHere, sleepChild } from "./host.ts";
2627 import { CHILD_NAME } from "./placement.ts";
2728
2829 export interface ArchiveRecord {
@@ -55,7 +56,9 @@ const busy = new Set<string>();
5556
5657 const root = (app: App): string => join(app.config.dataDir, "archive");
5758 const recordPath = (app: App, name: string): string => join(root(app), `${name}.json`);
58 const BODY_ID = /^[A-Za-z0-9_-]{1,64}$/;
59 // It opens with a letter or digit: it is handed to `tar` as a file operand, after a `-C` that must
60 // stay an option, so it can never be read as one (#14783).
61 export const BODY_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
5962
6063 /** The archived forges, newest first: one line each on /forges. */
6164 export function archivedForges(app: App): ArchiveRecord[] {
@@ -123,8 +126,28 @@ async function archiveOne(app: App, name: string, options: ArchiveOptions): Prom
123126 const stamp = new Date(now).toISOString().replace(/[-:.]/g, "").slice(0, 15);
124127 mkdirSync(join(root(app), "files"), { recursive: true });
125128 const local = join(root(app), "files", `${name}-${stamp}.tar.gz`);
126 // Stopped first, so nothing writes while it is read.
127 await sleepChild(app, name);
129 // Held closed, then stopped, so nothing writes while it is read nor between the read and the
130 // removal: what it wrote then would be in neither the copy nor the forge (#14783). The hold ends
131 // with the archive, removed or not.
132 const release = holdClosed(app, child.bodyId);
133 try {
134 await sleepChild(app, name);
135 return await copyAndRemove(app, name, child, destination, local, stamp, now, options);
136 } finally {
137 release();
138 }
139 }
140
141 async function copyAndRemove(
142 app: App,
143 name: string,
144 child: Awaited<ReturnType<typeof listChildren>>[number],
145 destination: Destination,
146 local: string,
147 stamp: string,
148 now: number,
149 options: ArchiveOptions,
150 ): Promise<Managed> {
128151 const made = await tar(["-czf", local, "-C", join(app.config.dataDir, "spawn"), `${name}.json`, "-C", join(app.config.dataDir, "children"), child.bodyId]);
129152 if (!made.ok) {
130153 await rm(local, { force: true });
@@ -158,6 +181,13 @@ async function archiveOne(app: App, name: string, options: ArchiveOptions): Prom
158181 for: child.for,
159182 apps: child.apps,
160183 };
184 // A start that was granted before the hold (a request already past the gate) could still have
185 // opened it: then what is copied is not all it holds, and nothing is removed.
186 if (isAwakeBody(app, child.bodyId) || openHere(app, child.bodyId)) {
187 await destination.store.delete(key).catch(() => {});
188 await rm(local, { force: true });
189 return { ok: false, says: `${name} was not archived: it was woken while it was being copied, so nothing is removed; it is running` };
190 }
161191 writeFileSync(recordPath(app, name), JSON.stringify(record));
162192 await removeForge(app, name);
163193 await rm(local, { force: true });
src/spawn/disk_mount.ts +6 −2
@@ -36,8 +36,12 @@ export interface FsSize {
3636 readonly files: number;
3737 }
3838
39 /** A body id is one path segment: letters, digits, `.`, `_`, `-`; a first character other than `.` rules out `.` and `..`. */
40 const BODY_ID = /^[A-Za-z0-9_-][A-Za-z0-9._-]{0,127}$/;
39 /**
40 * A body id is one path segment: letters, digits, `.`, `_`, `-`. The first character is a letter or
41 * digit, which rules out `.` and `..` and an id read as a command-line option (`-x`), as
42 * `spawn/archive.ts` does (#14783).
43 */
44 const BODY_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
4145 export const validBodyId = (id: string): boolean => BODY_ID.test(id);
4246
4347 /** Undo mountinfo's octal escapes: space, tab, newline and backslash are written `\ooo`. */
src/spawn/door.ts +22 −10
@@ -50,7 +50,7 @@ import { cellsAvailable } from "../apps/contain.ts";
5050 import { isSealed } from "./sealed.ts";
5151 import { isProber } from "../release/prober.ts";
5252 import { takePlace } from "./births.ts";
53 import { checkAnonSpawn, noteAnonSpawn } from "./rate.ts";
53 import { reserveAnonSpawn, type Reserved } from "./rate.ts";
5454 import { readdirSync, readFileSync } from "node:fs";
5555 import { readdir, stat } from "node:fs/promises";
5656 import { join } from "node:path";
@@ -383,8 +383,11 @@ export async function spawnDoor(
383383 // A gift code (`actions/codes.ts`) names the plan itself: no admin, no payment, whatever the
384384 // provider.
385385 const giftIn = raw["code"];
386 // One answer to "is a code sent", for the lookup, the plan checks and the claim: a null or empty
387 // code is none, so it exempts no plan check (#14783).
388 const hasGift = giftIn !== undefined && giftIn !== null && giftIn !== "";
386389 let giftSeen: { plan: string } | undefined;
387 if (giftIn !== undefined && giftIn !== null && giftIn !== "") {
390 if (hasGift) {
388391 if (raw["plan"] !== undefined && raw["plan"] !== null && raw["plan"] !== "") return invalid("code", "a code gives its own plan; send the code or a plan, not both");
389392 const seen = giftOf(ctx.app, ctx.request, giftIn);
390393 if (!seen.ok) return seen.error === "slow_down" ? { status: 429, body: { error: "rate_limited", message: REDEEM_WORDS.slow_down } } : invalid("code", REDEEM_WORDS[seen.error]);
@@ -394,8 +397,8 @@ export async function spawnDoor(
394397 if (raw["plan"] !== undefined && raw["plan"] !== null && raw["plan"] !== "") {
395398 const chosen = typeof raw["plan"] === "string" ? getPlan(db, raw["plan"]) : undefined;
396399 if (chosen === undefined) return invalid("plan", `must be one of ${getPlans(db).map((p) => p.key).join(", ")}`);
397 if (anon && chosen.key !== FREE && giftIn === undefined && !paid) return invalid("plan", `the ${chosen.name} plan is for a signed-in person; sign in first`);
398 if (chosen.key !== FREE && user?.isAdmin !== true && giftIn === undefined && !paid) return invalid("plan", PAID_PLAN_ADMIN_ONLY(chosen.name));
400 if (anon && chosen.key !== FREE && !hasGift && !paid) return invalid("plan", `the ${chosen.name} plan is for a signed-in person; sign in first`);
401 if (chosen.key !== FREE && user?.isAdmin !== true && !hasGift && !paid) return invalid("plan", PAID_PLAN_ADMIN_ONLY(chosen.name));
399402 if (size !== undefined && size !== null && size !== "" && size !== chosen.size)
400403 return invalid("size", `the ${chosen.name} plan is a ${chosen.size} forge; leave size out or send ${chosen.size}`);
401404 planKey = chosen.key;
@@ -454,9 +457,11 @@ export async function spawnDoor(
454457 },
455458 };
456459 }
457 // A stranger may start only a few children a day; the plan above is free.
460 // A stranger may start only a few children a day; the plan above is free. The slot is counted
461 // now, before the waits below, and given back unless a forge is made for it (#14783).
462 let slot: Reserved | undefined;
458463 if (anon && counted) {
459 const limit = checkAnonSpawn(ctx.app.db, ctx.request);
464 const limit = (slot = reserveAnonSpawn(ctx.app.db, ctx.request));
460465 if (!limit.allowed)
461466 return {
462467 status: 429,
@@ -468,7 +473,11 @@ export async function spawnDoor(
468473 }
469474 // One rule for births (`spawn/births.ts`): the machine's own figures decide how many are made at
470475 // once, and a spawn that does not fit waits its turn, then is told its place and how long.
471 const place = await takePlace(ctx.app, { anon });
476 const place = await takePlace(ctx.app, { anon }).catch((error: unknown) => {
477 slot?.release();
478 throw error;
479 });
480 if (!place.ok) slot?.release();
472481 if (!place.ok)
473482 return {
474483 status: 503,
@@ -485,9 +494,12 @@ export async function spawnDoor(
485494 // The use is spent just before the forge is made, in one statement; it goes back if no forge is
486495 // made.
487496 let gift: Code | undefined;
488 if (giftIn !== undefined && giftIn !== null && giftIn !== "") {
497 if (hasGift) {
489498 const claimed = claimCode(ctx.app.db, giftIn);
490 if (!claimed.ok) return invalid("code", CODE_WORDS[claimed.error]);
499 if (!claimed.ok) {
500 slot?.release();
501 return invalid("code", CODE_WORDS[claimed.error]);
502 }
491503 gift = claimed.value;
492504 }
493505 const startedAt = Date.now();
@@ -514,9 +526,9 @@ export async function spawnDoor(
514526 } finally {
515527 if (gift !== undefined && !settled) unclaimGift(ctx.app, gift);
516528 if (!born) place.release();
529 if (!born) slot?.release();
517530 }
518531 if (!made.ok) return spawnWords(made.error);
519 if (anon && counted && !made.value.existed) noteAnonSpawn(ctx.app.db, ctx.request);
520532 // The visitor who started (or was given) this forge becomes someone, on the host's side (#11265).
521533 if (!made.value.existed && !paid) linkRequestVisitor(ctx.app, ctx.request, made.value.name, gift === undefined ? "started" : "paid");
522534 // Its apps come up now, so the first visit after a claim finds them awake.
src/spawn/held_closed.ts added +27
@@ -0,0 +1,27 @@
1 /**
2 * Children held closed while their data is copied (#14783): an archive puts a forge to sleep, tars
3 * its data directory and only then removes it, and nothing may wake it in between, or what it wrote
4 * after the tar read its files would be deleted with the directory and kept nowhere. Every wake
5 * path asks `startRefused` (`spawn/host.ts`) once the child is asleep, and it answers the line
6 * below while the hold stands.
7 *
8 * Per forge, the body ids of the archives in flight: at most one per archive running, each released
9 * in a `finally`, gone on restart (an archive does not survive a restart either).
10 */
11
12 import type { App } from "../app.ts";
13
14 const held = new WeakMap<App, Set<string>>();
15
16 /** Hold `bodyId` closed; the answer releases it. */
17 export function holdClosed(parent: App, bodyId: string): () => void {
18 let set = held.get(parent);
19 if (set === undefined) held.set(parent, (set = new Set()));
20 set.add(bodyId);
21 return () => held.get(parent)?.delete(bodyId);
22 }
23
24 /** Why `name` may not start now, when it is held closed; undefined otherwise. */
25 export function closedRefusal(parent: App, ledger: { readonly name: string; readonly bodyId: string }): string | undefined {
26 return held.get(parent)?.has(ledger.bodyId) === true ? `${ledger.name} is being archived and does not start until that is done` : undefined;
27 }
src/spawn/host.ts +4
@@ -12,6 +12,7 @@ import { resolveAppHost, type AppSocket, type AppTarget } from "../apps/proxy.ts
1212 import { childAppsSummary, startChildApps, stopChildApps, stopChildAppsOf } from "./child-apps.ts";
1313 import { childHost } from "./door.ts";
1414 import { markHostedChild } from "./hosted-mark.ts";
15 import { closedRefusal } from "./held_closed.ts";
1516 import { firstBoot } from "./first-boot.ts";
1617 import {
1718 childHostSuffix,
@@ -585,6 +586,9 @@ const carriesCredentials = (parent: App, bodyId: string, request: Request): Prom
585586 * off a start costs no mountinfo read.
586587 */
587588 export function startRefused(parent: App, ledger: { readonly name: string; readonly bodyId: string }): string | undefined {
589 // Being archived: its data is copied, then removed, so nothing may write to it (#14783).
590 const closed = closedRefusal(parent, ledger);
591 if (closed !== undefined) return closed;
588592 // Below the disk floor nothing wakes, whatever its filesystem (#10873).
589593 const floor = floorRefusal(parent.config.dataDir);
590594 if (floor !== undefined) {
src/spawn/rate.ts +24 −3
@@ -41,9 +41,30 @@ export function checkAnonSpawn(db: Database, request: Request, now = Date.now())
4141 return { allowed: !clientFull && !globalFull, resetsAt: new Date(oldest + DAY_MS).toISOString(), perDay: limitValue(db, "spawn.anon_per_day") };
4242 }
4343
44 /** Count an anonymous spawn against this request's client. */
45 export function noteAnonSpawn(db: Database, request: Request, now = Date.now()): void {
46 db.query("INSERT INTO anon_spawn (bucket, at) VALUES (?, ?)").run(clientBucket(request), now);
44 export interface Reserved extends RateLimit {
45 /** Give the slot back: no forge was made for it. A second call does nothing. */
46 readonly release: () => void;
47 }
48
49 /**
50 * Check and count in one step (#14783): when another anonymous spawn may be made, a slot is counted
51 * against this client now, before the spawn's long awaits, so requests sent at once cannot all pass
52 * a check that none has recorded yet. The caller gives the slot back when no forge is made.
53 */
54 export function reserveAnonSpawn(db: Database, request: Request, now = Date.now()): Reserved {
55 return db.transaction((): Reserved => {
56 const limit = checkAnonSpawn(db, request, now);
57 if (!limit.allowed) return { ...limit, release: () => {} };
58 const row = db.query("INSERT INTO anon_spawn (bucket, at) VALUES (?, ?) RETURNING rowid AS id").get(clientBucket(request), now) as { id: number };
59 let held = true;
60 return {
61 ...limit,
62 release: () => {
63 if (held) db.query("DELETE FROM anon_spawn WHERE rowid = ?").run(row.id);
64 held = false;
65 },
66 };
67 }).immediate();
4768 }
4869
4970 /** Forget every counted spawn (tests). */
test/disk-mount.test.ts +1 −1
@@ -75,7 +75,7 @@ describe("a child's writable space, read from mountinfo", () => {
7575
7676 test("a body id is one path segment", () => {
7777 expect(validBodyId("cell-01abc")).toBe(true);
78 for (const bad of ["..", ".", "a/b", "", "../x", "a\nb"]) expect(validBodyId(bad)).toBe(false);
78 for (const bad of ["..", ".", "a/b", "", "../x", "a\nb", "-x", "--to-command=sh", "_a"]) expect(validBodyId(bad)).toBe(false);
7979 });
8080 });
8181
test/forge-archive.test.ts +59
@@ -23,6 +23,9 @@ import { useProbe } from "../src/spawn/door.ts";
2323 import { firstBoot } from "../src/spawn/first-boot.ts";
2424 import { personReading } from "../src/spawn/person_activity.ts";
2525 import { dirStore } from "../src/snapshot/store.ts";
26 import { BODY_ID } from "../src/spawn/archive.ts";
27 import { isAwakeBody, resolveHostedChild, sleepChild, wakeGate } from "../src/spawn/host.ts";
28 import { childPublicUrl } from "../src/spawn/placement.ts";
2629 import { quietLogs } from "./helpers/globals.ts";
2730
2831 quietLogs();
@@ -352,3 +355,59 @@ test("a destination that does not answer is found before the forge is stopped; a
352355 expect(existsSync(childDir)).toBe(true);
353356 expect(readdirSync(join(dir, "archive", "files")).filter((f) => f.startsWith("not-stopped"))).toEqual([]);
354357 });
358
359 test("a forge being archived is not woken by a request while it is copied, and wakes again after a failed archive (#14783)", async () => {
360 const { child } = await makeForge("held-shut", false);
361 child.close();
362 const real = dirStore(store);
363 let release = (): void => {};
364 const gate = new Promise<void>((r) => (release = r));
365 let reached = (): void => {};
366 const atUpload = new Promise<void>((r) => (reached = r));
367 const slow = { ...real, async put(key: string, body: Uint8Array | string) { if (typeof body !== "string") (reached(), await gate); return real.put(key, body); } };
368 const ask = () => new Request(childPublicUrl("held-shut", "https://forge.test"), { headers: { host: new URL(childPublicUrl("held-shut", "https://forge.test")).host } });
369 const archiving = archiveForge(parent, "held-shut", { destination: { source: "dir", store: slow } });
370 await atUpload; // the forge is stopped and its copy is on its way
371 const refused = await wakeGate(parent, ask());
372 expect(refused?.status).toBe(503);
373 expect(await refused?.text()).toContain("being archived");
374 release();
375 expect((await archiving).ok).toBe(true);
376 expect(await childNamed(parent, "held-shut")).toBeUndefined();
377 expect((await restoreForge(parent, "held-shut", { destination: { source: "dir", store: real } })).ok).toBe(true);
378 // A failed archive lets it go: the next request wakes it as before.
379 const big = await archiveForge(parent, "held-shut", { destination: { source: "dir", store: real }, maxBytes: 100 });
380 expect(big.ok).toBe(false);
381 expect(await wakeGate(parent, ask())).toBeUndefined();
382 });
383
384 test("a forge woken anyway while it is copied is not removed (#14783)", async () => {
385 const { childDir, child, made } = await makeForge("woke-midway", false);
386 child.close();
387 const real = dirStore(store);
388 let release = (): void => {};
389 const gate = new Promise<void>((r) => (release = r));
390 let reached = (): void => {};
391 const atUpload = new Promise<void>((r) => (reached = r));
392 const slow = { ...real, async put(key: string, body: Uint8Array | string) { if (typeof body !== "string") (reached(), await gate); return real.put(key, body); } };
393 const archiving = archiveForge(parent, "woke-midway", { destination: { source: "dir", store: slow } });
394 await atUpload;
395 // A request already past the gate when the hold began opens it.
396 const url = childPublicUrl("woke-midway", "https://forge.test");
397 expect(await resolveHostedChild(parent, new Request(url, { headers: { host: new URL(url).host } }))).not.toBe("unknown");
398 expect(isAwakeBody(parent, made.bodyId)).toBe(true);
399 release();
400 const done = await archiving;
401 expect(done.ok).toBe(false);
402 expect(done.says).toContain("woken while it was being copied");
403 expect(existsSync(childDir)).toBe(true);
404 expect(await childNamed(parent, "woke-midway")).toBeDefined();
405 expect(archivedForges(parent).some((a) => a.name === "woke-midway")).toBe(false);
406 expect((await real.list("forge-archives/woke-midway/")).length).toBe(0);
407 await sleepChild(parent, "woke-midway");
408 });
409
410 test("a body id is never read by tar as an option (#14783)", () => {
411 for (const bad of ["--remove-files", "-P", "_x", ""]) expect(BODY_ID.test(bad)).toBe(false);
412 for (const good of ["01m23vygyffx5hg2f5vm8r74sr", "a_b-c"]) expect(BODY_ID.test(good)).toBe(true);
413 });
test/gift-codes.test.ts +10
@@ -245,6 +245,16 @@ describe("gift codes", () => {
245245 expect((await send("POST", "/api/v1/forges", bob, { name: "both", code: m.code, plan: "large" })).status).toBe(422);
246246 });
247247
248 test("a null or empty code is no code: a paid plan's plan is still refused to a non-admin and a stranger (#14783)", async () => {
249 fresh();
250 for (const code of [null, ""])
251 for (const token of [bob, undefined]) {
252 const res = await send("POST", "/api/v1/forges", token, { name: "nocode", code, plan: "large" });
253 expect(res.status).toBe(422);
254 expect(JSON.stringify(await res.json())).toContain("plan");
255 }
256 });
257
248258 test("MCP forges plans a code first, then makes and revokes it with dryRun false", async () => {
249259 fresh();
250260 const plan = await mcp(admin, { code: { plan: "small", note: "mcp" } });
test/release-prober.test.ts +3 −3
@@ -13,7 +13,7 @@ import { createApp, type App } from "../src/app.ts";
1313 import { serve, type Servers } from "../src/http/server.ts";
1414 import { ok } from "../src/kernel/result.ts";
1515 import { usePlacement, useProbe } from "../src/spawn/door.ts";
16 import { noteAnonSpawn, resetAnonSpawns } from "../src/spawn/rate.ts";
16 import { resetAnonSpawns } from "../src/spawn/rate.ts";
1717 import { isProber, PROBER_HEADER, proberKey } from "../src/release/prober.ts";
1818 import { walkJourney } from "../src/release/journey.ts";
1919 import { LIMITS } from "../src/settings/limits.ts";
@@ -60,8 +60,8 @@ test("only this boot's key is the prober: not the hook secret, not another boot'
6060
6161 test("when the strangers' day is spent, the prober starts a forge and spends nothing; a stranger and a forger land on start=rate", async () => {
6262 resetAnonSpawns(app.db);
63 for (let i = 0; i < ANON_SPAWNS_GLOBAL_PER_DAY; i++)
64 noteAnonSpawn(app.db, new Request(`http://x/${i}`, { headers: { "x-forwarded-for": `10.${(i >> 8) & 255}.${i & 255}.1` } }));
63 // The global count filled row by row: each its own bucket, past any per-client cap.
64 for (let i = 0; i < ANON_SPAWNS_GLOBAL_PER_DAY; i++) app.db.query("INSERT INTO anon_spawn (bucket, at) VALUES (?, ?)").run(`fill-${i}`, Date.now());
6565 const before = spent();
6666
6767 for (const extra of [{}, { [PROBER_HEADER]: "0".repeat(64) }, { [PROBER_HEADER]: app.hookSecret }, { [PROBER_HEADER]: proberKey("forged") }]) {
test/snapshot-hosted.test.ts +4
@@ -145,6 +145,10 @@ describe("hosted forges in the snapshot", () => {
145145 expect(bodyIdOf('{"bodyId":"abc_1"}')).toBe("abc_1");
146146 expect(bodyIdOf('{"bodyId":"../x"}')).toBeUndefined();
147147 expect(bodyIdOf('{"bodyId":3}')).toBeUndefined();
148 // A leading `-` or `_` is refused: an id never reads as an option (#14783).
149 expect(bodyIdOf('{"bodyId":"-x"}')).toBeUndefined();
150 expect(bodyIdOf('{"bodyId":"--to-command=sh"}')).toBeUndefined();
151 expect(bodyIdOf('{"bodyId":"_a"}')).toBeUndefined();
148152 expect(bodyIdOf("")).toBeUndefined();
149153 });
150154
test/spawn-anon-burst.test.ts added +81
@@ -0,0 +1,81 @@
1 /**
2 * #14783: the anonymous spawn limit is counted as each request arrives, not after its spawn has
3 * finished, so requests a stranger sends at once cannot all pass a check none has recorded yet. A
4 * slot that makes no forge is given back.
5 */
6 import { afterAll, beforeAll, expect, test } from "bun:test";
7 import { existsSync } from "node:fs";
8 import { mkdtemp, rm } from "node:fs/promises";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { createApp, type App } from "../src/app.ts";
12 import { setStrangerLimits, strangerLimits } from "../src/http/strangers.ts";
13 import { serve, type Servers } from "../src/http/server.ts";
14 import { ok } from "../src/kernel/result.ts";
15 import { LIMITS, setLimit } from "../src/settings/limits.ts";
16 import { usePlacement, useProbe } from "../src/spawn/door.ts";
17 import { bootHostedChild } from "../src/spawn/host.ts";
18 import { reserveAnonSpawn, resetAnonSpawns } from "../src/spawn/rate.ts";
19 import { quietLogs } from "./helpers/globals.ts";
20
21 quietLogs();
22
23 const PER_DAY = LIMITS["spawn.anon_per_day"].default;
24 const NAMES = ["burst1", "burst2", "burst3"];
25 let dataDir = "";
26 let app: App;
27 let servers: Servers;
28 let placed = 0;
29
30 const count = (): number => (app.db.query("SELECT COUNT(*) AS n FROM anon_spawn").get() as { n: number }).n;
31
32 beforeAll(async () => {
33 dataDir = await mkdtemp(join(tmpdir(), "forge-anon-burst-"));
34 app = createApp({ dataDir, port: 0 });
35 setStrangerLimits(app, { perAddress: { ...strangerLimits(app).perAddress, plan: 1000 } });
36 setLimit(app.db, "spawn.births_at_once", 8, "test");
37 useProbe(app, () => async () => true);
38 usePlacement(app, {
39 place: async (plan) => ok({ url: `https://${plan.name}.fake.test`, bodyId: `fake${++placed}` }),
40 retire: async () => ok(),
41 });
42 servers = await serve(app);
43 }, 30_000);
44
45 afterAll(async () => {
46 for (const name of NAMES) if (existsSync(join(dataDir, "spawn", `${name}.json`))) await bootHostedChild(app, name);
47 servers.stop();
48 app.close();
49 await rm(dataDir, { recursive: true, force: true });
50 });
51
52 test("a reserved slot counts at once and a released one is given back, once", () => {
53 resetAnonSpawns(app.db);
54 const req = new Request("http://x/", { headers: { "x-forwarded-for": "203.0.113.7" } });
55 const slots = Array.from({ length: PER_DAY + 2 }, () => reserveAnonSpawn(app.db, req));
56 expect(slots.filter((s) => s.allowed).length).toBe(PER_DAY);
57 expect(count()).toBe(PER_DAY);
58 slots[0]!.release();
59 slots[0]!.release();
60 slots[PER_DAY]!.release(); // refused: holds nothing to give back
61 expect(count()).toBe(PER_DAY - 1);
62 expect(reserveAnonSpawn(app.db, req).allowed).toBe(true);
63 resetAnonSpawns(app.db);
64 });
65
66 test("spawns a stranger sends at once get only the slots left, and only a made forge is counted", async () => {
67 resetAnonSpawns(app.db);
68 // One left for this client: the day's count is 1.
69 setLimit(app.db, "spawn.anon_per_day", 1, "test");
70 const statuses = await Promise.all(
71 NAMES.map(async (name) => {
72 const res = await fetch(`${servers.url}/api/v1/forges`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ name, dryRun: false }) });
73 await res.body?.cancel();
74 return res.status;
75 }),
76 );
77 expect(statuses.filter((s) => s === 201).length).toBe(1);
78 expect(statuses.filter((s) => s === 429).length).toBe(2);
79 expect(count()).toBe(1);
80 setLimit(app.db, "spawn.anon_per_day", null, "test");
81 }, 30_000);
test/spawn-anon.test.ts +3 −3
@@ -12,7 +12,7 @@ import { createApp, type App } from "../src/app.ts";
1212 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1313 import { usePlacement, useProbe } from "../src/spawn/door.ts";
1414 import { Database } from "bun:sqlite";
15 import { checkAnonSpawn, noteAnonSpawn, resetAnonSpawns } from "../src/spawn/rate.ts";
15 import { checkAnonSpawn, reserveAnonSpawn, resetAnonSpawns } from "../src/spawn/rate.ts";
1616 import { createToken } from "../src/core/tokens.ts";
1717 import { createUser } from "../src/core/users.ts";
1818 import { serve, type Servers } from "../src/http/server.ts";
@@ -222,7 +222,7 @@ describe("anonymous spawn", () => {
222222
223223 test("the counts survive a restart and a global cap holds", async () => {
224224 const req = new Request("http://x/", { headers: { "x-forwarded-for": "203.0.113.9" } });
225 for (let i = 0; i < ANON_SPAWNS_PER_DAY; i++) noteAnonSpawn(app.db, req);
225 for (let i = 0; i < ANON_SPAWNS_PER_DAY; i++) reserveAnonSpawn(app.db, req);
226226 expect(checkAnonSpawn(app.db, req).allowed).toBe(false);
227227 // A new connection to the same file still sees the counted spawns.
228228 const again = new Database(join(dataDir, "forge.db"));
@@ -232,7 +232,7 @@ describe("anonymous spawn", () => {
232232 expect(checkAnonSpawn(app.db, req, Date.now() + 86_400_001).allowed).toBe(true);
233233 resetAnonSpawns(app.db);
234234 for (let i = 0; i < ANON_SPAWNS_GLOBAL_PER_DAY; i++)
235 noteAnonSpawn(app.db, new Request(`http://x/${i}`, { headers: { "x-forwarded-for": `198.51.100.${i}` } }));
235 reserveAnonSpawn(app.db, new Request(`http://x/${i}`, { headers: { "x-forwarded-for": `198.51.100.${i}` } }));
236236 expect(checkAnonSpawn(app.db, req).allowed).toBe(false);
237237 resetAnonSpawns(app.db);
238238 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs