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

The daily proof reads the forge's own origin through its own handler, so the forge's egress gate never stands between it and itself #15942

Merged opened this

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

Merged

scout/forge:scout/15936-the-daily-proof-reads-the-forge-s-own-or → main 0 commits fork

  • Merged. scout/15936-the-daily-proof-reads-the-forge-s-own-or was merged into main as 4be23f7.

Review

Sign in to review this change.
3 files changed +108 −3
src/maintain/duties/daily_proof.ts +19 −3
@@ -2,15 +2,18 @@
22 * The daily proof (#15625): once a day, the v1 checks against the forge's own public origin
33 * (`FORGE_URL`), read-only, no token, 60 s for the whole run; see `../../release/daily_proof.ts`.
44 * A line that asks the owner is a finding too, so the run's row says what the ask says.
5 * With no fetcher given, the forge's own origin is read through its own router in process
6 * (`../../release/own_fetch.ts`, #15936): the egress gate refuses the forge's own host.
57 */
68
79 import { ownRepoPath } from "../../kernel/own_repo.ts";
810 import { publicOrigin } from "../../core/public_origin.ts";
911 import { BUDGET_MS, proveDaily, recordRun } from "../../release/daily_proof.ts";
10 import type { Fetcher } from "../../release/v1_checks.ts";
12 import { IN_PROCESS, ownFetcher } from "../../release/own_fetch.ts";
13 import type { Fetcher, Line } from "../../release/v1_checks.ts";
1114 import type { Duty, Finding } from "../duty.ts";
1215
13 export function dailyProof(fetcher: Fetcher = fetch, budget: () => AbortSignal = () => AbortSignal.timeout(BUDGET_MS)): Duty {
16 export function dailyProof(fetcher?: Fetcher, budget: () => AbortSignal = () => AbortSignal.timeout(BUDGET_MS)): Duty {
1417 return {
1518 name: "daily-proof",
1619 intervalMs: 86_400_000,
@@ -20,7 +23,20 @@ export function dailyProof(fetcher: Fetcher = fetch, budget: () => AbortSignal =
2023 async run({ db, now, app }) {
2124 const url = app?.config.url;
2225 if (url === undefined || url === "") return { findings: [], actions: ["skipped: this forge does not know its public origin (FORGE_URL)"] };
23 const lines = await proveDaily(publicOrigin(url, ""), app?.config.forgeRepo || ownRepoPath(), fetcher, now.getTime(), budget());
26 const base = publicOrigin(url, "");
27 const repo = app?.config.forgeRepo || ownRepoPath();
28 let lines: Line[];
29 if (fetcher !== undefined || app === undefined) lines = await proveDaily(base, repo, fetcher ?? fetch, now.getTime(), budget());
30 else {
31 const own = await ownFetcher(app, base, fetch);
32 try {
33 lines = (await proveDaily(base, repo, own.fetch, now.getTime(), budget())).map((l) =>
34 l.item === "healthz fast" && / ms\b/.test(l.evidence) ? { ...l, evidence: `${l.evidence}, ${IN_PROCESS}` } : l,
35 );
36 } finally {
37 own.close();
38 }
39 }
2440 const done = recordRun(db, lines, now);
2541 const findings: Finding[] = done.raised.map((l) => ({
2642 summary: `daily proof: ${l.item} is ${l.verdict}`,
src/release/own_fetch.ts added +42
@@ -0,0 +1,42 @@
1 /**
2 * The daily proof's fetcher (#15936): a read of the forge's own public origin is answered by its
3 * own router in process, never through the egress gate, which refuses the forge's own host as it
4 * would any other undeclared one; every other origin goes to `outside` (the global `fetch` in the
5 * duty).
6 *
7 * In process means: the visitor's routes and the security floor every response leaves with, with
8 * none of the caller's credentials. A cookie or authorization header is dropped, so the proof reads
9 * what a stranger reads and never the owner's session. Only GET and the MCP POST the probe makes
10 * are answered here; anything else is refused before it reaches the router.
11 */
12
13 import type { App } from "../app.ts";
14 import { securityFloor } from "../http/respond.ts";
15 import type { Fetcher } from "./v1_checks.ts";
16
17 /** What every in-process line's timing says: the network a visitor crosses is not in it. */
18 export const IN_PROCESS = "measured in process, faster than a visitor's";
19
20 export interface OwnFetcher {
21 readonly fetch: Fetcher;
22 /** Ends the router's own sweeps; call once the run is over. */
23 close(): void;
24 }
25
26 export async function ownFetcher(app: App, origin: string, outside: Fetcher): Promise<OwnFetcher> {
27 const own = new URL(origin).origin;
28 const { forgeHttp } = await import("../http/server.ts");
29 const http = forgeHttp(app);
30 const answer: Fetcher = async (url, init) => {
31 const target = new URL(url);
32 if (target.origin !== own) return outside(url, init);
33 const method = init?.method ?? "GET";
34 if (method !== "GET" && !(method === "POST" && target.pathname === "/mcp")) throw new Error(`${method} ${target.pathname} is not a read`);
35 const headers = new Headers(init?.headers);
36 headers.delete("cookie");
37 headers.delete("authorization");
38 const request = new Request(target.href, { method, headers, ...(init?.body == null ? {} : { body: init.body }), ...(init?.signal ? { signal: init.signal } : {}) });
39 return securityFloor(await http.handle(request));
40 };
41 return { fetch: answer, close: () => http.close() };
42 }
test/daily-proof.test.ts +47
@@ -16,6 +16,7 @@ import { serve, type Servers } from "../src/http/server.ts";
1616 import { dailyProof } from "../src/maintain/duties/daily_proof.ts";
1717 import type { DutyResult } from "../src/maintain/duty.ts";
1818 import { lastRun, openAsks, plainEvidence, proofAlarm, recordRun, tallyWords } from "../src/release/daily_proof.ts";
19 import { IN_PROCESS, ownFetcher } from "../src/release/own_fetch.ts";
1920 import { OUT_OF_BUDGET, type Line } from "../src/release/v1_checks.ts";
2021 import { Session } from "../src/tui/forge.ts";
2122 import { statusLines } from "../src/tui/screens/status.ts";
@@ -197,6 +198,52 @@ describe("the daily proof", () => {
197198 });
198199 });
199200
201 describe("the forge's own origin, read in process (#15936)", () => {
202 const GATE = "refused by this forge: forge.example:443 is not in this app's forge.json egress";
203
204 test("with the gate refusing every outside fetch, the duty still reads real lines from its own router", async () => {
205 const own = createApp({ dataDir: join(dir, "own"), port: 0, url: `${BASE}/`, forgeRepo: "o/forge" });
206 const real = globalThis.fetch;
207 const outside: string[] = [];
208 globalThis.fetch = (async (input: string | URL | Request) => {
209 outside.push(String(input instanceof Request ? input.url : input));
210 throw new Error(GATE);
211 }) as unknown as typeof fetch;
212 try {
213 await dailyProof().run({ db: own.db, bus: own.bus, now: new Date(), app: own });
214 } finally {
215 globalThis.fetch = real;
216 }
217 const lines = lastRun(own.db)!.lines;
218 expect(outside).toEqual([]);
219 expect(lines.length).toBeGreaterThanOrEqual(8);
220 expect(lines.filter((l) => /refused by this forge|no answer/.test(l.evidence))).toEqual([]);
221 const by = Object.fromEntries(lines.map((l) => [l.item, l]));
222 expect(by["security headers on /"]?.verdict).toBe("PASS");
223 expect(by["sign-up page"]?.verdict).not.toBe("UNKNOWN");
224 expect(by["healthz fast"]?.evidence).toContain(IN_PROCESS);
225 own.close();
226 });
227
228 test("its fetcher: another origin goes out, a write is refused, and no credential reaches the router", async () => {
229 const own = createApp({ dataDir: join(dir, "own-fetch"), port: 0, url: `${BASE}/` });
230 const out: string[] = [];
231 const f = await ownFetcher(own, BASE, async (u) => (out.push(u), new Response("elsewhere")));
232 try {
233 expect(await (await f.fetch("https://other.example/x")).text()).toBe("elsewhere");
234 expect(out).toEqual(["https://other.example/x"]);
235 await expect(f.fetch(`${BASE}/signup`, { method: "POST", body: "a=1" })).rejects.toThrow("is not a read");
236 const here = await f.fetch(`${BASE}/api/v1/here`, { headers: { authorization: "Bearer forge_x", cookie: "forge_session=x" } });
237 expect(here.headers.get("x-content-type-options")).toBe("nosniff");
238 expect(here.status).toBe(200);
239 expect(JSON.stringify(await here.json())).toContain("anonymous");
240 } finally {
241 f.close();
242 own.close();
243 }
244 });
245 });
246
200247 describe("recording a run", () => {
201248 const line = (item: string, verdict: Line["verdict"], evidence = "seen"): Line => ({ item, verdict, url: `${BASE}/x`, evidence });
202249

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs