diff --git a/packages/engine/src/services/publicNetwork/captureProxy.test.ts b/packages/engine/src/services/publicNetwork/captureProxy.test.ts index 8675c717f0..2f70711563 100644 --- a/packages/engine/src/services/publicNetwork/captureProxy.test.ts +++ b/packages/engine/src/services/publicNetwork/captureProxy.test.ts @@ -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((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((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((resolve) => free.listen(0, "127.0.0.1", resolve)); diff --git a/packages/engine/src/services/publicNetwork/captureProxy.ts b/packages/engine/src/services/publicNetwork/captureProxy.ts index e36c658069..2cc6c10b9d 100644 --- a/packages/engine/src/services/publicNetwork/captureProxy.ts +++ b/packages/engine/src/services/publicNetwork/captureProxy.ts @@ -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 { + 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 { 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);