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

Parked opened this in answer to
Closed without merging

scout/forge:scout/10557-inside-a-child-cell-the-looks-browser-ca → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +98 −4
src/looks/capture.ts +50 −3
@@ -40,6 +40,7 @@ import { join } from "node:path";
4040 import { childEnv } from "../apps/process.ts";
4141 import { Gate } from "../git/env.ts";
4242 import { err, ok, type Result } from "../kernel/result.ts";
43 import { upstreamProxy } from "../apps/egress.ts";
4344 import type { Entries } from "../release/webvitals.ts";
4445
4546 export interface LookInput {
@@ -238,6 +239,43 @@ export function refusal(
238239 return undefined;
239240 }
240241
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
241279 /** The DevTools endpoint Chromium writes into its profile once it listens. */
242280 async function endpoint(
243281 profile: string,
@@ -362,7 +400,8 @@ async function shoot(
362400 binary: string,
363401 input: LookInput,
364402 ): 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;
366405 // A profile of its own, gone afterwards: no cookie outlives its picture,
367406 // and two forges in one process never share a browser's state.
368407 const profile = mkdtempSync(join(tmpdir(), "forge-look-"));
@@ -388,7 +427,7 @@ async function shoot(
388427 "--no-first-run",
389428 `--user-data-dir=${profile}`,
390429 // 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}`]),
392431 `--window-size=${input.width},${input.height}`,
393432 "--force-device-scale-factor=1",
394433 "--js-flags=--max-old-space-size=256",
@@ -480,8 +519,16 @@ async function shoot(
480519 { source: OBSERVER },
481520 sessionId,
482521 );
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 }
483530 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);
485532 // A page that never finishes loading is pictured as it stands.
486533 await Promise.race([loaded, Bun.sleep(LOAD_MS)]);
487534 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", () => {
13931393 height: 844,
13941394 timeoutMs: 30_000,
13951395 });
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');
13971398 // The observer ran before the page: every type is a list, as far as this browser has it.
13981399 const entries = taken.ok ? taken.value.entries : undefined;
13991400 expect(Array.isArray(entries?.lcp)).toBe(true);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs