fix(engine): a capture proxy request whose name will not resolve is reported as failed (#8409)

This commit is contained in:
Miguel Ángel
2026-11-21 23:40:18 +00:00
committed by GitHub
parent c3a6739903
commit 75c83d8772
2 changed files with 52 additions and 4 deletions
@@ -112,6 +112,45 @@ describe("capture proxy", () => {
expect(refused).toEqual([]);
});
// fails without the fix: a name that would not resolve answered 403 and reported nothing, refused or failed.
it("reports a name that would not resolve as a failed request, plain and tunneled", async () => {
const failures: CaptureFailure[] = [];
const refused: string[] = [];
const proxy = await startCaptureProxy(null, (c) => refused.push(c), undefined, {
onFailed: (failure) => failures.push(failure),
});
proxies.push(proxy);
const plain = (path: string) =>
new Promise<number>((resolve, reject) => {
const req = request(proxy.url, { path }, (res) => {
res.resume();
res.on("end", () => resolve(res.statusCode!));
});
req.on("error", reject);
req.end();
});
const tunnel = (authority: string) =>
new Promise<string>((resolve) => {
const socket = connect(Number(new URL(proxy.url).port), "127.0.0.1");
socket.write(`CONNECT ${authority} HTTP/1.1\r\nHost: ${authority}\r\n\r\n`);
socket.once("data", (data) => {
resolve(String(data).split("\r\n")[0]!);
socket.destroy();
});
});
resolveAddress.mockRejectedValue(new Error("DNS timed out"));
expect(await plain("http://slow.example.com/")).toBe(403);
expect(await tunnel("slow.example.com:443")).toBe("HTTP/1.1 403 Forbidden");
expect(failures.map((failure) => failure.reason)).toEqual([
"upstream_failed",
"upstream_failed",
]);
resolveAddress.mockRejectedValue(new NonPublicHostError("refused", "private"));
expect(await plain("http://inside.example.com/")).toBe(403);
expect(failures).toHaveLength(2);
expect(refused).toEqual(["private"]);
});
it("listens on the fixed loopback port it is given", async () => {
const free = createServer();
await new Promise<void>((resolve) => free.listen(0, "127.0.0.1", resolve));
@@ -90,9 +90,18 @@ function refuse(response: ServerResponse): void {
response.end();
}
async function httpPeer(url: URL, proxy: UpstreamProxy | undefined) {
async function resolveOrReportFailure(hostname: string, fail: () => void): Promise<string> {
try {
return await resolvePublicAddress(hostname);
} catch (error) {
if (!(error instanceof NonPublicHostError)) fail();
throw error;
}
}
async function httpPeer(url: URL, proxy: UpstreamProxy | undefined, fail: () => void) {
if (!proxy) {
const hostname = await resolvePublicAddress(url.hostname);
const hostname = await resolveOrReportFailure(url.hostname, fail);
return { hostname, family: 4, port: url.port || 80, path: url.pathname + url.search };
}
assertPublicHostname(url.hostname);
@@ -109,7 +118,7 @@ async function forwardHttp(
): Promise<void> {
try {
const url = destination(incoming.url ?? "", false);
const peer = await httpPeer(url, proxy);
const peer = await httpPeer(url, proxy, fail);
if (incoming.destroyed || response.destroyed) return;
const headers = forwardHeaders(incoming);
headers.host = url.host;
@@ -198,7 +207,7 @@ async function forwardTunnel(
assertPublicHostname(url.hostname);
return await forwardTunnelViaUpstream(url, proxy, client, head, sockets, fail);
}
const address = await resolvePublicAddress(url.hostname);
const address = await resolveOrReportFailure(url.hostname, fail);
if (client.destroyed) return;
const upstream = connect({ host: address, family: 4, port: Number(url.port || 443) });
trackSocket(upstream, sockets);