diff --git a/packages/engine/src/utils/urlDownloader.restrictedNetwork.test.ts b/packages/engine/src/utils/urlDownloader.restrictedNetwork.test.ts index 3d49219a84..7f3679892e 100644 --- a/packages/engine/src/utils/urlDownloader.restrictedNetwork.test.ts +++ b/packages/engine/src/utils/urlDownloader.restrictedNetwork.test.ts @@ -293,6 +293,7 @@ describe("downloads with the render-service setting off", () => { describe("downloads on a render service through the egress proxy in HYPERFRAMES_CAPTURE_PROXY", () => { const HOST = "cdn.egress-assets.com"; + const HOST_WHOSE_TUNNEL_THE_PROXY_DROPS = "gone.egress-assets.com"; const tunnels: string[] = []; const defaultCAs = getCACertificates("default"); let origin: Server; @@ -316,6 +317,7 @@ describe("downloads on a render service through the egress proxy in HYPERFRAMES_ egress = createServer(); egress.on("connect", (request, client) => { tunnels.push(request.url ?? ""); + if (request.url === `${HOST_WHOSE_TUNNEL_THE_PROXY_DROPS}:443`) return void client.destroy(); const upstream = connect(originPort, "127.0.0.1", () => { client.write("HTTP/1.1 200 Connection Established\r\n\r\n"); upstream.pipe(client).pipe(upstream); @@ -340,6 +342,12 @@ describe("downloads on a render service through the egress proxy in HYPERFRAMES_ ); }); + it("a tunnel the proxy drops fails the download without opening it again and again", async () => { + const url = `https://${HOST_WHOSE_TUNNEL_THE_PROXY_DROPS}/a.png`; + await expect(downloadToTemp(url, tempDir())).rejects.toMatchObject({ kind: "network" }); + expect(tunnels).toEqual([0, 1].map(() => `${HOST_WHOSE_TUNNEL_THE_PROXY_DROPS}:443`)); + }, 10_000); + it("downloads by hostname through the proxy, with the usual headers, resolving no names", async () => { routes[`https://${HOST}/a.png`] = { status: 200, body: BODY }; const path = await downloadToTemp(`https://${HOST}/a.png`, tempDir()); diff --git a/packages/engine/src/utils/urlDownloader.ts b/packages/engine/src/utils/urlDownloader.ts index d0cd0d8c2e..d0c17aa604 100644 --- a/packages/engine/src/utils/urlDownloader.ts +++ b/packages/engine/src/utils/urlDownloader.ts @@ -20,6 +20,7 @@ import { createHash, randomUUID } from "crypto"; import dns from "node:dns"; import { fetchGoesThroughEnvProxy } from "@hyperframes/core/env-proxy"; import { type LookupFunction } from "node:net"; +import type { buildConnector } from "undici"; import { isBlockedNetworkHost } from "./networkPolicy.js"; import { resolveRestrictPageNetwork } from "../config.js"; import { @@ -515,16 +516,44 @@ function undiciTransport(lookup: LookupFunction): Promise { const proxyTransports = new Map>(); +/** undici 7 reopens a tunnel the proxy closed without a reply with no pause or limit; this fails the request. */ +function failRequestOnDroppedTunnel(connect: buildConnector.connector): buildConnector.connector { + return (options, callback) => + connect(options, (...result) => { + const [error] = result; + if (error && "code" in error && error.code === "UND_ERR_SOCKET") { + const closed = new Error("The egress proxy closed the tunnel", { cause: error }); + return callback(Object.assign(closed, { code: "ECONNRESET" }), null); + } + callback(...result); + }); +} + function egressProxyTransport(proxy: UpstreamProxy): Promise { const uri = `http://${proxy.host.includes(":") ? `[${proxy.host}]` : proxy.host}:${proxy.port}`; const key = `${uri} ${proxy.authorization ?? ""}`; let transport = proxyTransports.get(key); if (!transport) { - transport = importUndiciPackageEvenUnderBun().then(({ ProxyAgent, fetch: undiciFetch }) => { - const dispatcher = new ProxyAgent({ uri, token: proxy.authorization }); - return (target, options) => - undiciFetch(target, { ...(options as object), dispatcher }) as unknown as Promise; - }); + transport = importUndiciPackageEvenUnderBun().then( + ({ Pool, ProxyAgent, fetch: undiciFetch }) => { + const dispatcher = new ProxyAgent({ + uri, + token: proxy.authorization, + factory: (origin, options) => + new Pool(origin, { + ...options, + connect: failRequestOnDroppedTunnel( + (options as { connect: buildConnector.connector }).connect, + ), + }), + }); + return (target, options) => + undiciFetch(target, { + ...(options as object), + dispatcher, + }) as unknown as Promise; + }, + ); proxyTransports.set(key, transport); } return transport;