Public: anyone can read this; the crew can change it.
Inside a child cell, the looks browser cannot reach the child's own app (net::ERR_INTERNET_DISCONNECTED) #10610
mason/forge:mason/10557-inside-a-child-cell-the-looks-browser-ca → main 3 commits fork
2 files
changed +105
−3
src/looks/capture.ts +45 −3
| @@ -34,7 +34,7 @@ | ||
| 34 | 34 | * process starts, and a pod that holds two agent runs has room for one. |
| 35 | 35 | */ |
| 36 | 36 | |
| 37 | import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | |
| 37 | import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | |
| 38 | 38 | import { tmpdir } from "node:os"; |
| 39 | 39 | import { join } from "node:path"; |
| 40 | 40 | import { childEnv } from "../apps/process.ts"; |
| @@ -53,6 +53,39 @@ export interface LookInput { | ||
| 53 | 53 | readonly dark?: boolean | undefined; |
| 54 | 54 | /** A browser that has no picture by then is killed; the look fails. */ |
| 55 | 55 | readonly timeoutMs: number; |
| 56 | /** | |
| 57 | * Open the forge's port by its loopback address, naming the app's host | |
| 58 | * only in the `Host` header. Chromium treats a machine with no network | |
| 59 | * but loopback (a cell) as offline and refuses any hostname there with | |
| 60 | * ERR_INTERNET_DISCONNECTED, resolved or not; an address needs no lookup. | |
| 61 | * Unset: on when this machine has no interface but loopback. | |
| 62 | */ | |
| 63 | readonly byAddress?: boolean | undefined; | |
| 64 | } | |
| 65 | ||
| 66 | /** Whether a list of network interface names holds nothing but loopback. */ | |
| 67 | export const onlyLoopback = (names: readonly string[]): boolean => | |
| 68 | names.every((n) => n === "lo"); | |
| 69 | ||
| 70 | /** Whether this process lives in a network with loopback alone, as a cell does. */ | |
| 71 | export function isolated(): boolean { | |
| 72 | try { | |
| 73 | return onlyLoopback(readdirSync("/sys/class/net")); | |
| 74 | } catch { | |
| 75 | return false; | |
| 76 | } | |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * The same page by the loopback address: the URL to open, the `Host` the | |
| 81 | * app is to see, and the hostname the browser will report. The port is the | |
| 82 | * forge's own, as in the hostname form; nothing but loopback is reached. | |
| 83 | */ | |
| 84 | export function byAddress(url: string): { url: string; host: string; hostname: string } { | |
| 85 | const u = new URL(url); | |
| 86 | const host = u.host; | |
| 87 | u.hostname = "127.0.0.1"; | |
| 88 | return { url: u.href, host, hostname: "127.0.0.1" }; | |
| 56 | 89 | } |
| 57 | 90 | |
| 58 | 91 | /** What the page recorded while it loaded; a type the browser lacks is an empty list. */ |
| @@ -362,7 +395,9 @@ async function shoot( | ||
| 362 | 395 | binary: string, |
| 363 | 396 | input: LookInput, |
| 364 | 397 | ): Promise<Result<Recorded, string>> { |
| 365 | const host = new URL(input.url).hostname; | |
| 398 | const door = (input.byAddress ?? isolated()) ? byAddress(input.url) : undefined; | |
| 399 | const host = door?.hostname ?? new URL(input.url).hostname; | |
| 400 | const target = door?.url ?? input.url; | |
| 366 | 401 | // A profile of its own, gone afterwards: no cookie outlives its picture, |
| 367 | 402 | // and two forges in one process never share a browser's state. |
| 368 | 403 | const profile = mkdtempSync(join(tmpdir(), "forge-look-")); |
| @@ -458,6 +493,7 @@ async function shoot( | ||
| 458 | 493 | flatten: true, |
| 459 | 494 | })) as { sessionId: string }; |
| 460 | 495 | await dt.send("Page.enable", {}, sessionId); |
| 496 | if (door !== undefined) await dt.send("Network.enable", {}, sessionId); | |
| 461 | 497 | await dt.send( |
| 462 | 498 | "Emulation.setDeviceMetricsOverride", |
| 463 | 499 | { |
| @@ -480,8 +516,14 @@ async function shoot( | ||
| 480 | 516 | { source: OBSERVER }, |
| 481 | 517 | sessionId, |
| 482 | 518 | ); |
| 519 | if (door !== undefined) | |
| 520 | await dt.send( | |
| 521 | "Network.setExtraHTTPHeaders", | |
| 522 | { headers: { Host: door.host } }, | |
| 523 | sessionId, | |
| 524 | ); | |
| 483 | 525 | const loaded = dt.heard("Page.loadEventFired", sessionId); |
| 484 | const nav = await dt.send("Page.navigate", { url: input.url }, sessionId); | |
| 526 | const nav = await dt.send("Page.navigate", { url: target }, sessionId); | |
| 485 | 527 | // A page that never finishes loading is pictured as it stands. |
| 486 | 528 | await Promise.race([loaded, Bun.sleep(LOAD_MS)]); |
| 487 | 529 | await Bun.sleep(SETTLE_MS); |
test/look-by-address.test.ts added +60
| @@ -0,0 +1,60 @@ | ||
| 1 | /** Inside a cell (loopback only) a look opens the forge's port by address, not by hostname (#10557). */ | |
| 2 | import { describe, expect, test } from "bun:test"; | |
| 3 | import { byAddress, findChromium, onlyLoopback, takeLook } from "../src/looks/capture.ts"; | |
| 4 | import { mkdtempSync, existsSync } from "node:fs"; | |
| 5 | import { tmpdir } from "node:os"; | |
| 6 | import { join } from "node:path"; | |
| 7 | ||
| 8 | describe("by address", () => { | |
| 9 | test("a machine with only lo is isolated; one with an uplink is not", () => { | |
| 10 | expect(onlyLoopback(["lo"])).toBe(true); | |
| 11 | expect(onlyLoopback(["lo", "eth0"])).toBe(false); | |
| 12 | }); | |
| 13 | ||
| 14 | test("the URL keeps its port and path; the host moves to the header", () => { | |
| 15 | const door = byAddress("http://app.lab.test:4123/__forge/grant?t=a%20b&to=%2F"); | |
| 16 | expect(door.url).toBe("http://127.0.0.1:4123/__forge/grant?t=a%20b&to=%2F"); | |
| 17 | expect(door.host).toBe("app.lab.test:4123"); | |
| 18 | }); | |
| 19 | }); | |
| 20 | ||
| 21 | const seen: string[] = []; | |
| 22 | const outcome = await (async () => { | |
| 23 | if (findChromium() === undefined) return "no chromium"; | |
| 24 | const server = Bun.serve({ | |
| 25 | hostname: "127.0.0.1", | |
| 26 | port: 0, | |
| 27 | fetch(req) { | |
| 28 | const host = req.headers.get("host") ?? ""; | |
| 29 | seen.push(host); | |
| 30 | if (!host.startsWith("app.lab.test:")) return new Response("wrong host", { status: 404 }); | |
| 31 | return new Response("<!doctype html><title>hi</title><h1>hello</h1>", { | |
| 32 | headers: { "content-type": "text/html" }, | |
| 33 | }); | |
| 34 | }, | |
| 35 | }); | |
| 36 | try { | |
| 37 | const out = join(mkdtempSync(join(tmpdir(), "look-addr-")), "p.png"); | |
| 38 | const shot = await takeLook({ | |
| 39 | url: `http://app.lab.test:${server.port}/`, | |
| 40 | out, | |
| 41 | width: 400, | |
| 42 | height: 300, | |
| 43 | timeoutMs: 20_000, | |
| 44 | byAddress: true, | |
| 45 | }); | |
| 46 | const hosts = seen.map((h) => h.replace(String(server.port), "P")); | |
| 47 | return `${shot.ok ? "ok" : shot.error.slice(0, 200)} file=${existsSync(out)} hosts=${hosts.join("|")}`; | |
| 48 | } finally { | |
| 49 | server.stop(true); | |
| 50 | } | |
| 51 | })(); | |
| 52 | ||
| 53 | describe("by address, captured", () => { | |
| 54 | test(`a child gets a picture of its own app by address [${outcome}]`, () => { | |
| 55 | if (outcome === "no chromium") return; | |
| 56 | expect(outcome.startsWith("ok file=true")).toBe(true); | |
| 57 | expect(seen.every((h) => h.startsWith("app.lab.test:"))).toBe(true); | |
| 58 | expect(seen.length).toBeGreaterThan(0); | |
| 59 | }); | |
| 60 | }); |
new above
offline