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) #10615
scout/forge:scout/10557-inside-a-child-cell-the-looks-browser-ca → main 3 commits fork
3 files
changed +98
−4
src/looks/capture.ts +50 −3
| @@ -40,6 +40,7 @@ import { join } from "node:path"; | ||
| 40 | 40 | import { childEnv } from "../apps/process.ts"; |
| 41 | 41 | import { Gate } from "../git/env.ts"; |
| 42 | 42 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 43 | import { upstreamProxy } from "../apps/egress.ts"; | |
| 43 | 44 | import type { Entries } from "../release/webvitals.ts"; |
| 44 | 45 | |
| 45 | 46 | export interface LookInput { |
| @@ -238,6 +239,43 @@ export function refusal( | ||
| 238 | 239 | return undefined; |
| 239 | 240 | } |
| 240 | 241 | |
| 242 | /** Where the browser goes: the address it dials, the host the app knows itself by. */ | |
| 243 | export interface Target { | |
| 244 | /** What `Page.navigate` opens. */ | |
| 245 | readonly url: string; | |
| 246 | /** The host the page must end on. */ | |
| 247 | readonly host: string; | |
| 248 | /** Sent with every request, so the app is served as its own host. */ | |
| 249 | readonly headers: Record<string, string>; | |
| 250 | /** A host-resolver rule, or undefined when the address needs none. */ | |
| 251 | readonly rule?: string | undefined; | |
| 252 | } | |
| 253 | ||
| 254 | /** | |
| 255 | * Inside a child forge's cell the network is loopback and nothing else, and | |
| 256 | * Chromium takes that for "offline": any name it must resolve fails with | |
| 257 | * `net::ERR_INTERNET_DISCONNECTED`, even one mapped to 127.0.0.1. There the | |
| 258 | * browser dials the loopback address itself, and the app's hostname travels | |
| 259 | * in the `Host` header: the same listener, the same app, no resolving, and | |
| 260 | * no network the cell did not have. | |
| 261 | */ | |
| 262 | export function targetFor( | |
| 263 | input: string, | |
| 264 | inCell: boolean, | |
| 265 | ): Target { | |
| 266 | const url = new URL(input); | |
| 267 | // An address needs no resolving, in a cell or out of one: it is dialled as it is. | |
| 268 | const literal = /^\d{1,3}(\.\d{1,3}){3}$/.test(url.hostname) || url.hostname.startsWith("["); | |
| 269 | if (!inCell || literal) return { url: input, host: url.hostname, headers: {}, rule: `MAP ${url.hostname} 127.0.0.1` }; | |
| 270 | const local = new URL(input); | |
| 271 | local.hostname = "127.0.0.1"; | |
| 272 | return { | |
| 273 | url: local.href, | |
| 274 | host: "127.0.0.1", | |
| 275 | headers: { Host: url.host }, | |
| 276 | }; | |
| 277 | } | |
| 278 | ||
| 241 | 279 | /** The DevTools endpoint Chromium writes into its profile once it listens. */ |
| 242 | 280 | async function endpoint( |
| 243 | 281 | profile: string, |
| @@ -362,7 +400,8 @@ async function shoot( | ||
| 362 | 400 | binary: string, |
| 363 | 401 | input: LookInput, |
| 364 | 402 | ): Promise<Result<Recorded, string>> { |
| 365 | const host = new URL(input.url).hostname; | |
| 403 | const target = targetFor(input.url, upstreamProxy() !== undefined); | |
| 404 | const host = target.host; | |
| 366 | 405 | // A profile of its own, gone afterwards: no cookie outlives its picture, |
| 367 | 406 | // and two forges in one process never share a browser's state. |
| 368 | 407 | const profile = mkdtempSync(join(tmpdir(), "forge-look-")); |
| @@ -388,7 +427,7 @@ async function shoot( | ||
| 388 | 427 | "--no-first-run", |
| 389 | 428 | `--user-data-dir=${profile}`, |
| 390 | 429 | // The app's hostname is the forge's own listener: no DNS, no edge. |
| 391 | `--host-resolver-rules=MAP ${host} 127.0.0.1`, | |
| 430 | ...(target.rule === undefined ? [] : [`--host-resolver-rules=${target.rule}`]), | |
| 392 | 431 | `--window-size=${input.width},${input.height}`, |
| 393 | 432 | "--force-device-scale-factor=1", |
| 394 | 433 | "--js-flags=--max-old-space-size=256", |
| @@ -480,8 +519,16 @@ async function shoot( | ||
| 480 | 519 | { source: OBSERVER }, |
| 481 | 520 | sessionId, |
| 482 | 521 | ); |
| 522 | if (Object.keys(target.headers).length > 0) { | |
| 523 | await dt.send("Network.enable", {}, sessionId); | |
| 524 | await dt.send( | |
| 525 | "Network.setExtraHTTPHeaders", | |
| 526 | { headers: target.headers }, | |
| 527 | sessionId, | |
| 528 | ); | |
| 529 | } | |
| 483 | 530 | const loaded = dt.heard("Page.loadEventFired", sessionId); |
| 484 | const nav = await dt.send("Page.navigate", { url: input.url }, sessionId); | |
| 531 | const nav = await dt.send("Page.navigate", { url: target.url }, sessionId); | |
| 485 | 532 | // A page that never finishes loading is pictured as it stands. |
| 486 | 533 | await Promise.race([loaded, Bun.sleep(LOAD_MS)]); |
| 487 | 534 | await Bun.sleep(SETTLE_MS); |
test/look-cell-target.test.ts added +46
| @@ -0,0 +1,46 @@ | ||
| 1 | /** Inside a child forge's cell the browser dials loopback itself and sends the app's host (#10557). */ | |
| 2 | import { describe, expect, test } from "bun:test"; | |
| 3 | import { targetFor } from "../src/looks/capture.ts"; | |
| 4 | ||
| 5 | const URL_ = "http://hello.lab.test:8123/__forge/grant?t=abc&to=%2F"; | |
| 6 | ||
| 7 | describe("targetFor", () => { | |
| 8 | test("outside a cell the app's host is mapped to loopback by the browser", () => { | |
| 9 | const t = targetFor(URL_, false); | |
| 10 | expect(t.url).toBe(URL_); | |
| 11 | expect(t.host).toBe("hello.lab.test"); | |
| 12 | expect(t.rule).toBe("MAP hello.lab.test 127.0.0.1"); | |
| 13 | expect(t.headers).toEqual({}); | |
| 14 | }); | |
| 15 | ||
| 16 | test("inside a cell no name is resolved: loopback address, app host in the Host header", () => { | |
| 17 | const t = targetFor(URL_, true); | |
| 18 | expect(t.url).toBe("http://127.0.0.1:8123/__forge/grant?t=abc&to=%2F"); | |
| 19 | expect(t.host).toBe("127.0.0.1"); | |
| 20 | expect(t.rule).toBeUndefined(); | |
| 21 | expect(t.headers).toEqual({ Host: "hello.lab.test:8123" }); | |
| 22 | }); | |
| 23 | ||
| 24 | test("an address is dialled as it is, in a cell too: no header, no new path", () => { | |
| 25 | const t = targetFor("http://127.0.0.1:8123/", true); | |
| 26 | expect(t.url).toBe("http://127.0.0.1:8123/"); | |
| 27 | expect(t.host).toBe("127.0.0.1"); | |
| 28 | expect(t.headers).toEqual({}); | |
| 29 | }); | |
| 30 | ||
| 31 | test("the cell's target reaches the app on its own port as its own host, and only loopback", async () => { | |
| 32 | const server = Bun.serve({ | |
| 33 | port: 0, | |
| 34 | hostname: "127.0.0.1", | |
| 35 | fetch: (req) => new Response(req.headers.get("host") ?? ""), | |
| 36 | }); | |
| 37 | try { | |
| 38 | const t = targetFor(`http://hello.lab.test:${server.port}/`, true); | |
| 39 | expect(new URL(t.url).hostname).toBe("127.0.0.1"); | |
| 40 | const res = await fetch(t.url, { headers: t.headers }); | |
| 41 | expect(await res.text()).toBe(`hello.lab.test:${server.port}`); | |
| 42 | } finally { | |
| 43 | server.stop(true); | |
| 44 | } | |
| 45 | }); | |
| 46 | }); |
test/looks.test.ts +2 −1
| @@ -1393,7 +1393,8 @@ describe("the look", () => { | ||
| 1393 | 1393 | height: 844, |
| 1394 | 1394 | timeoutMs: 30_000, |
| 1395 | 1395 | }); |
| 1396 | expect(taken.ok).toBe(true); | |
| 1396 | // The whole result is compared, so a failure names its reason. | |
| 1397 | expect(JSON.stringify(taken).slice(0, 400)).toContain('"ok":true'); | |
| 1397 | 1398 | // The observer ran before the page: every type is a list, as far as this browser has it. |
| 1398 | 1399 | const entries = taken.ok ? taken.value.entries : undefined; |
| 1399 | 1400 | expect(Array.isArray(entries?.lcp)).toBe(true); |
new above
offline