Skip to content

Commit 00d153f

Browse files
committed
fix(cli): capture reads the hosts file first and kills its lookup child on exit
The hosts file answers before DNS, as the system lookup does (overrides and 0.0.0.0 blocks keep working). A host whose system answer is cached skips c-ares, a caller that joins a running lookup keeps its own deadline, and a pending lookup child is killed when the CLI exits.
1 parent e7bb8b0 commit 00d153f

6 files changed

Lines changed: 161 additions & 17 deletions

File tree

‎packages/cli/src/capture/captureProxy.browser.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ vi.mock("node:child_process", async (importOriginal) => {
3737
if (!(args[1] as string[] | undefined)?.includes("-e")) return real.execFile(...args);
3838
const callback = args.at(-1) as (error: Error, stdout: string, stderr: string) => void;
3939
callback(new Error("Fixture has no system DNS"), "", "ENOTFOUND");
40+
return { once() {}, kill() {} } as unknown as ReturnType<typeof real.execFile>;
4041
},
4142
};
4243
});

‎packages/cli/src/capture/publicNetwork.fixture.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,38 @@ dns.Resolver = class extends Resolver {
1010
this.setServers([server]);
1111
}
1212
};
13+
const requireBuiltin = createRequire(import.meta.url);
14+
const fsPromises = requireBuiltin("node:fs/promises");
15+
const readFile = fsPromises.readFile;
16+
fsPromises.readFile = (path: string, ...rest: unknown[]) =>
17+
/hosts$/.test(String(path))
18+
? Promise.resolve(process.env.HF_TEST_HOSTS ?? "")
19+
: readFile(path, ...rest);
1320
// HF_TEST_SYSTEM: "stall" for a system lookup that never returns, or the address it answers.
1421
const system = process.env.HF_TEST_SYSTEM ?? "stall";
15-
const childProcess = createRequire(import.meta.url)("node:child_process");
22+
const childProcess = requireBuiltin("node:child_process");
1623
const execFile = childProcess.execFile;
17-
childProcess.execFile = (file: string, _args: string[], ...rest: unknown[]) =>
18-
execFile(
24+
let lookupPid: number | undefined;
25+
childProcess.execFile = (file: string, _args: string[], ...rest: unknown[]) => {
26+
const child = execFile(
1927
file,
2028
[
2129
"-e",
2230
system === "stall" ? "setInterval(() => {}, 1000)" : `process.stdout.write('["${system}"]')`,
2331
],
2432
...rest,
2533
);
34+
lookupPid = child.pid;
35+
return child;
36+
};
2637
syncBuiltinESMExports();
38+
const exitAfterMs = Number(process.env.HF_TEST_EXIT_AFTER_MS ?? 0);
39+
if (exitAfterMs) {
40+
setTimeout(() => {
41+
process.stdout.write(`exit ${lookupPid}\n`);
42+
process.exit(0);
43+
}, exitAfterMs);
44+
}
2745
const { resolvePublicAddress } = await import("./publicNetwork.js");
2846
const outcome = await resolvePublicAddress("assets.capture-fixture.com").catch(
2947
(error: Error) => error.message,

‎packages/cli/src/capture/publicNetwork.process.test.ts‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@ import { dirname, join } from "node:path";
33
import { fileURLToPath } from "node:url";
44
import { expect, it, onTestFinished } from "vitest";
55
import { startTestDnsServer } from "../utils/dnsTestServer.js";
6+
import { testProcessIsAlive } from "../utils/processTestUtils.js";
7+
import { systemLookupInKillableChild } from "./publicNetwork.js";
68

79
const fixture = join(dirname(fileURLToPath(import.meta.url)), "publicNetwork.fixture.ts");
810

911
/** What the fixture printed, and how long its process lived after printing it. */
10-
async function resolveInChild(mode: "silent" | "answer", system: string, ipv4?: string) {
12+
async function resolveInChild(
13+
mode: "silent" | "answer",
14+
system: string,
15+
ipv4?: string,
16+
env: Record<string, string> = {},
17+
) {
1118
const server = await startTestDnsServer(mode, ipv4);
1219
onTestFinished(() => server.close());
1320
const child = spawn(process.execPath, ["--import", "tsx", fixture], {
14-
env: { ...process.env, HF_TEST_DNS: server.address, HF_TEST_SYSTEM: system },
21+
env: { ...process.env, HF_TEST_DNS: server.address, HF_TEST_SYSTEM: system, ...env },
1522
stdio: ["ignore", "pipe", "inherit"],
1623
});
1724
onTestFinished(() => void child.kill("SIGKILL"));
@@ -45,3 +52,27 @@ it("uses the DNS answer without asking the system", async () => {
4552
expect(run.printed).toBe("93.184.216.34");
4653
expect(run.livedOnMs).toBeLessThan(1_000);
4754
}, 30_000);
55+
56+
it("takes the hosts file's answer over the DNS one, as the system lookup does", async () => {
57+
const hosts = "# local override\n8.8.4.4 assets.capture-fixture.com\n";
58+
const run = await resolveInChild("answer", "stall", "93.184.216.34", { HF_TEST_HOSTS: hosts });
59+
expect(run.printed).toBe("8.8.4.4");
60+
}, 30_000);
61+
62+
it("refuses a host the hosts file blocks", async () => {
63+
const hosts = "0.0.0.0 assets.capture-fixture.com\n";
64+
const run = await resolveInChild("answer", "stall", "93.184.216.34", { HF_TEST_HOSTS: hosts });
65+
expect(run.printed).toBe("Capture DNS returned a non-public address");
66+
}, 30_000);
67+
68+
it("leaves no system lookup running when the CLI exits mid-lookup", async () => {
69+
const run = await resolveInChild("silent", "stall", undefined, { HF_TEST_EXIT_AFTER_MS: "6000" });
70+
const pid = Number(/^exit (\d+)$/m.exec(run.printed)?.[1]);
71+
onTestFinished(() => void (testProcessIsAlive(pid) && process.kill(pid, "SIGKILL")));
72+
expect(pid).toBeGreaterThan(0);
73+
expect(testProcessIsAlive(pid)).toBe(false);
74+
}, 30_000);
75+
76+
it("runs the real system lookup in its child", async () => {
77+
await expect(systemLookupInKillableChild("localhost", 10_000)).resolves.toContain("127.0.0.1");
78+
}, 30_000);

‎packages/cli/src/capture/publicNetwork.test.ts‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EventEmitter } from "node:events";
33
import { PassThrough } from "node:stream";
44
const mocks = vi.hoisted(() => ({ resolve4: vi.fn(), request: vi.fn(), execFile: vi.fn() }));
55
vi.mock("node:child_process", () => ({ execFile: mocks.execFile }));
6+
vi.mock("node:fs/promises", () => ({ readFile: async () => "" }));
67
vi.mock("node:dns", () => ({
78
promises: {
89
Resolver: class {
@@ -17,9 +18,10 @@ import { isPublicIPv4, publicRequest, resolvePublicAddress } from "./publicNetwo
1718

1819
/** The system lookup child prints `stdout`, or fails with `stderr`. */
1920
function systemLookupPrints(stdout: string, stderr = "") {
20-
mocks.execFile.mockImplementation((_file, _args, _options, callback) =>
21-
callback(stdout ? null : new Error("Command failed"), stdout, stderr),
22-
);
21+
mocks.execFile.mockImplementation((_file, _args, _options, callback) => {
22+
callback(stdout ? null : new Error("Command failed"), stdout, stderr);
23+
return new EventEmitter();
24+
});
2325
}
2426

2527
beforeEach(() => {
@@ -88,6 +90,28 @@ describe("public address policy", () => {
8890
await expect(resolvePublicAddress("cached.example.org")).resolves.toBe("8.8.8.8");
8991
expect(mocks.execFile).toHaveBeenCalledTimes(2);
9092
});
93+
it("holds a later caller to its own deadline when it joins a system lookup already running", async () => {
94+
vi.useFakeTimers();
95+
try {
96+
mocks.resolve4.mockRejectedValue(new Error("queryA ENOTFOUND"));
97+
mocks.execFile.mockImplementation(() => new EventEmitter());
98+
const first = resolvePublicAddress("slow.example.org").catch((error: Error) => error.message);
99+
await vi.advanceTimersByTimeAsync(9_000);
100+
const second = resolvePublicAddress("slow.example.org").catch(
101+
(error: Error) => error.message,
102+
);
103+
await vi.advanceTimersByTimeAsync(1_000);
104+
expect(await first).toBe("Capture DNS timed out");
105+
await vi.advanceTimersByTimeAsync(8_000);
106+
expect(await Promise.race([second, "still waiting"])).toBe("still waiting");
107+
await vi.advanceTimersByTimeAsync(1_000);
108+
expect(await second).toBe("Capture DNS timed out");
109+
expect(mocks.execFile).toHaveBeenCalledOnce();
110+
expect(mocks.resolve4).toHaveBeenCalledOnce();
111+
} finally {
112+
vi.useRealTimers();
113+
}
114+
});
91115
it("never starts the system lookup when DNS answers", async () => {
92116
mocks.resolve4.mockResolvedValue(["8.8.8.8"]);
93117
await expect(resolvePublicAddress("example.org")).resolves.toBe("8.8.8.8");

‎packages/cli/src/capture/publicNetwork.ts‎

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { execFile } from "node:child_process";
1+
import { execFile, type ChildProcess } from "node:child_process";
22
import { promises as dns } from "node:dns";
3+
import { readFile } from "node:fs/promises";
34
import { isIP } from "node:net";
45
import { request as httpRequest, type IncomingMessage, type ClientRequest } from "node:http";
56
import { request as httpsRequest } from "node:https";
@@ -54,17 +55,60 @@ export async function resolvePublicAddress(hostname: string): Promise<string> {
5455
) {
5556
throw new Error("Capture requires a public hostname");
5657
}
57-
const deadline = Date.now() + DNS_TIMEOUT_MS;
58-
const answers = await resolveWithCares(host).catch(() =>
59-
resolveWithSystemInKillableChild(host, deadline - Date.now()),
60-
);
58+
const answers = await resolveIPv4(host);
6159
const first = answers[0];
6260
if (!first || !answers.every(isPublicIPv4)) {
6361
throw new Error("Capture DNS returned a non-public address");
6462
}
6563
return first;
6664
}
6765

66+
async function resolveIPv4(host: string): Promise<string[]> {
67+
const fromHostsFile = (await hostsFileAnswers()).get(host);
68+
if (fromHostsFile) return fromHostsFile;
69+
const deadline = Date.now() + DNS_TIMEOUT_MS;
70+
const fromSystem = systemAnswers.get(host);
71+
if (fromSystem) return beforeDeadline(fromSystem, deadline);
72+
return resolveWithCares(host).catch(() =>
73+
beforeDeadline(systemLookupInKillableChild(host, deadline - Date.now()), deadline),
74+
);
75+
}
76+
77+
let hostsFile: Promise<Map<string, string[]>> | undefined;
78+
79+
function hostsFileAnswers(): Promise<Map<string, string[]>> {
80+
const path =
81+
process.platform === "win32"
82+
? `${process.env.SystemRoot ?? "C:\\Windows"}\\System32\\drivers\\etc\\hosts`
83+
: "/etc/hosts";
84+
hostsFile ??= readFile(path, "utf8").then(parseHostsFile, () => new Map());
85+
return hostsFile;
86+
}
87+
88+
function parseHostsFile(text: string): Map<string, string[]> {
89+
const answers = new Map<string, string[]>();
90+
for (const line of text.split(/\r?\n/)) {
91+
const [address, ...names] = line.replace(/#.*/, "").trim().split(/\s+/);
92+
if (!address || isIP(address) !== 4) continue;
93+
for (const name of names) {
94+
const key = name.toLowerCase().replace(/\.$/, "");
95+
if (!answers.has(key)) answers.set(key, [address]);
96+
}
97+
}
98+
return answers;
99+
}
100+
101+
function beforeDeadline<T>(work: Promise<T>, deadline: number): Promise<T> {
102+
let timer: ReturnType<typeof setTimeout> | undefined;
103+
const late = new Promise<never>((_resolve, reject) => {
104+
timer = setTimeout(
105+
() => reject(new Error("Capture DNS timed out")),
106+
Math.max(0, deadline - Date.now()),
107+
);
108+
});
109+
return Promise.race([work, late]).finally(() => clearTimeout(timer));
110+
}
111+
68112
async function resolveWithCares(host: string): Promise<string[]> {
69113
const cancellableResolver = new dns.Resolver();
70114
const timer = setTimeout(() => cancellableResolver.cancel(), CARES_TIMEOUT_MS);
@@ -79,11 +123,12 @@ const systemAnswers = new Map<string, Promise<string[]>>();
79123
const SYSTEM_LOOKUP = `require("node:dns").lookup(process.argv[1], { all: true, family: 4 }, (error, answers) =>
80124
error ? process.stderr.write(String(error.code)) : process.stdout.write(JSON.stringify(answers.map((a) => a.address))))`;
81125

82-
function resolveWithSystemInKillableChild(host: string, timeoutMs: number): Promise<string[]> {
83-
const cached = systemAnswers.get(host);
84-
if (cached) return cached;
126+
const liveLookups = new Set<ChildProcess>();
127+
128+
export function systemLookupInKillableChild(host: string, timeoutMs: number): Promise<string[]> {
129+
if (!process.listeners("exit").includes(killLiveLookups)) process.on("exit", killLiveLookups);
85130
const answers = new Promise<string[]>((resolve, reject) => {
86-
execFile(
131+
const child = execFile(
87132
process.execPath,
88133
["-e", SYSTEM_LOOKUP, host],
89134
{
@@ -104,12 +149,18 @@ function resolveWithSystemInKillableChild(host: string, timeoutMs: number): Prom
104149
}
105150
},
106151
);
152+
liveLookups.add(child);
153+
child.once("exit", () => liveLookups.delete(child));
107154
});
108155
systemAnswers.set(host, answers);
109156
answers.catch(() => systemAnswers.delete(host));
110157
return answers;
111158
}
112159

160+
function killLiveLookups(): void {
161+
for (const child of liveLookups) child.kill("SIGKILL");
162+
}
163+
113164
/** Pinned asset transport; redirects are returned for caller validation. */
114165
export async function publicRequest(url: string | URL, init: RequestInit = {}): Promise<Response> {
115166
const target = new URL(url);

‎packages/cli/src/commands/capture.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,25 @@ describe("capture command — vision control", () => {
238238
}
239239
});
240240

241+
it("asks to end the process with 1 after a capture that threw", async () => {
242+
vi.useFakeTimers();
243+
const exit = vi.fn();
244+
registerRootExitRequester(exit);
245+
try {
246+
vi.spyOn(console, "log").mockImplementation(() => {});
247+
vi.spyOn(console, "error").mockImplementation(() => {});
248+
captureWebsiteMock.mockRejectedValueOnce(new Error("browser crashed"));
249+
await captureCommand.run!({
250+
args: { url: "https://example.com", output: "/tmp/hf-exit-threw", json: true },
251+
} as never).catch(() => {});
252+
vi.advanceTimersByTime(100);
253+
expect(exit).toHaveBeenCalledWith(1);
254+
} finally {
255+
registerRootExitRequester(() => {});
256+
vi.useRealTimers();
257+
}
258+
});
259+
241260
it("returns a nonzero command result after presenting failed capture JSON once", async () => {
242261
const log = vi.spyOn(console, "log").mockImplementation(() => {});
243262
vi.spyOn(console, "error").mockImplementation(() => {});

0 commit comments

Comments
 (0)