Public: anyone can read this; the crew can change it.
A test proves a spawn body id beginning with `-` is refused before it reaches tar #15046
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15029-a-test-proves-a-spawn-body-id-beginning → main 1 commit fork
2 files
changed +103
−0
test/helpers/archive-child.ts added +17
| @@ -0,0 +1,17 @@ | ||
| 1 | /** | |
| 2 | * Archive one hosted forge in a process of its own (`test/spawn-archive-dash-id.test.ts`). Bun | |
| 3 | * looks `tar` up on the PATH the process started with, so the PATH the test sets is the one this | |
| 4 | * process's `tar` comes from. Prints the archive's answer as one JSON line. | |
| 5 | */ | |
| 6 | import { createApp } from "../../src/app.ts"; | |
| 7 | import { archiveForge } from "../../src/spawn/archive.ts"; | |
| 8 | import { useChildFetch } from "../../src/spawn/clients.ts"; | |
| 9 | import { useProbe } from "../../src/spawn/door.ts"; | |
| 10 | import { dirStore } from "../../src/snapshot/store.ts"; | |
| 11 | ||
| 12 | const app = createApp({ dataDir: process.env["DATA_DIR"]!, url: "https://forge.test", port: 0 }); | |
| 13 | useProbe(app, () => async () => true); | |
| 14 | useChildFetch(app, async () => new Response("{}", { status: 503 })); | |
| 15 | const done = await archiveForge(app, process.env["NAME"]!, { destination: { source: "dir", store: dirStore(process.env["STORE"]!) } }); | |
| 16 | app.close(); | |
| 17 | console.log(JSON.stringify(done)); |
test/spawn-archive-dash-id.test.ts added +86
| @@ -0,0 +1,86 @@ | ||
| 1 | /** | |
| 2 | * #15029: a spawn body id that begins with `-` is refused at the archive's boundary (`BODY_ID` in | |
| 3 | * `src/spawn/archive.ts`, #15004), before any `tar` process starts. Otherwise tar would read the id | |
| 4 | * as an option, so `--checkpoint-action=exec=…` would run a command. The witness is a `tar` first | |
| 5 | * on the PATH of the process that archives (`test/helpers/archive-child.ts`), which writes down | |
| 6 | * every call it gets and does nothing else. | |
| 7 | */ | |
| 8 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 9 | import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; | |
| 10 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 11 | import { tmpdir } from "node:os"; | |
| 12 | import { join } from "node:path"; | |
| 13 | import { childDirOf, childNamed } from "../src/actions/spawn.ts"; | |
| 14 | import { createApp, type App } from "../src/app.ts"; | |
| 15 | import { serve } from "../src/http/server.ts"; | |
| 16 | import { setStrangerLimits, strangerLimits } from "../src/http/strangers.ts"; | |
| 17 | import { useChildFetch } from "../src/spawn/clients.ts"; | |
| 18 | import { useProbe } from "../src/spawn/door.ts"; | |
| 19 | import { dirStore } from "../src/snapshot/store.ts"; | |
| 20 | import { quietLogs } from "./helpers/globals.ts"; | |
| 21 | ||
| 22 | quietLogs(); | |
| 23 | ||
| 24 | let dir = ""; | |
| 25 | let store = ""; | |
| 26 | let bin = ""; | |
| 27 | const evil = "--checkpoint-action=exec=touch pwned"; | |
| 28 | ||
| 29 | /** Every argv the spy `tar` was started with, one line each. */ | |
| 30 | const tarCalls = () => (existsSync(join(bin, "calls")) ? readFileSync(join(bin, "calls"), "utf8").split("\n").filter(Boolean) : []); | |
| 31 | ||
| 32 | /** Runs `argv` with the spy first on its PATH; what it printed. */ | |
| 33 | async function withSpy(argv: string[], env: Record<string, string> = {}): Promise<string> { | |
| 34 | const p = Bun.spawn(argv, { env: { ...process.env, ...env, PATH: `${bin}:${process.env["PATH"] ?? ""}` }, stdout: "pipe", stderr: "inherit" }); | |
| 35 | const out = await new Response(p.stdout).text(); | |
| 36 | await p.exited; | |
| 37 | return out; | |
| 38 | } | |
| 39 | ||
| 40 | beforeAll(async () => { | |
| 41 | dir = await mkdtemp(join(tmpdir(), "spawn-dash-id-")); | |
| 42 | store = await mkdtemp(join(tmpdir(), "spawn-dash-id-store-")); | |
| 43 | bin = await mkdtemp(join(tmpdir(), "spawn-dash-id-bin-")); | |
| 44 | writeFileSync(join(bin, "tar"), `#!/bin/sh\nprintf '%s\\n' "$*" >> '${join(bin, "calls")}'\nexit 1\n`); | |
| 45 | chmodSync(join(bin, "tar"), 0o755); | |
| 46 | // A forge the host made, then its ledger and data directory given a body id tar would read as an | |
| 47 | // option that runs a command. | |
| 48 | const parent: App = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); | |
| 49 | useProbe(parent, () => async () => true); | |
| 50 | useChildFetch(parent, async () => new Response("{}", { status: 503 })); | |
| 51 | setStrangerLimits(parent, { perAddress: { ...strangerLimits(parent).perAddress, plan: 1000 } }); | |
| 52 | const servers = await serve(parent); | |
| 53 | const res = await fetch(`${servers.url}/forges/start`, { method: "POST", redirect: "manual", headers: { origin: "https://forge.test" }, body: new URLSearchParams({ name: "dash-body" }) }); | |
| 54 | await res.body?.cancel(); | |
| 55 | if (res.status !== 200) throw new Error(`start answered ${res.status}`); | |
| 56 | const made = (await childNamed(parent, "dash-body"))!; | |
| 57 | if (!made.done) throw new Error("the forge was not made"); | |
| 58 | mkdirSync(childDirOf(parent, made.bodyId), { recursive: true }); | |
| 59 | renameSync(childDirOf(parent, made.bodyId), childDirOf(parent, evil)); | |
| 60 | const ledger = join(dir, "spawn", "dash-body.json"); | |
| 61 | writeFileSync(ledger, JSON.stringify({ ...JSON.parse(readFileSync(ledger, "utf8")), bodyId: evil })); | |
| 62 | if ((await childNamed(parent, "dash-body"))?.bodyId !== evil) throw new Error("the ledger did not take the id"); | |
| 63 | servers.stop(); | |
| 64 | parent.close(); | |
| 65 | }, 30_000); | |
| 66 | ||
| 67 | afterAll(async () => { | |
| 68 | for (const d of [dir, store, bin]) await rm(d, { recursive: true, force: true }); | |
| 69 | }); | |
| 70 | ||
| 71 | test("the spy is the tar a process started with its PATH runs", async () => { | |
| 72 | // Without this, "no tar call" could only mean the spy was never reached. | |
| 73 | await withSpy(["bun", "-e", `await Bun.spawn(["tar", "--version"]).exited`]); | |
| 74 | expect(tarCalls()).toEqual(["--version"]); | |
| 75 | writeFileSync(join(bin, "calls"), ""); | |
| 76 | }); | |
| 77 | ||
| 78 | test("a spawn body id that begins with - is refused before any tar process starts", async () => { | |
| 79 | const out = await withSpy(["bun", join(import.meta.dir, "helpers", "archive-child.ts")], { DATA_DIR: dir, STORE: store, NAME: "dash-body" }); | |
| 80 | const done = JSON.parse(out.trim().split("\n").at(-1)!) as { ok: boolean; says: string }; | |
| 81 | expect(done).toEqual({ ok: false, says: "dash-body has no data directory to archive" }); | |
| 82 | expect(tarCalls()).toEqual([]); | |
| 83 | expect(existsSync(join(dir, "spawn", "dash-body.json"))).toBe(true); | |
| 84 | expect(existsSync(join(dir, "children", evil))).toBe(true); | |
| 85 | expect(await dirStore(store).list("forge-archives/")).toEqual([]); | |
| 86 | }, 20_000); |
new above
offline