Public: anyone can read this; the crew can change it.
The forge running as an app never calls its own public host: the egress gate refused it 13 times #14668
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14664-the-forge-running-as-an-app-never-calls → main 6 commits fork 2 threads · 2 unresolved
10 files
changed +310
−15
AGENTS.md +1 −1
Diff not shown — a line 3,799 characters wide (limit 2,000). +1 −1
View fileforge.json +1 −2
| @@ -1,8 +1,7 @@ | ||
| 1 | 1 | { |
| 2 | 2 | "egress": [ |
| 3 | 3 | "api.anthropic.com", |
| 4 | "api.github.com", | |
| 5 | "lab--forge-trevato-apps.open-platform.sh" | |
| 4 | "api.github.com" | |
| 6 | 5 | ], |
| 7 | 6 | "app": { |
| 8 | 7 | "looks": ["/", "/docs", "/explore", "/board", "/notes", "/review", "/releases", "/apps", "/security", "/system", "/admin/limits", "/maintenance", "/talk", "/flow", "/resilience", "/boards?help", "/settings/crew", "/settings/crew?help", "/settings/security/controls", "/settings/grants", "/preview-admin/sample", "/preview-admin/sample/drop", "/preview-admin/sample/data-move", "/preview-admin/sample/cut-over", "/preview-admin/sample/pulls/3", "/preview-admin/sample/next?pick", "/preview-admin/sample/bets", "/preview-admin/sample/waves", "/preview-admin/sample/space", "/looks/missing", "/looks/failed", "/nope/nothing"], |
src/kernel/outbound.ts +30
| @@ -17,6 +17,7 @@ import { isIP } from "node:net"; | ||
| 17 | 17 | import { err, ok, type Result } from "./result.ts"; |
| 18 | 18 | import { addressBytes, inCidr, inCidrs, parseCidr, type Cidr } from "./cidr.ts"; |
| 19 | 19 | import { cellProxy, UNRESOLVED_WORDS } from "../apps/egress.ts"; |
| 20 | import { localFor } from "./own_host.ts"; | |
| 20 | 21 | |
| 21 | 22 | const REFUSED = [ |
| 22 | 23 | "0.0.0.0/8", |
| @@ -81,6 +82,12 @@ export interface OutboundOptions { | ||
| 81 | 82 | * runs in (`cellProxy`), and to none outside one. Injected by tests. |
| 82 | 83 | */ |
| 83 | 84 | proxy?: string | undefined; |
| 85 | /** | |
| 86 | * The caller is the forge's own code asking a URL that may be on this forge's own public host | |
| 87 | * (#14664): such a URL is answered by the local listener, as a stranger would be. Without it, | |
| 88 | * the own host is refused: a webhook or notify target a person set never reaches the listener. | |
| 89 | */ | |
| 90 | self?: boolean; | |
| 84 | 91 | } |
| 85 | 92 | |
| 86 | 93 | /** How a vetted host is reached: at the address that was judged, or by name through the cell's gate. */ |
| @@ -101,6 +108,22 @@ const systemResolve: Resolve = async (host) => | ||
| 101 | 108 | export const REFUSED_SENTENCE = "refused: private address"; |
| 102 | 109 | export const PLAIN_SENTENCE = "refused: not https"; |
| 103 | 110 | export const UNRESOLVED_SENTENCE = "unresolved host"; |
| 111 | export const OWN_HOST_SENTENCE = "refused: this forge's own host"; | |
| 112 | ||
| 113 | /** | |
| 114 | * Headers for a call to the own listener that earns it nothing a remote caller lacks: no cookie or | |
| 115 | * authorization, no client-naming header of the caller's, and `x-forwarded-for` set so the | |
| 116 | * listener does not read it as the forge asking itself (`http/peer.ts` `isInternalPeer`). | |
| 117 | */ | |
| 118 | export function asStranger(given: RequestInit["headers"], target: URL): Headers { | |
| 119 | const headers = new Headers(given); | |
| 120 | for (const name of ["cookie", "authorization", "proxy-authorization", "x-forwarded-for", "forwarded", "x-real-ip", "cf-connecting-ip", "true-client-ip"]) | |
| 121 | headers.delete(name); | |
| 122 | headers.set("host", target.host); | |
| 123 | headers.set("x-forwarded-proto", target.protocol.slice(0, -1)); | |
| 124 | headers.set("x-forwarded-for", "127.0.0.1"); | |
| 125 | return headers; | |
| 126 | } | |
| 104 | 127 | |
| 105 | 128 | export class RefusedError extends Error { |
| 106 | 129 | constructor(sentence = REFUSED_SENTENCE) { |
| @@ -174,6 +197,13 @@ export async function outboundFetch( | ||
| 174 | 197 | options: OutboundOptions = {}, |
| 175 | 198 | ): Promise<Response> { |
| 176 | 199 | const target = new URL(url); |
| 200 | // The forge's own public host never goes over the network (#14664): the forge's own code is | |
| 201 | // answered by its listener, anyone else's URL is refused here. | |
| 202 | const self = localFor(target); | |
| 203 | if (self !== undefined) { | |
| 204 | if (options.self !== true) throw new RefusedError(OWN_HOST_SENTENCE); | |
| 205 | return fetch(self, { ...init, headers: asStranger(init.headers, target), redirect: "manual" }); | |
| 206 | } | |
| 177 | 207 | if (target.protocol !== "https:" && !options.plain) |
| 178 | 208 | throw new RefusedError(PLAIN_SENTENCE); |
| 179 | 209 | const vetted = await vetHost(target.hostname, options); |
src/kernel/own_host.ts added +58
| @@ -0,0 +1,58 @@ | ||
| 1 | /** | |
| 2 | * The forge's own public host, and the local listener that answers for it (#14664). A call to | |
| 3 | * yourself should not leave the process's network: from a cell it is a hairpin through the | |
| 4 | * uplink, and the egress gate refuses it unless forge.json declares the forge's own name. | |
| 5 | * `localFor` turns a URL on the public host into the same path on the local listener; | |
| 6 | * `outboundFetch` (`outbound.ts`), the one helper for what the forge fetches, asks it first: the | |
| 7 | * forge's own code (`self`) is answered there as a stranger, every other caller is refused. | |
| 8 | * | |
| 9 | * Process memory: one pair, set once by `main.ts` when the listener is up (`setOwnHost`), read by | |
| 10 | * every outbound fetch; gone on restart. `forgetOwnHost` is for tests. | |
| 11 | */ | |
| 12 | ||
| 13 | let own: { readonly host: string; readonly local: URL } | undefined; | |
| 14 | ||
| 15 | /** Set the public origin (`FORGE_APP_URL` hosted, `FORGE_URL` otherwise) and the listener's URL. */ | |
| 16 | export function setOwnHost( | |
| 17 | publicUrl: string | undefined, | |
| 18 | localUrl: string, | |
| 19 | say: (line: string) => void = (line) => console.error(line), | |
| 20 | ): void { | |
| 21 | own = undefined; | |
| 22 | if (publicUrl === undefined || publicUrl.trim() === "") return; | |
| 23 | if (!URL.canParse(publicUrl) || !URL.canParse(localUrl)) { | |
| 24 | // Said once, at boot: the guard stays off, and nobody would know why without this line. | |
| 25 | say(`own host: not set, the public URL or listener is not a URL (${JSON.stringify(publicUrl.slice(0, 200))}); a fetch of this forge's own host is not kept off the network (#14664)`); | |
| 26 | return; | |
| 27 | } | |
| 28 | own = { host: new URL(publicUrl).host.toLowerCase(), local: new URL(localUrl) }; | |
| 29 | } | |
| 30 | ||
| 31 | export function forgetOwnHost(): void { | |
| 32 | own = undefined; | |
| 33 | } | |
| 34 | ||
| 35 | /** The public host (`name[:port]`), or undefined when this forge was not told its public URL. */ | |
| 36 | export function ownHost(): string | undefined { | |
| 37 | return own?.host; | |
| 38 | } | |
| 39 | ||
| 40 | /** Whether `url` names the forge's own public host. */ | |
| 41 | export function isOwnHost(url: string | URL): boolean { | |
| 42 | if (own === undefined) return false; | |
| 43 | try { | |
| 44 | return new URL(url).host.toLowerCase() === own.host; | |
| 45 | } catch { | |
| 46 | return false; | |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 | /** The same path and query on the local listener, or undefined when `url` is not the own host. */ | |
| 51 | export function localFor(url: string | URL): URL | undefined { | |
| 52 | if (own === undefined || !isOwnHost(url)) return undefined; | |
| 53 | const from = new URL(url); | |
| 54 | const to = new URL(own.local.href); | |
| 55 | to.pathname = from.pathname; | |
| 56 | to.search = from.search; | |
| 57 | return to; | |
| 58 | } |
src/main.ts +3
| @@ -152,6 +152,7 @@ import { timeStatements } from "./kernel/slow_sql.ts"; | ||
| 152 | 152 | import { waitForPeer } from "./runs/peer.ts"; |
| 153 | 153 | import { noteWritePause } from "./keeper/pause.ts"; |
| 154 | 154 | import { closeStaleSessions } from "./machines/shell.ts"; |
| 155 | import { setOwnHost } from "./kernel/own_host.ts"; | |
| 155 | 156 | |
| 156 | 157 | // `forge hook <name>` re-enters here from a git hook stub. It relays to the |
| 157 | 158 | // running forge over its socket and exits — it must never boot a server. |
| @@ -462,6 +463,8 @@ await warmFlow(app); | ||
| 462 | 463 | await warmPlans(app); |
| 463 | 464 | await warmDisplays(app).catch(() => undefined); |
| 464 | 465 | const servers = await serve(app, { warm: {} }); |
| 466 | // A fetch of this forge's own public host is answered by this listener, not the network (#14664). | |
| 467 | setOwnHost(canonicalUrl, servers.url); | |
| 465 | 468 | markReady(); |
| 466 | 469 | // From here the flow's forge-wide readings are read once and shared for half a minute |
| 467 | 470 | // (`flow/kept.ts`). |
src/maintain/duties/wait_split.ts +21 −9
| @@ -8,6 +8,8 @@ | ||
| 8 | 8 | * issue per layer (#13636), naming the setting that bounds that layer's time with its reason (or |
| 9 | 9 | * that none is held, and what to check) (`../wait_limits.ts`), so a repeat is loud, not a line |
| 10 | 10 | * nobody reads. |
| 11 | * A forge in a cell (hosted as an app) reads loopback alone: calling its own public host from the | |
| 12 | * cell is a hairpin its gate refuses (#14664). | |
| 11 | 13 | */ |
| 12 | 14 | |
| 13 | 15 | import { connect as tcpConnect } from "node:net"; |
| @@ -15,6 +17,7 @@ import { connect as tlsConnect } from "node:tls"; | ||
| 15 | 17 | import { fileIssueOnce } from "../../checks/flake.ts"; |
| 16 | 18 | import { getRepo } from "../../core/repos.ts"; |
| 17 | 19 | import { ownRepoPath } from "../../kernel/own_repo.ts"; |
| 20 | import { cellProxy } from "../../apps/egress.ts"; | |
| 18 | 21 | import type { Duty, Finding } from "../duty.ts"; |
| 19 | 22 | import { alarmBody, alarmTitle } from "../wait_limits.ts"; |
| 20 | 23 | import { serverAppMs, splitLine, waitLayer, type Layer, type Timings } from "../wait_split.ts"; |
| @@ -80,7 +83,10 @@ export const networkMeter: Meter = { handshake, get }; | ||
| 80 | 83 | |
| 81 | 84 | const why = (e: unknown): string => (e instanceof Error ? `${e.name}: ${e.message}` : String(e)); |
| 82 | 85 | |
| 83 | export function waitSplit(meter: Meter = networkMeter, runMs: number = RUN_MS): Duty { | |
| 86 | /** Whether this forge runs in a cell (hosted as an app): its way out is its parent's gate. */ | |
| 87 | const inACell = (): boolean => cellProxy() !== undefined; | |
| 88 | ||
| 89 | export function waitSplit(meter: Meter = networkMeter, runMs: number = RUN_MS, inCell: () => boolean = inACell): Duty { | |
| 84 | 90 | return { |
| 85 | 91 | name: "wait-split", |
| 86 | 92 | intervalMs: 6 * 3_600_000, |
| @@ -94,6 +100,10 @@ export function waitSplit(meter: Meter = networkMeter, runMs: number = RUN_MS): | ||
| 94 | 100 | const actions: string[] = []; |
| 95 | 101 | const slow = new Map<Exclude<Layer, "none">, string[]>(); |
| 96 | 102 | const run = AbortSignal.timeout(runMs); |
| 103 | // A forge in a cell does not call its own public host: that is a hairpin through the | |
| 104 | // uplink its gate refuses (#14664). It times loopback alone, and says the edge went unread. | |
| 105 | const local = inCell(); | |
| 106 | if (local) actions.push(`public side not read: this forge runs in a cell and does not call its own host ${origin.host} (#14664)`); | |
| 97 | 107 | for (const [i, path] of PATHS.entries()) { |
| 98 | 108 | if (run.aborted) { |
| 99 | 109 | actions.push(`not read within this run's ${runMs} ms: ${PATHS.slice(i).join(", ")}`); |
| @@ -102,14 +112,16 @@ export function waitSplit(meter: Meter = networkMeter, runMs: number = RUN_MS): | ||
| 102 | 112 | try { |
| 103 | 113 | // A cell reaches out only through the egress proxy, where a bare handshake cannot go: |
| 104 | 114 | // the GETs are still timed, and the line says the edge went unmeasured. |
| 105 | const connectMs = await meter.handshake(origin, run).catch((e: unknown) => { | |
| 106 | actions.push(`${path} handshake unmeasured, counted as 0 ms: ${why(e)}`); | |
| 107 | return 0; | |
| 108 | }); | |
| 109 | const pub = await meter.get(new URL(path, origin).href, run); | |
| 110 | const local = await meter.get(`${loopback}${path}`, run); | |
| 111 | const t: Timings = { connectMs, ttfbMs: pub.ttfbMs, loopbackMs: local.ttfbMs, appMs: serverAppMs(pub.serverTiming) ?? serverAppMs(local.serverTiming) ?? 0 }; | |
| 112 | const said = splitLine(path, t, pub.totalMs); | |
| 115 | const connectMs = local | |
| 116 | ? 0 | |
| 117 | : await meter.handshake(origin, run).catch((e: unknown) => { | |
| 118 | actions.push(`${path} handshake unmeasured, counted as 0 ms: ${why(e)}`); | |
| 119 | return 0; | |
| 120 | }); | |
| 121 | const pub = local ? undefined : await meter.get(new URL(path, origin).href, run); | |
| 122 | const near = await meter.get(`${loopback}${path}`, run); | |
| 123 | const t: Timings = { connectMs, ttfbMs: (pub ?? near).ttfbMs, loopbackMs: near.ttfbMs, appMs: serverAppMs(pub?.serverTiming) ?? serverAppMs(near.serverTiming) ?? 0 }; | |
| 124 | const said = splitLine(path, t, (pub ?? near).totalMs); | |
| 113 | 125 | actions.push(said); |
| 114 | 126 | const layer = waitLayer(t); |
| 115 | 127 | if (layer !== "none") slow.set(layer, [...(slow.get(layer) ?? []), said]); |
src/origins/lineage.ts +2 −1
| @@ -59,7 +59,8 @@ export async function lookupLineage( | ||
| 59 | 59 | headers: { accept: "application/json", "user-agent": "forge-migrate" }, |
| 60 | 60 | signal: AbortSignal.timeout(TIMEOUT_MS), |
| 61 | 61 | }, |
| 62 | options, | |
| 62 | // An address on this forge's own host is read from its listener, anonymously (#14664). | |
| 63 | { ...options, self: true }, | |
| 63 | 64 | ); |
| 64 | 65 | if (response.status !== 200) { |
| 65 | 66 | await response.body?.cancel().catch(() => undefined); |
test/maintain-wait-split.test.ts +16 −1
| @@ -55,7 +55,7 @@ const appAt = (url: string, port = 3000): App => { | ||
| 55 | 55 | made.push({ app, dir }); |
| 56 | 56 | return app; |
| 57 | 57 | }; |
| 58 | const runOn = (meter: Meter, app: App, now = new Date(), runMs?: number) => waitSplit(meter, runMs).run({ db: app.db, bus: app.bus, now, app }); | |
| 58 | const runOn = (meter: Meter, app: App, now = new Date(), runMs?: number) => waitSplit(meter, runMs, () => false).run({ db: app.db, bus: app.bus, now, app }); | |
| 59 | 59 | afterAll(() => { |
| 60 | 60 | for (const { app, dir } of made) { |
| 61 | 61 | app.close(); |
| @@ -120,3 +120,18 @@ test("the run's bound sits under the runner's deadline", () => { | ||
| 120 | 120 | test("the forge runs it", () => { |
| 121 | 121 | expect(duties.map((d) => d.name)).toContain("wait-split"); |
| 122 | 122 | }); |
| 123 | ||
| 124 | test("in a cell the duty never asks its own public host: loopback alone, and the line says so (#14664)", async () => { | |
| 125 | const asked: string[] = []; | |
| 126 | let shaken = 0; | |
| 127 | const meter: Meter = { | |
| 128 | handshake: async () => (shaken++, 5), | |
| 129 | get: async (url) => (asked.push(url), read(3, 1)), | |
| 130 | }; | |
| 131 | const app = appAt("https://lab--forge.example.test", 4123); | |
| 132 | const out = await waitSplit(meter, undefined, () => true).run({ db: app.db, bus: app.bus, now: new Date(), app }); | |
| 133 | expect(shaken).toBe(0); | |
| 134 | expect(asked.every((u) => new URL(u).host === "127.0.0.1:4123")).toBe(true); | |
| 135 | expect(asked).toHaveLength(PATHS.length); | |
| 136 | expect(out.actions[0]).toContain("does not call its own host lab--forge.example.test"); | |
| 137 | }); |
test/outbound-sweep.test.ts +39 −1
| @@ -22,7 +22,7 @@ const BROWSER = "browser script served by the forge: it fetches its own origin's | ||
| 22 | 22 | |
| 23 | 23 | /** File → how many non-literal fetches it holds, and why none of them takes a caller-given host. */ |
| 24 | 24 | const ALLOWED: Record<string, { count: number; why: string }> = { |
| 25 | "src/kernel/outbound.ts": { count: 2, why: "the guard itself: dials the address it judged, or the cell's gate" }, | |
| 25 | "src/kernel/outbound.ts": { count: 3, why: "the guard itself: dials the address it judged, the cell's gate, or the forge's own local listener for its own public host" }, | |
| 26 | 26 | "src/origins/origin_fetch.ts": { count: 1, why: "operator-allowed hosts (FORGE_ORIGIN_ALLOW) and dev loopback only; every other through the guard" }, |
| 27 | 27 | "src/origins/discover.ts": { count: 1, why: "the exempt branch of discovery; every other probe is outboundFetch" }, |
| 28 | 28 | "src/git/hook-address.ts": { count: 1, why: "the forge's own internal listener: a unix socket or 127.0.0.1" }, |
| @@ -56,6 +56,31 @@ const ALLOWED: Record<string, { count: number; why: string }> = { | ||
| 56 | 56 | "src/maintain/duties/wait_split.ts": { count: 1, why: "the forge's own public URL, timed" }, |
| 57 | 57 | }; |
| 58 | 58 | |
| 59 | /** | |
| 60 | * A name that holds this forge's own public URL (`FORGE_URL`, `FORGE_APP_URL`, its config's `url`). | |
| 61 | */ | |
| 62 | const OWN_URL = /canonicalUrl|FORGE_APP_URL|FORGE_URL|forgeUrl|config\.url|ownHost|publicUrl/; | |
| 63 | ||
| 64 | /** | |
| 65 | * A file with a bare fetch that also names the forge's own public URL could fetch its own host | |
| 66 | * past the guard (#14664). Each says what its fetch reaches now; a new one fails the sweep until | |
| 67 | * it goes through `outboundFetch` (refused, or answered by the listener with `self`) or says why. | |
| 68 | */ | |
| 69 | const OWN_HOST: Record<string, string> = { | |
| 70 | "src/maintain/duties/wait_split.ts": "in a cell it reads loopback alone (#14664); outside one it times its own public URL on purpose, the edge being what it measures", | |
| 71 | "src/http/server.ts": "the forge's own unix socket; the public URL only names links", | |
| 72 | "src/apps/proxy.ts": "an app's socket or loopback port; the forge URL only builds links for the app", | |
| 73 | "src/apps/toolbar.ts": BROWSER, | |
| 74 | "src/http/rp.ts": "the parent forge's issuer; the forge URL only sets the cookie's secure flag", | |
| 75 | "src/actions/upstream.ts": "the parent forge's API; the forge URL is never the target", | |
| 76 | "src/crew/openai_runner.ts": "the model endpoint; FORGE_URL is only handed to the run's environment", | |
| 77 | "src/release/small_forge.ts": "a forge the release check started on loopback", | |
| 78 | "src/runner/main.ts": "a runner on its own machine, calling the forge it was enrolled with", | |
| 79 | "src/chaos/report.ts": "a command an operator runs, not the forge's process: it reports to the FORGE_URL it is given", | |
| 80 | "src/snapshot/probe.ts": "a command an operator runs, not the forge's process", | |
| 81 | "src/snapshot/verdict.ts": "a command an operator runs, not the forge's process", | |
| 82 | }; | |
| 83 | ||
| 59 | 84 | /** Every non-literal fetch in src, as `file:line`. */ |
| 60 | 85 | async function sweep(): Promise<Map<string, string[]>> { |
| 61 | 86 | const found = new Map<string, string[]>(); |
| @@ -87,6 +112,19 @@ describe("the sweep of src", () => { | ||
| 87 | 112 | for (const [file, { why }] of Object.entries(ALLOWED)) expect([file, why.length > 10]).toEqual([file, true]); |
| 88 | 113 | }); |
| 89 | 114 | |
| 115 | test("no bare fetch can reach the forge's own public host unnamed (#14664)", async () => { | |
| 116 | const unnamed: string[] = []; | |
| 117 | for (const [file] of await sweep()) { | |
| 118 | if (file === "src/kernel/outbound.ts" || file.startsWith("src/web/")) continue; | |
| 119 | if (OWN_URL.test(await Bun.file(file).text()) && OWN_HOST[file] === undefined) unnamed.push(file); | |
| 120 | } | |
| 121 | expect(unnamed).toEqual([]); | |
| 122 | for (const [file, why] of Object.entries(OWN_HOST)) { | |
| 123 | expect([file, why.length > 10]).toEqual([file, true]); | |
| 124 | expect([file, OWN_URL.test(await Bun.file(file).text())]).toEqual([file, true]); | |
| 125 | } | |
| 126 | }); | |
| 127 | ||
| 90 | 128 | test("the origin paths a member names go through the guard, not a bare fetch", async () => { |
| 91 | 129 | const found = await sweep(); |
| 92 | 130 | expect(found.get("src/origins/oauth.ts")).toBeUndefined(); |
test/own-host.test.ts added +139
| @@ -0,0 +1,139 @@ | ||
| 1 | /** | |
| 2 | * The forge never fetches its own public host over the network (#14664): `outboundFetch`, the one | |
| 3 | * helper for what it fetches, hands such a URL to the local listener when the forge's own code | |
| 4 | * asks (`self`), as a stranger, and refuses it for every other caller (webhooks, notify, the | |
| 5 | * parity probe). `fetch` is watched while each path runs, so a call that reached the public host | |
| 6 | * fails here; the network is never asked. | |
| 7 | */ | |
| 8 | import { afterAll, afterEach, expect, test } from "bun:test"; | |
| 9 | import { OWN_HOST_SENTENCE, RefusedError, outboundFetch } from "../src/kernel/outbound.ts"; | |
| 10 | import { isInternalPeer, resolveClient } from "../src/http/peer.ts"; | |
| 11 | import { post } from "../src/crew/notify.ts"; | |
| 12 | import { statusOf } from "../src/apps/parity_probe.ts"; | |
| 13 | import { lookupLineage } from "../src/origins/lineage.ts"; | |
| 14 | import { forgetOwnHost, isOwnHost, localFor, ownHost, setOwnHost } from "../src/kernel/own_host.ts"; | |
| 15 | ||
| 16 | const PUBLIC = "https://lab--forge.example.test"; | |
| 17 | ||
| 18 | const server = Bun.serve({ | |
| 19 | hostname: "127.0.0.1", | |
| 20 | port: 0, | |
| 21 | fetch: (req, srv) => { | |
| 22 | const peer = srv.requestIP(req)?.address; | |
| 23 | const heard = { | |
| 24 | path: new URL(req.url).pathname, | |
| 25 | search: new URL(req.url).search, | |
| 26 | host: req.headers.get("host"), | |
| 27 | method: req.method, | |
| 28 | cookie: req.headers.get("cookie"), | |
| 29 | authorization: req.headers.get("authorization"), | |
| 30 | // What the forge's listener would make of this caller (`http/peer.ts`, as `notePeer` does). | |
| 31 | internal: isInternalPeer(peer, req.headers, { proxies: [] }), | |
| 32 | client: resolveClient(peer, req.headers, { proxies: [] }), | |
| 33 | }; | |
| 34 | return new URL(req.url).pathname.startsWith("/api/v1/repos/") | |
| 35 | ? Response.json({ full_name: "me/fork", parent: { full_name: "up/root" } }) | |
| 36 | : Response.json(heard); | |
| 37 | }, | |
| 38 | }); | |
| 39 | const local = `http://127.0.0.1:${server.port}`; | |
| 40 | ||
| 41 | afterEach(() => forgetOwnHost()); | |
| 42 | afterAll(() => server.stop(true)); | |
| 43 | ||
| 44 | /** Run `fn` with `fetch` watched; every URL it was asked for comes back. */ | |
| 45 | async function watched<T>(fn: () => Promise<T>, network = true): Promise<{ result: T; asked: string[] }> { | |
| 46 | const real = globalThis.fetch; | |
| 47 | const asked: string[] = []; | |
| 48 | globalThis.fetch = Object.assign( | |
| 49 | (input: string | URL | Request, init?: RequestInit) => { | |
| 50 | const url = input instanceof Request ? input.url : String(input); | |
| 51 | asked.push(url); | |
| 52 | // Only the test's own listener is reachable; anything else is rejected, never dialled. | |
| 53 | if (network && new URL(url).host === `127.0.0.1:${server.port}`) return real(input, init); | |
| 54 | return Promise.reject(new Error(`stubbed: no network for ${url}`)); | |
| 55 | }, | |
| 56 | real, | |
| 57 | ); | |
| 58 | try { | |
| 59 | return { result: await fn(), asked }; | |
| 60 | } finally { | |
| 61 | globalThis.fetch = real; | |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 65 | test("only the configured public host is the own host, and its path keeps to the listener", () => { | |
| 66 | expect(isOwnHost(`${PUBLIC}/x`)).toBe(false); | |
| 67 | setOwnHost(PUBLIC, local); | |
| 68 | expect(ownHost()).toBe("lab--forge.example.test"); | |
| 69 | expect(isOwnHost(`${PUBLIC}/x`)).toBe(true); | |
| 70 | expect(isOwnHost("https://LAB--forge.example.test/")).toBe(true); | |
| 71 | expect(isOwnHost("https://other.example.test/")).toBe(false); | |
| 72 | expect(isOwnHost("https://lab--forge.example.test:8443/")).toBe(false); | |
| 73 | expect(localFor(`${PUBLIC}/api/v1/x?y=1#z`)?.href).toBe(`${local}/api/v1/x?y=1`); | |
| 74 | expect(localFor("https://other.example.test/")).toBeUndefined(); | |
| 75 | }); | |
| 76 | ||
| 77 | test("an unset or broken public URL makes no host the own host, and a broken one is said once", () => { | |
| 78 | const said: string[] = []; | |
| 79 | setOwnHost("", local, (l) => said.push(l)); | |
| 80 | expect(ownHost()).toBeUndefined(); | |
| 81 | expect(said).toEqual([]); | |
| 82 | setOwnHost("not a url", local, (l) => said.push(l)); | |
| 83 | expect(isOwnHost(PUBLIC)).toBe(false); | |
| 84 | expect(said).toHaveLength(1); | |
| 85 | expect(said[0]).toContain("not a url"); | |
| 86 | }); | |
| 87 | ||
| 88 | test("the forge's own code is answered by the local listener, never the network", async () => { | |
| 89 | setOwnHost(PUBLIC, local); | |
| 90 | const { result, asked } = await watched(() => outboundFetch(`${PUBLIC}/hooks/self?a=1`, { method: "POST", body: "{}" }, { self: true })); | |
| 91 | expect(await result.json()).toMatchObject({ path: "/hooks/self", search: "?a=1", host: "lab--forge.example.test", method: "POST" }); | |
| 92 | expect(asked).toEqual([`${local}/hooks/self?a=1`]); | |
| 93 | }); | |
| 94 | ||
| 95 | test("a call to the own listener earns no trust a remote caller lacks: not internal, no credentials", async () => { | |
| 96 | setOwnHost(PUBLIC, local); | |
| 97 | const { result } = await watched(() => | |
| 98 | outboundFetch(`${PUBLIC}/x`, { headers: { cookie: "forge_session=s", authorization: "Bearer t", "x-forwarded-for": "10.0.0.9" } }, { self: true }), | |
| 99 | ); | |
| 100 | const heard = (await result.json()) as Record<string, unknown>; | |
| 101 | expect(heard["internal"]).toBe(false); | |
| 102 | expect(heard["cookie"]).toBeNull(); | |
| 103 | expect(heard["authorization"]).toBeNull(); | |
| 104 | // Not the address the caller wrote: the listener keys its limits on the forge's word. | |
| 105 | expect(heard["client"]).toBe("127.0.0.1"); | |
| 106 | }); | |
| 107 | ||
| 108 | test("every caller that did not opt in is refused the own host before any fetch: webhooks, notify, the parity probe", async () => { | |
| 109 | setOwnHost(PUBLIC, local); | |
| 110 | const { result, asked } = await watched(async () => { | |
| 111 | const helper = await outboundFetch(`${PUBLIC}/hooks/x`, { method: "POST", headers: { cookie: "a=b" } }).catch((e: unknown) => e); | |
| 112 | const notified = await post(`${PUBLIC}/notify`, "hi", {}); | |
| 113 | const probed = await statusOf(`${PUBLIC}/`); | |
| 114 | return { helper, notified, probed }; | |
| 115 | }); | |
| 116 | expect(result.helper).toBeInstanceOf(RefusedError); | |
| 117 | expect((result.helper as Error).message).toBe(OWN_HOST_SENTENCE); | |
| 118 | expect(result.notified).toBe(false); | |
| 119 | expect(result.probed).toBeNull(); | |
| 120 | expect(asked).toEqual([]); | |
| 121 | }); | |
| 122 | ||
| 123 | test("lineage of an address on the own host reads the listener, anonymously", async () => { | |
| 124 | setOwnHost(PUBLIC, local); | |
| 125 | const { result, asked } = await watched(() => lookupLineage(`${PUBLIC}/me/fork`)); | |
| 126 | expect(result).toEqual(["up/root"]); | |
| 127 | expect(asked.every((u) => new URL(u).host === `127.0.0.1:${server.port}`)).toBe(true); | |
| 128 | expect(asked.length).toBeGreaterThan(0); | |
| 129 | }); | |
| 130 | ||
| 131 | test("without its own host set, the same URL is not taken for the forge's", async () => { | |
| 132 | // The network is stubbed to reject: the call fails out on the vetted path, not at the listener. | |
| 133 | const { asked } = await watched( | |
| 134 | () => outboundFetch(`${PUBLIC}/x`, {}, { self: true, resolve: async () => ["93.184.216.34"] }).catch(() => undefined), | |
| 135 | false, | |
| 136 | ); | |
| 137 | expect(asked.includes(`${local}/x`)).toBe(false); | |
| 138 | expect(asked).toHaveLength(1); | |
| 139 | }); |
new above
offline
Real network call to a .test host: stub fetch to reject so the test is hermetic.