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

On a cell child, [looks] gets a picture of the child's own app #12234

Merged opened this

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

Merged

mason/forge:mason/10620-on-a-cell-child-looks-gets-a-picture-of → main 15 commits fork

  • Merged. mason/10620-on-a-cell-child-looks-gets-a-picture-of was merged into main as 7e2f7e7.

Review

Sign in to review this change.
3 files changed +209 −0
scripts/gate.ts +1
@@ -50,6 +50,7 @@ export const EXCLUDED: readonly Exclusion[] = [];
5050 * short: an entry is a debt with an owner, not a home.
5151 */
5252 export const SLOW: readonly Exclusion[] = [
53 { file: "test/spawn-cell-looks.test.ts", reason: "boots a real child forge in a cell (~30 s) and waits for its own looks job to take a picture", issue: 10620 },
5354 // From the breach list closing this pull's check log at e2a5c15 (#12088):
5455 // every test over the 2 s budget, its file named once with its slowest time.
5556 { file: "test/account-budget.test.ts", reason: "starts stand-in agent runs per account to reach and test spend ceilings; 10.5 s a test on the gate box", issue: 1242 },
test/helpers/cell-looks-e2e.ts added +149
@@ -0,0 +1,149 @@
1 /**
2 * A child forge in a real cell takes a picture of its own app (#10620), end to
3 * end, as `test/spawn-cell-looks.test.ts` drives it. Its own process, for the
4 * reason `real-spawn-e2e.ts` is: a check sets `FORGE_CELLS=off` for the forges
5 * its tests boot.
6 *
7 * A parent makes a child forge in a real cell from one app, waits for the
8 * child's own looks job to write the row for `/` (read from the child's
9 * database), then asks the app, which runs inside the cell, to fetch two hosts
10 * through the cell's egress proxy: one its forge.json declares (the positive
11 * control: the path from app to proxy works) and one it does not (must be
12 * refused by the proxy's 403, `X-Forge-Egress: refused`). Printed: one JSON line.
13 */
14 import { Database } from "bun:sqlite";
15 import { existsSync, mkdirSync, mkdtempSync, rmSync } from "node:fs";
16 import { tmpdir } from "node:os";
17 import { join } from "node:path";
18 // spawn.ts first: entered through actions/repos.ts or app.ts, the import cycle
19 // through actions/make_it_yours.ts reads FORKS_PER_DAY before it exists.
20 import { childDirOf, spawnForge } from "../../src/actions/spawn.ts";
21 import { createRepo } from "../../src/actions/repos.ts";
22 import { createApp } from "../../src/app.ts";
23 import { cellsAvailable } from "../../src/apps/contain.ts";
24 import { createUser, type User } from "../../src/core/users.ts";
25 import { repoDir } from "../../src/git/repos.ts";
26 import { commitFiles } from "../../src/git/write.ts";
27 import { serve } from "../../src/http/server.ts";
28 import { cellPlacement, stopCellChildren } from "../../src/spawn/cell.ts";
29 import { bootHostedChild } from "../../src/spawn/host.ts";
30 import { memoryBodies } from "../../src/spawn/placement.ts";
31 import "./roomy-disk.ts";
32
33 // The test's timeout is these two plus slack (test/spawn-cell-looks.test.ts).
34 const SPAWN_MS = 120_000;
35 const LOOKS_MS = 200_000;
36 const ALLOWED = "example.org";
37 const OUTSIDE = "example.com";
38
39 const cells = await cellsAvailable();
40 if (!cells.ok) {
41 console.log(JSON.stringify({ skipped: cells.reason }));
42 process.exit(0);
43 }
44
45 // The app: `/` is a page; `/reach?host=` fetches that host through the cell's proxy and
46 // says `refused:<its reason>` only for the proxy's own refusal, `error` for anything thrown, else
47 // `answered:<status>`.
48 const APP = `Bun.serve({ port: Number(process.env.PORT), async fetch(req) {
49 const url = new URL(req.url);
50 if (url.pathname === "/reach") {
51 const host = url.searchParams.get("host") ?? "";
52 try {
53 const res = await fetch("http://" + host + "/", { proxy: process.env.HTTP_PROXY, signal: AbortSignal.timeout(10000) });
54 if (res.status === 403 && res.headers.get("x-forge-egress") === "refused") return new Response("refused:" + (await res.text()).trim().slice(0, 160));
55 await res.body?.cancel();
56 return new Response("answered:" + res.status);
57 } catch (e) { return new Response("error:" + String(e).slice(0, 120) + " proxy=" + (process.env.HTTP_PROXY ?? "unset")); }
58 }
59 return new Response("<!doctype html><title>bakery</title><h1>bakery says hi</h1>", { headers: { "content-type": "text/html; charset=utf-8" } });
60 } })
61 `;
62
63 const dataDir = mkdtempSync(join(tmpdir(), "cell-looks-"));
64 mkdirSync(dataDir, { recursive: true });
65 const parent = createApp({ dataDir, url: "https://forge.test", port: 0 });
66 const servers = await serve(parent);
67 let pending: Promise<unknown> = Promise.resolve();
68 const cleanup = async () => {
69 servers.stop();
70 await Promise.race([pending.catch(() => undefined), Bun.sleep(15_000)]);
71 await stopCellChildren(parent);
72 };
73 process.on("SIGTERM", () => void cleanup().finally(() => process.exit(143)));
74
75 async function spawnAndBoot(alice: User) {
76 const made = await spawnForge(
77 parent,
78 alice,
79 { name: "kid", org: "kids", apps: ["alice/bakery"], carryBriefs: false, carrySubscriptions: false },
80 { catalog: [{ ref: "alice/bakery", readable: true, bytes: 10, forgeJsonBytes: 0 }], briefs: [], subscriptions: [] },
81 {
82 placement: cellPlacement(parent, memoryBodies(), () => 10),
83 request: new Request("https://forge.test/x", { method: "POST", headers: { origin: "https://forge.test" } }),
84 url: new URL("https://forge.test/x"),
85 },
86 );
87 if (!made.ok) throw new Error(JSON.stringify(made.error));
88 await bootHostedChild(parent, "kid");
89 return made;
90 }
91
92 type Row = { page: string; kind: string; path: string; error: string | null };
93 const ask = async (path: string): Promise<string> => {
94 const res = await fetch(`${servers.url}${path}`, { headers: { host: "bakery--kid--forge.test" }, redirect: "manual" }).catch(() => undefined);
95 return res === undefined ? "no answer" : await res.text();
96 };
97
98 try {
99 const alice = await createUser(parent.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", isAdmin: true });
100 if (!alice.ok) throw new Error("user");
101 const repo = await createRepo(parent, alice.value, { name: "bakery", visibility: "public" });
102 if (!repo.ok) throw new Error("repo");
103 const committed = await commitFiles(repoDir(parent.reposRoot, "alice", "bakery"), {
104 branch: "main",
105 message: "bakery",
106 files: [
107 { path: "forge.json", content: JSON.stringify({ app: { run: "bun run index.ts", health: "/", egress: [ALLOWED] } }) },
108 { path: "index.ts", content: APP },
109 ],
110 author: { name: "a", email: "[email protected]" },
111 });
112 if (!committed.ok) throw new Error("commit");
113 const spawning = spawnAndBoot(alice.value);
114 pending = spawning;
115 // The timer is cleared once the spawn is done: left running it holds the process open for the
116 // rest of SPAWN_MS after the result is printed.
117 let timer: ReturnType<typeof setTimeout> | undefined;
118 const late = new Promise<never>((_, reject) => {
119 timer = setTimeout(() => reject(new Error(`the spawn and boot phase did not finish in ${SPAWN_MS / 1000}s`)), SPAWN_MS);
120 });
121 const made = await Promise.race([spawning, late]).finally(() => clearTimeout(timer));
122 const dbFile = join(childDirOf(parent, made.value.bodyId), "forge.db");
123 const until = Date.now() + LOOKS_MS;
124 let pictures: Row[] | undefined;
125 // The child's looks job writes a row per page and kind: a path when it got a picture, an error
126 // line when it did not, and a failed look is tried again after a backoff. Wait for a picture of
127 // `/`, not for the first row; on the budget, report the rows seen, errors included.
128 while (Date.now() < until) {
129 if (existsSync(dbFile)) {
130 const db = new Database(dbFile, { readonly: true });
131 try {
132 pictures = db.query<Row, []>("SELECT page, kind, path, error FROM look").all();
133 } catch {
134 pictures = undefined;
135 } finally {
136 db.close();
137 }
138 if (pictures?.some((l) => l.page === "/" && l.path !== "" && l.error === null) === true) break;
139 }
140 await Bun.sleep(1000);
141 }
142 const allowed = await ask(`/reach?host=${ALLOWED}`);
143 const outside = await ask(`/reach?host=${OUTSIDE}`);
144 console.log(JSON.stringify({ placement: made.value.placement, pictures: pictures ?? null, allowed, outside }));
145 } finally {
146 await cleanup();
147 parent.close();
148 rmSync(dataDir, { recursive: true, force: true });
149 }
test/spawn-cell-looks.test.ts added +59
@@ -0,0 +1,59 @@
1 /**
2 * A child forge in a real cell takes a picture of its own app (#10620). Its
3 * looks open the app through the child's own listener on 127.0.0.1, so the
4 * row for `/` carries a picture, never a `no picture` line such as
5 * `net::ERR_INTERNET_DISCONNECTED`. The cell gains no network by it: from
6 * inside the cell, the app still cannot reach a host outside the cell's
7 * reach list.
8 *
9 * Skipped where bwrap and socat are missing, and in `FORGE_TEST_SOCKET` mode
10 * (no unix sockets, no cells); the forge's check runs it. The run itself is
11 * `helpers/cell-looks-e2e.ts`.
12 */
13 import { test } from "bun:test";
14 import { join } from "node:path";
15 import { portInsteadOfSocket } from "../src/git/hook-address.ts";
16 import { missing } from "./helpers/tools.ts";
17 import { failedWith } from "./helpers/machine-note.ts";
18 import { resultLine } from "./helpers/result-line.ts";
19
20 const cellTools = process.platform === "linux" && !portInsteadOfSocket() && !missing("bwrap", "socat");
21
22 test.skipIf(!cellTools)("a child cell's looks of its own app produce a picture, and the cell gains no reach", async () => {
23 const env: Record<string, string> = {};
24 for (const [k, v] of Object.entries(process.env)) if (v !== undefined && k !== "FORGE_CELLS") env[k] = v;
25 const proc = Bun.spawn([process.execPath, join(import.meta.dir, "helpers", "cell-looks-e2e.ts")], { env, stdout: "pipe", stderr: "pipe", timeout: 400_000 });
26 const [out, errText] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]);
27 let seen: Record<string, unknown>;
28 try {
29 seen = JSON.parse(resultLine(out)) as Record<string, unknown>;
30 } catch {
31 throw new Error(failedWith("the child looks run printed no result", out.slice(-800), errText));
32 }
33 if (typeof seen["skipped"] === "string") {
34 console.log(`SKIPPING: this host refuses a cell inside the test (${seen["skipped"]})`);
35 return;
36 }
37 // The child's own lines reach the parent's log, so stderr's tail is mostly boot chatter: keep
38 // what the looks said first, so a run with no picture names why.
39 const said = errText.split("\n").filter((l) => /look|chrom|child forge/i.test(l));
40 const evidence = `${errText.slice(-400)}\n-- the looks lines:\n${said.slice(-8).map((l) => l.slice(0, 160)).join("\n")}`;
41 const pictures = (seen["pictures"] ?? []) as { page: string; kind: string; path: string; error: string | null }[];
42 const home = pictures.filter((l) => l.page === "/");
43 if (seen["placement"] !== "cell" || home.length === 0 || home.some((l) => l.path === "" || l.error !== null))
44 throw new Error(failedWith("the child's looks of `/` gave no picture", { placement: seen["placement"], pictures }, evidence));
45 // The control first: the declared host must get past the proxy's allow-list, or a cell with no
46 // way to the app's proxy would read as refusing everything. Any answer from the host does; so
47 // does a refusal that is not the allow-list's (a check machine with no outside network cannot
48 // resolve the name, and the proxy says so).
49 const notDeclared = "is not in this repository's egress";
50 const allowed = seen["allowed"];
51 const passedList =
52 typeof allowed === "string" &&
53 ((allowed.startsWith("answered:") && allowed !== "answered:403") || (allowed.startsWith("refused:") && !allowed.includes(notDeclared)));
54 if (!passedList)
55 throw new Error(failedWith("the app could not reach a host its forge.json declares, so a refusal proves nothing", { allowed }, evidence));
56 const outside = seen["outside"];
57 if (typeof outside !== "string" || !outside.startsWith("refused:"))
58 throw new Error(failedWith("a host outside the cell's reach was not refused by the egress proxy's allow-list", { outside }, evidence));
59 }, 420_000);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs