Skip to content

Commit d677423

Browse files
committed
node:http: destroy the proxy socket on non-200 CONNECT failures
cleanupAndPropagate deliberately skips destroy for status-code tunnel errors ('let req.onSocket handle the destruction'), but oncreate's error path dropped the socket, so onSocketNT never reached its destroy and a proxy holding its end open after a 407/502 leaked an idle ref'd connection. Forward the socket through oncreate (and the pending-queue variant) so the existing onSocketNT destroy path runs. Test: a proxy answering CONNECT with 407 and holding the connection open sees the client close its end.
1 parent 2be259b commit d677423

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎src/js/node/_http_agent.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
278278
options.encoding = null;
279279

280280
const oncreate = once((err, s) => {
281-
if (err) return cb(err);
281+
// Pass the socket along with the error: proxy-tunnel failures with a
282+
// statusCode deliberately skip destroy in cleanupAndPropagate so
283+
// req.onSocket can destroy the connection - dropping it here would leak
284+
// a proxy socket that holds its end open after a non-200 CONNECT.
285+
if (err) return cb(err, s);
282286
this.sockets[name] ||= [];
283287
this.sockets[name].push(s);
284288
this.totalSocketCount++;
@@ -424,7 +428,9 @@ function onSocketCreated(this: any, req, err, socket) {
424428
function onSocketCreatedForPending(req, err, socket) {
425429
if (err) {
426430
handleSocketAfterProxy(err, req);
427-
req.onSocket(null, err);
431+
// Forward the socket (when the creation error left one behind) so
432+
// onSocketNT can destroy it, like the non-pending path.
433+
req.onSocket(socket ?? null, err);
428434
return;
429435
}
430436

‎test/js/node/http/node-http.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,3 +3400,41 @@ it("the over-limit 503 advertises Connection: close, not keep-alive", async () =
34003400
server.close();
34013401
}
34023402
});
3403+
3404+
it("a non-200 CONNECT through a proxy that holds the connection open is destroyed client-side", async () => {
3405+
// cleanupAndPropagate deliberately defers destroy to req.onSocket for
3406+
// status-code tunnel failures; oncreate must forward the socket so
3407+
// onSocketNT actually destroys it - otherwise the proxy connection leaks
3408+
// until the proxy closes its side.
3409+
const proxySockets: import("node:net").Socket[] = [];
3410+
const proxy = createNetServer(socket => {
3411+
proxySockets.push(socket);
3412+
socket.on("error", () => {});
3413+
// Reply 407 and HOLD the connection open (no end()).
3414+
socket.once("data", () => {
3415+
socket.write("HTTP/1.1 407 Proxy Authentication Required\r\nContent-Length: 0\r\n\r\n");
3416+
});
3417+
});
3418+
try {
3419+
proxy.listen(0, "127.0.0.1");
3420+
await once(proxy, "listening");
3421+
const proxyPort = (proxy.address() as AddressInfo).port;
3422+
3423+
const agent = new https.Agent({ proxyEnv: { HTTPS_PROXY: `http://127.0.0.1:${proxyPort}` } });
3424+
const { promise: errored, resolve: onError } = Promise.withResolvers<any>();
3425+
const req = https.request({ host: "example.com", port: 443, path: "/", agent }, () => {});
3426+
req.on("error", onError);
3427+
req.end();
3428+
3429+
const err = await errored;
3430+
expect(err.code).toBe("ERR_PROXY_TUNNEL");
3431+
expect(err.statusCode).toBe(407);
3432+
// The client must close its end despite the proxy holding the socket.
3433+
expect(proxySockets.length).toBe(1);
3434+
await once(proxySockets[0], "close");
3435+
agent.destroy();
3436+
} finally {
3437+
for (const s of proxySockets) s.destroy();
3438+
proxy.close();
3439+
}
3440+
});

0 commit comments

Comments
 (0)