Skip to content

Commit d372fa6

Browse files
committed
fix(cli): a finished capture exits even when a site's DNS never answers
Capture resolved every page request with the system lookup and gave up after 10 s, but a system lookup cannot be cancelled: the abandoned one kept the process alive until the OS resolver timed out, so a capture that had printed its result could run past the app's 5-minute cap and lose its folder. Resolve through c-ares instead, as hostAnswers does, and cancel it at the same 10 s deadline.
1 parent a8a8032 commit d372fa6

9 files changed

Lines changed: 172 additions & 94 deletions

‎.fallowrc.jsonc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
// Warm-check client, spawned by path from warmCheck.process.test.ts.
6363
"packages/cli/src/warmCheckClient.fixture.ts",
6464
"packages/cli/src/browser/chromeOrphans.fixture.ts",
65+
"packages/cli/src/capture/publicNetwork.fixture.ts",
6566
// Test fixture worker, spawned by path via the pools' workerEntryPath
6667
// option from the crash-recovery tests; has no import-graph referrer.
6768
"packages/producer/src/services/__fixtures__/crashOnMessageWorker.mjs",

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,24 @@ const fixture = vi.hoisted(() => ({
1111
rebound: false,
1212
dialed: [] as string[],
1313
}));
14-
vi.mock("node:dns/promises", () => ({
15-
lookup: vi.fn(async (host: string) => {
16-
if (host === "capture-fixture.example.com") {
17-
return [{ address: fixture.rebound ? "127.0.0.1" : "93.184.216.34", family: 4 }];
18-
}
19-
throw new Error("Fixture has no such DNS name");
20-
}),
21-
}));
14+
vi.mock("node:dns", async (importOriginal) => {
15+
const real = await importOriginal<typeof import("node:dns")>();
16+
return {
17+
...real,
18+
promises: {
19+
...real.promises,
20+
Resolver: class {
21+
async resolve4(host: string) {
22+
if (host === "capture-fixture.example.com") {
23+
return [fixture.rebound ? "127.0.0.1" : "93.184.216.34"];
24+
}
25+
throw new Error("Fixture has no such DNS name");
26+
}
27+
cancel() {}
28+
},
29+
},
30+
};
31+
});
2232
vi.mock("node:http", async (importOriginal) => {
2333
const original = await importOriginal<typeof import("node:http")>();
2434
return {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { promises as dns } from "node:dns";
2+
3+
const server = process.env.HF_TEST_DNS ?? "";
4+
if (!server) throw new Error("Missing DNS server");
5+
const Resolver = dns.Resolver;
6+
dns.Resolver = class extends Resolver {
7+
constructor(options?: ConstructorParameters<typeof Resolver>[0]) {
8+
super(options);
9+
this.setServers([server]);
10+
}
11+
};
12+
const { resolvePublicAddress } = await import("./publicNetwork.js");
13+
const outcome = await resolvePublicAddress("assets.capture-fixture.com").catch(
14+
(error: Error) => error.message,
15+
);
16+
process.stdout.write(`${outcome}\n`);

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,25 @@ import type { TLSSocket } from "node:tls";
1414
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
1515

1616
const transport = vi.hoisted(() => ({
17-
lookup: vi.fn(),
17+
resolve4: vi.fn(),
1818
dials: [] as HttpsOptions[],
1919
certificate: "",
2020
trustCertificate: true,
2121
}));
2222

23-
vi.mock("node:dns/promises", () => ({ lookup: transport.lookup }));
23+
vi.mock("node:dns", async (importOriginal) => {
24+
const real = await importOriginal<typeof import("node:dns")>();
25+
return {
26+
...real,
27+
promises: {
28+
...real.promises,
29+
Resolver: class {
30+
resolve4 = transport.resolve4;
31+
cancel() {}
32+
},
33+
},
34+
};
35+
});
2436

2537
// Only the already-checked numeric peer is mapped to an isolated loopback server.
2638
// The real Node HTTP/TLS transport, headers and certificate checks remain active.
@@ -172,9 +184,9 @@ describe("publicRequest controlled socket integration", () => {
172184
receipts.length = 0;
173185
transport.dials.length = 0;
174186
transport.trustCertificate = true;
175-
transport.lookup.mockReset();
176-
transport.lookup.mockResolvedValue([{ address: "127.0.0.1", family: 4 }]);
177-
transport.lookup.mockResolvedValueOnce([{ address: "93.184.216.34", family: 4 }]);
187+
transport.resolve4.mockReset();
188+
transport.resolve4.mockResolvedValue(["127.0.0.1"]);
189+
transport.resolve4.mockResolvedValueOnce(["93.184.216.34"]);
178190
});
179191

180192
afterAll(async () => {
@@ -193,12 +205,8 @@ describe("publicRequest controlled socket integration", () => {
193205
it("pins the checked address without a second DNS lookup and preserves HTTP Host/path", async () => {
194206
const response = await publicRequest(`http://assets.capture-fixture.com:${httpPort}/asset?q=1`);
195207
expect(await response.text()).toBe("fixture response");
196-
expect(transport.lookup).toHaveBeenCalledTimes(1);
197-
expect(transport.lookup).toHaveBeenCalledWith("assets.capture-fixture.com", {
198-
all: true,
199-
family: 4,
200-
verbatim: true,
201-
});
208+
expect(transport.resolve4).toHaveBeenCalledTimes(1);
209+
expect(transport.resolve4).toHaveBeenCalledWith("assets.capture-fixture.com");
202210
expect(transport.dials).toHaveLength(1);
203211
expect(transport.dials[0]?.hostname).toBe("93.184.216.34");
204212
expect(receipts).toEqual([
@@ -255,7 +263,7 @@ describe("publicRequest controlled socket integration", () => {
255263
});
256264

257265
it("rejects private DNS answers before dialing or reaching a target", async () => {
258-
transport.lookup.mockReset().mockResolvedValue([{ address: "127.0.0.1", family: 4 }]);
266+
transport.resolve4.mockReset().mockResolvedValue(["127.0.0.1"]);
259267
await expect(
260268
publicRequest(`http://assets.capture-fixture.com:${httpPort}/asset`),
261269
).rejects.toThrow(/non-public/);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { spawn } from "node:child_process";
2+
import { dirname, join } from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
import { expect, it, onTestFinished } from "vitest";
5+
import { startTestDnsServer } from "../utils/dnsTestServer.js";
6+
7+
const fixture = join(dirname(fileURLToPath(import.meta.url)), "publicNetwork.fixture.ts");
8+
9+
/** What the fixture printed, and how long its process lived after printing it. */
10+
async function resolveInChild(mode: "silent" | "answer", ipv4?: string) {
11+
const server = await startTestDnsServer(mode, ipv4);
12+
onTestFinished(() => server.close());
13+
const child = spawn(process.execPath, ["--import", "tsx", fixture], {
14+
env: { ...process.env, HF_TEST_DNS: server.address },
15+
stdio: ["ignore", "pipe", "inherit"],
16+
});
17+
onTestFinished(() => void child.kill("SIGKILL"));
18+
let printed = "";
19+
let printedAt = 0;
20+
child.stdout.setEncoding("utf8");
21+
child.stdout.on("data", (chunk: string) => {
22+
printed += chunk;
23+
if (printedAt) return;
24+
printedAt = Date.now();
25+
setTimeout(() => child.kill("SIGKILL"), 5_000).unref();
26+
});
27+
await new Promise((resolve) => child.once("exit", resolve));
28+
return { printed: printed.trim(), livedOnMs: Date.now() - printedAt };
29+
}
30+
31+
it("gives up on a DNS server that never answers, and nothing it started keeps the process alive", async () => {
32+
const run = await resolveInChild("silent");
33+
expect(run.printed).toBe("Capture DNS timed out");
34+
expect(run.livedOnMs).toBeLessThan(2_000);
35+
}, 30_000);
36+
37+
it("answers with the server's public address", async () => {
38+
expect((await resolveInChild("answer", "93.184.216.34")).printed).toBe("93.184.216.34");
39+
}, 30_000);

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

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
22
import { EventEmitter } from "node:events";
33
import { PassThrough } from "node:stream";
4-
const mocks = vi.hoisted(() => ({ lookup: vi.fn(), request: vi.fn() }));
5-
vi.mock("node:dns/promises", () => ({ lookup: mocks.lookup }));
4+
const mocks = vi.hoisted(() => ({ resolve4: vi.fn(), request: vi.fn() }));
5+
vi.mock("node:dns", () => ({
6+
promises: {
7+
Resolver: class {
8+
resolve4 = mocks.resolve4;
9+
cancel() {}
10+
},
11+
},
12+
}));
613
vi.mock("node:http", () => ({ request: mocks.request }));
714
vi.mock("node:https", () => ({ request: mocks.request }));
815
import { isPublicIPv4, publicRequest, resolvePublicAddress } from "./publicNetwork.js";
@@ -30,7 +37,7 @@ describe("public address policy", () => {
3037
])("rejects %s", (ip) => expect(isPublicIPv4(ip)).toBe(false));
3138
it("accepts public literals without DNS", async () => {
3239
await expect(resolvePublicAddress("8.8.8.8")).resolves.toBe("8.8.8.8");
33-
expect(mocks.lookup).not.toHaveBeenCalled();
40+
expect(mocks.resolve4).not.toHaveBeenCalled();
3441
});
3542
it.each([
3643
"localhost",
@@ -41,25 +48,22 @@ describe("public address policy", () => {
4148
"[::1]",
4249
])("blocks alias/literal %s before DNS", async (host) => {
4350
await expect(resolvePublicAddress(host)).rejects.toThrow();
44-
expect(mocks.lookup).not.toHaveBeenCalled();
51+
expect(mocks.resolve4).not.toHaveBeenCalled();
4552
});
4653
it("rejects mixed DNS before opening a socket", async () => {
47-
mocks.lookup.mockResolvedValue([
48-
{ address: "8.8.8.8", family: 4 },
49-
{ address: "127.0.0.1", family: 4 },
50-
]);
54+
mocks.resolve4.mockResolvedValue(["8.8.8.8", "127.0.0.1"]);
5155
await expect(publicRequest("http://example.com/")).rejects.toThrow();
5256
expect(mocks.request).not.toHaveBeenCalled();
5357
});
54-
it.each([{ answers: [] }, { answers: [{ address: "8.8.8.8", family: 6 }] }])(
58+
it.each([{ answers: [] }, { answers: ["::ffff:8.8.8.8"] }])(
5559
"rejects empty or malformed DNS",
5660
async ({ answers }) => {
57-
mocks.lookup.mockResolvedValue(answers);
61+
mocks.resolve4.mockResolvedValue(answers);
5862
await expect(resolvePublicAddress("example.com")).rejects.toThrow();
5963
},
6064
);
6165
it("propagates DNS errors", async () => {
62-
mocks.lookup.mockRejectedValue(new Error("DNS failed"));
66+
mocks.resolve4.mockRejectedValue(new Error("DNS failed"));
6367
await expect(resolvePublicAddress("example.com")).rejects.toThrow("DNS failed");
6468
});
6569
});
@@ -86,14 +90,12 @@ function fakeRequest() {
8690
}
8791
describe("pinned request", () => {
8892
it("dials the checked numeric IP only, preserving TLS identity and manual redirects", async () => {
89-
mocks.lookup
90-
.mockResolvedValueOnce([{ address: "8.8.8.8", family: 4 }])
91-
.mockResolvedValue([{ address: "127.0.0.1", family: 4 }]);
93+
mocks.resolve4.mockResolvedValueOnce(["8.8.8.8"]).mockResolvedValue(["127.0.0.1"]);
9294
fakeRequest();
9395
const response = await publicRequest("https://example.com:8443/path?q=1", {
9496
headers: { host: "evil.test", "accept-encoding": "gzip" },
9597
});
96-
expect(mocks.lookup).toHaveBeenCalledTimes(1);
98+
expect(mocks.resolve4).toHaveBeenCalledTimes(1);
9799
expect(mocks.request).toHaveBeenCalledTimes(1);
98100
expect(mocks.request.mock.calls[0]?.[0]).toMatchObject({
99101
hostname: "8.8.8.8",
@@ -116,9 +118,9 @@ describe("pinned request", () => {
116118
);
117119
it("does not dial after abort during DNS", async () => {
118120
const controller = new AbortController();
119-
mocks.lookup.mockImplementation(async () => {
121+
mocks.resolve4.mockImplementation(async () => {
120122
controller.abort();
121-
return [{ address: "8.8.8.8", family: 4 }];
123+
return ["8.8.8.8"];
122124
});
123125
await expect(
124126
publicRequest("https://example.com", { signal: controller.signal }),
@@ -128,20 +130,8 @@ describe("pinned request", () => {
128130
});
129131

130132
describe("transport lifecycle", () => {
131-
it("bounds stalled DNS without dialing", async () => {
132-
vi.useFakeTimers();
133-
try {
134-
mocks.lookup.mockImplementation(() => new Promise(() => {}));
135-
const result = expect(publicRequest("https://example.com")).rejects.toThrow("DNS timed out");
136-
await vi.advanceTimersByTimeAsync(10_000);
137-
await result;
138-
expect(mocks.request).not.toHaveBeenCalled();
139-
} finally {
140-
vi.useRealTimers();
141-
}
142-
});
143133
it("cancels the upstream stream and socket when the body is cancelled", async () => {
144-
mocks.lookup.mockResolvedValue([{ address: "8.8.8.8", family: 4 }]);
134+
mocks.resolve4.mockResolvedValue(["8.8.8.8"]);
145135
const incoming = Object.assign(new PassThrough(), { statusCode: 200, headers: {} });
146136
const req = Object.assign(new EventEmitter(), { end: vi.fn(), destroy: vi.fn() });
147137
mocks.request.mockImplementation((_options, callback) => {
@@ -161,7 +151,7 @@ describe("transport lifecycle", () => {
161151
}).not.toThrow();
162152
});
163153
it("fails unsupported compressed responses instead of returning corrupted asset bytes", async () => {
164-
mocks.lookup.mockResolvedValue([{ address: "8.8.8.8", family: 4 }]);
154+
mocks.resolve4.mockResolvedValue(["8.8.8.8"]);
165155
const incoming = Object.assign(new PassThrough(), {
166156
statusCode: 200,
167157
headers: { "content-encoding": "gzip" },

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { lookup } from "node:dns/promises";
1+
import { promises as dns } from "node:dns";
22
import { isIP } from "node:net";
33
import { request as httpRequest, type IncomingMessage, type ClientRequest } from "node:http";
44
import { request as httpsRequest } from "node:https";
55

6+
const DNS_TIMEOUT_MS = 10_000;
7+
68
function ipv4Number(address: string): number {
79
return address.split(".").reduce((value, octet) => value * 256 + Number(octet), 0);
810
}
@@ -50,24 +52,20 @@ export async function resolvePublicAddress(hostname: string): Promise<string> {
5052
) {
5153
throw new Error("Capture requires a public hostname");
5254
}
53-
let timer: ReturnType<typeof setTimeout> | undefined;
55+
const cancellableResolver = new dns.Resolver();
56+
const timer = setTimeout(() => cancellableResolver.cancel(), DNS_TIMEOUT_MS);
5457
try {
55-
const answers = await Promise.race([
56-
lookup(host, { all: true, family: 4, verbatim: true }),
57-
new Promise<never>((_resolve, reject) => {
58-
timer = setTimeout(() => reject(new Error("Capture DNS timed out")), 10_000);
59-
timer.unref();
60-
}),
61-
]);
62-
if (
63-
!answers[0] ||
64-
answers.some((answer) => answer.family !== 4 || !isPublicIPv4(answer.address))
65-
) {
58+
const answers = await cancellableResolver.resolve4(host);
59+
const first = answers[0];
60+
if (!first || !answers.every(isPublicIPv4)) {
6661
throw new Error("Capture DNS returned a non-public address");
6762
}
68-
const first = answers[0];
69-
if (!first) throw new Error("Capture DNS returned no address");
70-
return first.address;
63+
return first;
64+
} catch (error) {
65+
if ((error as NodeJS.ErrnoException).code === "ECANCELLED") {
66+
throw new Error("Capture DNS timed out", { cause: error });
67+
}
68+
throw error;
7169
} finally {
7270
clearTimeout(timer);
7371
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { createSocket } from "node:dgram";
2+
3+
export interface TestDnsServer {
4+
/** `host:port`, for a Resolver's setServers. */
5+
address: string;
6+
close(): void;
7+
}
8+
9+
/** A DNS server: "silent" never replies, "answer" answers A with `ipv4`, "no-aaaa" answers A and drops AAAA. */
10+
export async function startTestDnsServer(
11+
mode: "silent" | "answer" | "no-aaaa",
12+
ipv4 = "127.0.0.1",
13+
): Promise<TestDnsServer> {
14+
const socket = createSocket("udp4");
15+
socket.on("message", (msg, peer) => {
16+
if (mode === "silent") return;
17+
let end = 12;
18+
while (msg[end] !== 0) end += (msg[end] ?? 0) + 1;
19+
end += 5;
20+
const isA = msg.readUInt16BE(end - 4) === 1;
21+
if (mode === "no-aaaa" && !isA) return;
22+
const header = Buffer.alloc(12);
23+
msg.copy(header, 0, 0, 2);
24+
header.writeUInt16BE(0x8180, 2);
25+
header.writeUInt16BE(1, 4);
26+
header.writeUInt16BE(isA ? 1 : 0, 6);
27+
const answer = Buffer.from([0xc0, 0x0c, 0, 1, 0, 1, 0, 0, 0, 60, 0, 4]);
28+
const record = Buffer.concat([answer, Buffer.from(ipv4.split(".").map(Number))]);
29+
socket.send(
30+
Buffer.concat([header, msg.subarray(12, end), ...(isA ? [record] : [])]),
31+
peer.port,
32+
peer.address,
33+
);
34+
});
35+
await new Promise<void>((resolve) => socket.bind(0, "127.0.0.1", resolve));
36+
return { address: `127.0.0.1:${socket.address().port}`, close: () => socket.close() };
37+
}

0 commit comments

Comments
 (0)