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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14931-the-four-src-spawn-deepsec-fixes-land-fr → main 1 commit fork
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 | ||
| 48 | 48 | `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`). |
| 49 | 49 | |
| 50 | 50 | `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. | |
| 51 | 52 | `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. |
| 52 | 53 | `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. |
| 53 | 54 | `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`, `${ | ||
| 77 | 77 | export const HISTORY_DIRS = ["runs", "drop", "ledger"] as const; |
| 78 | 78 | export const HISTORY_FILES = ["parent.json", "plan.json"] as const; |
| 79 | 79 | |
| 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}$/; | |
| 81 | 82 | |
| 82 | 83 | /** The data directory name a forge's ledger text names, or undefined when it names none. */ |
| 83 | 84 | export function bodyIdOf(ledger: string): string | undefined { |
src/spawn/archive.ts +34 −4
| @@ -22,7 +22,8 @@ import { childPerson } from "./child_db.ts"; | ||
| 22 | 22 | import { dropServed, type ClientRow } from "./clients.ts"; |
| 23 | 23 | import { needsNotice } from "./archive_proposal.ts"; |
| 24 | 24 | 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"; | |
| 26 | 27 | import { CHILD_NAME } from "./placement.ts"; |
| 27 | 28 | |
| 28 | 29 | export interface ArchiveRecord { |
| @@ -55,7 +56,9 @@ const busy = new Set<string>(); | ||
| 55 | 56 | |
| 56 | 57 | const root = (app: App): string => join(app.config.dataDir, "archive"); |
| 57 | 58 | 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}$/; | |
| 59 | 62 | |
| 60 | 63 | /** The archived forges, newest first: one line each on /forges. */ |
| 61 | 64 | export function archivedForges(app: App): ArchiveRecord[] { |
| @@ -123,8 +126,28 @@ async function archiveOne(app: App, name: string, options: ArchiveOptions): Prom | ||
| 123 | 126 | const stamp = new Date(now).toISOString().replace(/[-:.]/g, "").slice(0, 15); |
| 124 | 127 | mkdirSync(join(root(app), "files"), { recursive: true }); |
| 125 | 128 | 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> { | |
| 128 | 151 | const made = await tar(["-czf", local, "-C", join(app.config.dataDir, "spawn"), `${name}.json`, "-C", join(app.config.dataDir, "children"), child.bodyId]); |
| 129 | 152 | if (!made.ok) { |
| 130 | 153 | await rm(local, { force: true }); |
| @@ -158,6 +181,13 @@ async function archiveOne(app: App, name: string, options: ArchiveOptions): Prom | ||
| 158 | 181 | for: child.for, |
| 159 | 182 | apps: child.apps, |
| 160 | 183 | }; |
| 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 | } | |
| 161 | 191 | writeFileSync(recordPath(app, name), JSON.stringify(record)); |
| 162 | 192 | await removeForge(app, name); |
| 163 | 193 | await rm(local, { force: true }); |
src/spawn/disk_mount.ts +6 −2
| @@ -36,8 +36,12 @@ export interface FsSize { | ||
| 36 | 36 | readonly files: number; |
| 37 | 37 | } |
| 38 | 38 | |
| 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}$/; | |
| 41 | 45 | export const validBodyId = (id: string): boolean => BODY_ID.test(id); |
| 42 | 46 | |
| 43 | 47 | /** 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"; | ||
| 50 | 50 | import { isSealed } from "./sealed.ts"; |
| 51 | 51 | import { isProber } from "../release/prober.ts"; |
| 52 | 52 | import { takePlace } from "./births.ts"; |
| 53 | import { checkAnonSpawn, noteAnonSpawn } from "./rate.ts"; | |
| 53 | import { reserveAnonSpawn, type Reserved } from "./rate.ts"; | |
| 54 | 54 | import { readdirSync, readFileSync } from "node:fs"; |
| 55 | 55 | import { readdir, stat } from "node:fs/promises"; |
| 56 | 56 | import { join } from "node:path"; |
| @@ -383,8 +383,11 @@ export async function spawnDoor( | ||
| 383 | 383 | // A gift code (`actions/codes.ts`) names the plan itself: no admin, no payment, whatever the |
| 384 | 384 | // provider. |
| 385 | 385 | 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 !== ""; | |
| 386 | 389 | let giftSeen: { plan: string } | undefined; |
| 387 | if (giftIn !== undefined && giftIn !== null && giftIn !== "") { | |
| 390 | if (hasGift) { | |
| 388 | 391 | 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"); |
| 389 | 392 | const seen = giftOf(ctx.app, ctx.request, giftIn); |
| 390 | 393 | 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( | ||
| 394 | 397 | if (raw["plan"] !== undefined && raw["plan"] !== null && raw["plan"] !== "") { |
| 395 | 398 | const chosen = typeof raw["plan"] === "string" ? getPlan(db, raw["plan"]) : undefined; |
| 396 | 399 | 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)); | |
| 399 | 402 | if (size !== undefined && size !== null && size !== "" && size !== chosen.size) |
| 400 | 403 | return invalid("size", `the ${chosen.name} plan is a ${chosen.size} forge; leave size out or send ${chosen.size}`); |
| 401 | 404 | planKey = chosen.key; |
| @@ -454,9 +457,11 @@ export async function spawnDoor( | ||
| 454 | 457 | }, |
| 455 | 458 | }; |
| 456 | 459 | } |
| 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; | |
| 458 | 463 | if (anon && counted) { |
| 459 | const limit = checkAnonSpawn(ctx.app.db, ctx.request); | |
| 464 | const limit = (slot = reserveAnonSpawn(ctx.app.db, ctx.request)); | |
| 460 | 465 | if (!limit.allowed) |
| 461 | 466 | return { |
| 462 | 467 | status: 429, |
| @@ -468,7 +473,11 @@ export async function spawnDoor( | ||
| 468 | 473 | } |
| 469 | 474 | // One rule for births (`spawn/births.ts`): the machine's own figures decide how many are made at |
| 470 | 475 | // 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(); | |
| 472 | 481 | if (!place.ok) |
| 473 | 482 | return { |
| 474 | 483 | status: 503, |
| @@ -485,9 +494,12 @@ export async function spawnDoor( | ||
| 485 | 494 | // The use is spent just before the forge is made, in one statement; it goes back if no forge is |
| 486 | 495 | // made. |
| 487 | 496 | let gift: Code | undefined; |
| 488 | if (giftIn !== undefined && giftIn !== null && giftIn !== "") { | |
| 497 | if (hasGift) { | |
| 489 | 498 | 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 | } | |
| 491 | 503 | gift = claimed.value; |
| 492 | 504 | } |
| 493 | 505 | const startedAt = Date.now(); |
| @@ -514,9 +526,9 @@ export async function spawnDoor( | ||
| 514 | 526 | } finally { |
| 515 | 527 | if (gift !== undefined && !settled) unclaimGift(ctx.app, gift); |
| 516 | 528 | if (!born) place.release(); |
| 529 | if (!born) slot?.release(); | |
| 517 | 530 | } |
| 518 | 531 | if (!made.ok) return spawnWords(made.error); |
| 519 | if (anon && counted && !made.value.existed) noteAnonSpawn(ctx.app.db, ctx.request); | |
| 520 | 532 | // The visitor who started (or was given) this forge becomes someone, on the host's side (#11265). |
| 521 | 533 | if (!made.value.existed && !paid) linkRequestVisitor(ctx.app, ctx.request, made.value.name, gift === undefined ? "started" : "paid"); |
| 522 | 534 | // 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 | ||
| 12 | 12 | import { childAppsSummary, startChildApps, stopChildApps, stopChildAppsOf } from "./child-apps.ts"; |
| 13 | 13 | import { childHost } from "./door.ts"; |
| 14 | 14 | import { markHostedChild } from "./hosted-mark.ts"; |
| 15 | import { closedRefusal } from "./held_closed.ts"; | |
| 15 | 16 | import { firstBoot } from "./first-boot.ts"; |
| 16 | 17 | import { |
| 17 | 18 | childHostSuffix, |
| @@ -585,6 +586,9 @@ const carriesCredentials = (parent: App, bodyId: string, request: Request): Prom | ||
| 585 | 586 | * off a start costs no mountinfo read. |
| 586 | 587 | */ |
| 587 | 588 | 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; | |
| 588 | 592 | // Below the disk floor nothing wakes, whatever its filesystem (#10873). |
| 589 | 593 | const floor = floorRefusal(parent.config.dataDir); |
| 590 | 594 | if (floor !== undefined) { |
src/spawn/rate.ts +24 −3
| @@ -41,9 +41,30 @@ export function checkAnonSpawn(db: Database, request: Request, now = Date.now()) | ||
| 41 | 41 | return { allowed: !clientFull && !globalFull, resetsAt: new Date(oldest + DAY_MS).toISOString(), perDay: limitValue(db, "spawn.anon_per_day") }; |
| 42 | 42 | } |
| 43 | 43 | |
| 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(); | |
| 47 | 68 | } |
| 48 | 69 | |
| 49 | 70 | /** 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", () => { | ||
| 75 | 75 | |
| 76 | 76 | test("a body id is one path segment", () => { |
| 77 | 77 | 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); | |
| 79 | 79 | }); |
| 80 | 80 | }); |
| 81 | 81 |
test/forge-archive.test.ts +59
| @@ -23,6 +23,9 @@ import { useProbe } from "../src/spawn/door.ts"; | ||
| 23 | 23 | import { firstBoot } from "../src/spawn/first-boot.ts"; |
| 24 | 24 | import { personReading } from "../src/spawn/person_activity.ts"; |
| 25 | 25 | 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"; | |
| 26 | 29 | import { quietLogs } from "./helpers/globals.ts"; |
| 27 | 30 | |
| 28 | 31 | quietLogs(); |
| @@ -352,3 +355,59 @@ test("a destination that does not answer is found before the forge is stopped; a | ||
| 352 | 355 | expect(existsSync(childDir)).toBe(true); |
| 353 | 356 | expect(readdirSync(join(dir, "archive", "files")).filter((f) => f.startsWith("not-stopped"))).toEqual([]); |
| 354 | 357 | }); |
| 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", () => { | ||
| 245 | 245 | expect((await send("POST", "/api/v1/forges", bob, { name: "both", code: m.code, plan: "large" })).status).toBe(422); |
| 246 | 246 | }); |
| 247 | 247 | |
| 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 | ||
| 248 | 258 | test("MCP forges plans a code first, then makes and revokes it with dryRun false", async () => { |
| 249 | 259 | fresh(); |
| 250 | 260 | 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"; | ||
| 13 | 13 | import { serve, type Servers } from "../src/http/server.ts"; |
| 14 | 14 | import { ok } from "../src/kernel/result.ts"; |
| 15 | 15 | 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"; | |
| 17 | 17 | import { isProber, PROBER_HEADER, proberKey } from "../src/release/prober.ts"; |
| 18 | 18 | import { walkJourney } from "../src/release/journey.ts"; |
| 19 | 19 | 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' | ||
| 60 | 60 | |
| 61 | 61 | 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 () => { |
| 62 | 62 | 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()); | |
| 65 | 65 | const before = spent(); |
| 66 | 66 | |
| 67 | 67 | 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", () => { | ||
| 145 | 145 | expect(bodyIdOf('{"bodyId":"abc_1"}')).toBe("abc_1"); |
| 146 | 146 | expect(bodyIdOf('{"bodyId":"../x"}')).toBeUndefined(); |
| 147 | 147 | 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(); | |
| 148 | 152 | expect(bodyIdOf("")).toBeUndefined(); |
| 149 | 153 | }); |
| 150 | 154 |
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"; | ||
| 12 | 12 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 13 | 13 | import { usePlacement, useProbe } from "../src/spawn/door.ts"; |
| 14 | 14 | 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"; | |
| 16 | 16 | import { createToken } from "../src/core/tokens.ts"; |
| 17 | 17 | import { createUser } from "../src/core/users.ts"; |
| 18 | 18 | import { serve, type Servers } from "../src/http/server.ts"; |
| @@ -222,7 +222,7 @@ describe("anonymous spawn", () => { | ||
| 222 | 222 | |
| 223 | 223 | test("the counts survive a restart and a global cap holds", async () => { |
| 224 | 224 | 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); | |
| 226 | 226 | expect(checkAnonSpawn(app.db, req).allowed).toBe(false); |
| 227 | 227 | // A new connection to the same file still sees the counted spawns. |
| 228 | 228 | const again = new Database(join(dataDir, "forge.db")); |
| @@ -232,7 +232,7 @@ describe("anonymous spawn", () => { | ||
| 232 | 232 | expect(checkAnonSpawn(app.db, req, Date.now() + 86_400_001).allowed).toBe(true); |
| 233 | 233 | resetAnonSpawns(app.db); |
| 234 | 234 | 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}` } })); | |
| 236 | 236 | expect(checkAnonSpawn(app.db, req).allowed).toBe(false); |
| 237 | 237 | resetAnonSpawns(app.db); |
| 238 | 238 | }); |
new above
offline