diff --git a/deps/undici/src/docs/docs/api/EnvHttpProxyAgent.md b/deps/undici/src/docs/docs/api/EnvHttpProxyAgent.md index a4932de8be79..ab69499518cf 100644 --- a/deps/undici/src/docs/docs/api/EnvHttpProxyAgent.md +++ b/deps/undici/src/docs/docs/api/EnvHttpProxyAgent.md @@ -14,18 +14,27 @@ Uppercase environment variables are also supported: `HTTP_PROXY`, `HTTPS_PROXY`, Arguments: -* **options** `EnvHttpProxyAgentOptions` (optional) - extends the `Agent` options. +* **options** `EnvHttpProxyAgentOptions` (optional) - extends the `ProxyAgent` options, except `uri`. Returns: `EnvHttpProxyAgent` ### Parameter: `EnvHttpProxyAgentOptions` -Extends: [`AgentOptions`](Agent.md#parameter-agentoptions) +Extends: [`ProxyAgentOptions`](ProxyAgent.md#parameter-proxyagentoptions), except `uri` * **httpProxy** `string` (optional) - When set, it will override the `HTTP_PROXY` environment variable. * **httpsProxy** `string` (optional) - When set, it will override the `HTTPS_PROXY` environment variable. * **noProxy** `string` (optional) - When set, it will override the `NO_PROXY` environment variable. +The TLS options for the connection to the target server, given as `requestTls` +or as a `connect` object, apply on every route: direct (no proxy configured), +hosts matching `noProxy`, and proxied through an HTTP or HTTPS proxy. +When both are set they are merged key by key, and keys set in `connect` take +precedence over `requestTls` on every route. `proxyTls` applies only to the connection to the +proxy server, and the target TLS options are never applied to it. When `connect` +is a function it is used as-is on direct routes, where `requestTls` is then not +applied, and it is not used on proxied routes. + Examples: ```js diff --git a/deps/undici/src/lib/api/api-stream.js b/deps/undici/src/lib/api/api-stream.js index fba2266dd7d9..9fbd5523c3be 100644 --- a/deps/undici/src/lib/api/api-stream.js +++ b/deps/undici/src/lib/api/api-stream.js @@ -56,6 +56,9 @@ class StreamHandler extends AsyncResource { this.body = body this.onInfo = onInfo || null this.throwOnError = throwOnError || false + // Set once the factory's writable is tracked: from then on its lifecycle + // owns the callback, which must not be invoked from onError. + this.responseStarted = false if (util.isStream(body)) { body.on('error', (err) => { @@ -134,12 +137,16 @@ class StreamHandler extends AsyncResource { } this.callback = null - this.runInAsyncScope(callback, null, err || null, { opaque, trailers }) + if (callback) { + this.runInAsyncScope(callback, null, err || null, { opaque, trailers }) + } if (err) { abort() } }) + + this.responseStarted = true } res.on('drain', resume) @@ -183,7 +190,7 @@ class StreamHandler extends AsyncResource { if (res) { this.res = null util.destroy(res, err) - } else if (callback) { + } else if (callback && !this.responseStarted) { this.callback = null queueMicrotask(() => { this.runInAsyncScope(callback, null, err, { opaque }) diff --git a/deps/undici/src/lib/dispatcher/client-h1.js b/deps/undici/src/lib/dispatcher/client-h1.js index 7d1fdbdf6e7b..e439e098d986 100644 --- a/deps/undici/src/lib/dispatcher/client-h1.js +++ b/deps/undici/src/lib/dispatcher/client-h1.js @@ -303,10 +303,32 @@ class Parser { finish () { assert(currentParser === null) assert(this.ptr != null) - assert(!this.paused) const { llhttp } = this + // The peer closed the connection. If the body parser was paused by + // backpressure we must finish parsing before signalling EOF, otherwise + // llhttp_finish() would crash (it used to assert !paused) or report a + // half-parsed message. Backpressure is advisory here: onData keeps buffering + // delivered bytes into the response stream, so resume across pauses and + // drain whatever is still buffered on the socket. A Content-Length/chunked + // body reaches on_message_complete during execute(); an EOF-delimited body + // stays paused (its length is unknown) and is completed by llhttp_finish(). + if (this.paused) { + let data + do { + llhttp.llhttp_resume(this.ptr) + this.paused = false + data = this.socket.read() || EMPTY_BUF + this.execute(data) + } while (this.paused && data.length > 0) + + if (this.paused) { + llhttp.llhttp_resume(this.ptr) + this.paused = false + } + } + let ret try { diff --git a/deps/undici/src/lib/dispatcher/env-http-proxy-agent.js b/deps/undici/src/lib/dispatcher/env-http-proxy-agent.js index 897011adbcd6..c70dda30b77a 100644 --- a/deps/undici/src/lib/dispatcher/env-http-proxy-agent.js +++ b/deps/undici/src/lib/dispatcher/env-http-proxy-agent.js @@ -30,18 +30,36 @@ class EnvHttpProxyAgent extends DispatcherBase { const { httpProxy, httpsProxy, noProxy, ...agentOpts } = opts - this[kNoProxyAgent] = new Agent(agentOpts) + // The environment picks the route (direct, NO_PROXY or proxied), so the + // TLS options for the target must apply on every route, merged the same + // way: field by field, `connect` over `requestTls` over `tls`. The direct + // Agent reads them from `connect` and `tls`, a ProxyAgent from + // `requestTls`. A `connect` function is a custom connector: it is used + // as-is on direct routes and cannot be applied to the target of a proxy. + const { connect, tls, requestTls } = agentOpts + const connectOpts = typeof connect === 'function' ? undefined : connect + const tlsOpts = typeof tls === 'object' ? tls : undefined + + this[kNoProxyAgent] = new Agent( + requestTls && typeof connect !== 'function' + ? { ...agentOpts, connect: { ...requestTls, ...connect } } + : agentOpts + ) + + const proxyOpts = connectOpts || tlsOpts + ? { ...agentOpts, requestTls: { ...tlsOpts, ...requestTls, ...connectOpts } } + : agentOpts const HTTP_PROXY = httpProxy ?? process.env.http_proxy ?? process.env.HTTP_PROXY if (HTTP_PROXY) { - this[kHttpProxyAgent] = new ProxyAgent({ ...agentOpts, uri: HTTP_PROXY }) + this[kHttpProxyAgent] = new ProxyAgent({ ...proxyOpts, uri: HTTP_PROXY }) } else { this[kHttpProxyAgent] = this[kNoProxyAgent] } const HTTPS_PROXY = httpsProxy ?? process.env.https_proxy ?? process.env.HTTPS_PROXY if (HTTPS_PROXY) { - this[kHttpsProxyAgent] = new ProxyAgent({ ...agentOpts, uri: HTTPS_PROXY }) + this[kHttpsProxyAgent] = new ProxyAgent({ ...proxyOpts, uri: HTTPS_PROXY }) } else { this[kHttpsProxyAgent] = this[kHttpProxyAgent] } diff --git a/deps/undici/src/lib/dispatcher/proxy-agent.js b/deps/undici/src/lib/dispatcher/proxy-agent.js index fed180389092..4fda6282fe19 100644 --- a/deps/undici/src/lib/dispatcher/proxy-agent.js +++ b/deps/undici/src/lib/dispatcher/proxy-agent.js @@ -114,7 +114,10 @@ class ProxyAgent extends DispatcherBase { : [] this[kRequestTls] = opts.requestTls this[kProxyTls] = opts.proxyTls - this[kProxyHeaders] = opts.headers || {} + // Copy the caller's headers: proxy-authorization is added below, and it + // must not leak into an object the caller shares with other agents (such + // as the HTTP and HTTPS proxies of an EnvHttpProxyAgent) or requests. + this[kProxyHeaders] = { ...opts.headers } this[kTunnelProxy] = proxyTunnel if (opts.auth && opts.token) { diff --git a/deps/undici/src/lib/global.js b/deps/undici/src/lib/global.js index 0c7528fa6531..f4effbfc525f 100644 --- a/deps/undici/src/lib/global.js +++ b/deps/undici/src/lib/global.js @@ -6,6 +6,9 @@ const globalDispatcher = Symbol.for('undici.globalDispatcher.1') const { InvalidArgumentError } = require('./core/errors') const Agent = require('./dispatcher/agent') +// Fallback storage for when globalThis is not extensible (e.g. frozen) +let fallbackDispatcher + if (getGlobalDispatcher() === undefined) { setGlobalDispatcher(new Agent()) } @@ -14,16 +17,27 @@ function setGlobalDispatcher (agent) { if (!agent || typeof agent.dispatch !== 'function') { throw new InvalidArgumentError('Argument agent must implement Agent') } - Object.defineProperty(globalThis, globalDispatcher, { - value: agent, - writable: true, - enumerable: false, - configurable: false - }) + + try { + Object.defineProperty(globalThis, globalDispatcher, { + value: agent, + writable: true, + enumerable: false, + configurable: false + }) + } catch (err) { + // globalThis is not extensible (e.g. Object.freeze(globalThis)) + // Use fallback storage instead + if (err instanceof TypeError) { + fallbackDispatcher = agent + return + } + throw err + } } function getGlobalDispatcher () { - return globalThis[globalDispatcher] + return globalThis[globalDispatcher] ?? fallbackDispatcher } module.exports = { diff --git a/deps/undici/src/lib/handler/redirect-handler.js b/deps/undici/src/lib/handler/redirect-handler.js index 16a7b2150a9d..cd03a1e4078a 100644 --- a/deps/undici/src/lib/handler/redirect-handler.js +++ b/deps/undici/src/lib/handler/redirect-handler.js @@ -116,8 +116,17 @@ class RedirectHandler { // Remove headers referring to the original URL. // By default it is Host only, unless it's a 303 (see below), which removes also all Content-* headers. // https://tools.ietf.org/html/rfc7231#section-6.4 - this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303, this.opts.origin !== origin) + // this.opts.origin may be a URL object or a non-canonical string, while + // origin is always URL.origin, so compare normalized origins. + const crossOrigin = !this.opts.origin || new URL(this.opts.origin).origin !== origin + this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303, crossOrigin) this.opts.path = path + if (crossOrigin && this.opts.servername != null) { + // A TLS servername chosen for the original host must not be used to + // verify the certificate of another origin. Let the next request derive + // it from the new host instead. + this.opts.servername = null + } this.opts.origin = origin this.opts.maxRedirections = 0 this.opts.query = null diff --git a/deps/undici/src/lib/llhttp/wasm_build_env.txt b/deps/undici/src/lib/llhttp/wasm_build_env.txt index beee64378372..f96c1c105ef6 100644 --- a/deps/undici/src/lib/llhttp/wasm_build_env.txt +++ b/deps/undici/src/lib/llhttp/wasm_build_env.txt @@ -1,12 +1,12 @@ -> undici@6.29.1 prebuild:wasm +> undici@6.29.2 prebuild:wasm > node build/wasm.js --prebuild > docker build --platform=linux/x86_64 -t llhttp_wasm_builder -f /home/runner/work/node/node/deps/undici/src/build/Dockerfile /home/runner/work/node/node/deps/undici/src -> undici@6.29.1 build:wasm +> undici@6.29.2 build:wasm > node build/wasm.js --docker > docker run --rm -t --platform=linux/x86_64 --user 1001:1001 --mount type=bind,source=/home/runner/work/node/node/deps/undici/src/lib/llhttp,target=/home/node/undici/lib/llhttp llhttp_wasm_builder node build/wasm.js diff --git a/deps/undici/src/lib/web/websocket/connection.js b/deps/undici/src/lib/web/websocket/connection.js index 1197b9b650c2..7805afe42f3f 100644 --- a/deps/undici/src/lib/web/websocket/connection.js +++ b/deps/undici/src/lib/web/websocket/connection.js @@ -8,7 +8,7 @@ const { kReceivedClose, kResponse } = require('./symbols') -const { fireEvent, failWebsocketConnection, isClosing, isClosed, isEstablished, parseExtensions } = require('./util') +const { fireEvent, failWebsocketConnection, isConnecting, isClosing, isClosed, isEstablished, parseExtensions } = require('./util') const { channels } = require('../../core/diagnostics') const { CloseEvent } = require('./events') const { makeRequest } = require('../fetch/request') @@ -107,7 +107,10 @@ function establishWebSocketConnection (url, protocols, client, ws, onEstablish, // 1. If response is a network error or its status is not 101, // fail the WebSocket connection. if (response.type === 'error' || response.status !== 101) { - failWebsocketConnection(ws, 'Received network error or non-101 status code.') + // close() aborts the fetch after it has already failed the connection. + if (isConnecting(ws)) { + failWebsocketConnection(ws, 'Received network error or non-101 status code.') + } return } diff --git a/deps/undici/src/lib/web/websocket/util.js b/deps/undici/src/lib/web/websocket/util.js index 2a04887f2632..5f1a8d67b531 100644 --- a/deps/undici/src/lib/web/websocket/util.js +++ b/deps/undici/src/lib/web/websocket/util.js @@ -2,7 +2,7 @@ const { kReadyState, kController, kResponse, kBinaryType, kWebSocketURL } = require('./symbols') const { states, opcodes } = require('./constants') -const { ErrorEvent, createFastMessageEvent } = require('./events') +const { ErrorEvent, CloseEvent, createFastMessageEvent } = require('./events') const { isUtf8 } = require('node:buffer') const { collectASequenceOfCodePointsFast, removeHTTPWhitespace } = require('../fetch/data-url') @@ -202,6 +202,14 @@ function failWebsocketConnection (ws, reason) { response.socket.destroy() } + // A connection that was never established has no socket close listener, + // so the connection is closed here instead of in onSocketClose. + const wasConnecting = isConnecting(ws) + + if (wasConnecting) { + ws[kReadyState] = states.CLOSED + } + if (reason) { // TODO: process.nextTick fireEvent('error', ws, (type, init) => new ErrorEvent(type, init), { @@ -209,6 +217,13 @@ function failWebsocketConnection (ws, reason) { message: reason }) } + + if (wasConnecting) { + fireEvent('close', ws, (type, init) => new CloseEvent(type, init), { + wasClean: false, + code: 1006 + }) + } } /** diff --git a/deps/undici/src/package.json b/deps/undici/src/package.json index 4bccc5455b89..50d8afb5b1a1 100644 --- a/deps/undici/src/package.json +++ b/deps/undici/src/package.json @@ -1,6 +1,6 @@ { "name": "undici", - "version": "6.29.1", + "version": "6.29.2", "description": "An HTTP/1.1 client, written from scratch for Node.js", "homepage": "https://undici.nodejs.org", "bugs": { diff --git a/deps/undici/src/types/env-http-proxy-agent.d.ts b/deps/undici/src/types/env-http-proxy-agent.d.ts index d6509dc673b4..25970173d867 100644 --- a/deps/undici/src/types/env-http-proxy-agent.d.ts +++ b/deps/undici/src/types/env-http-proxy-agent.d.ts @@ -1,4 +1,5 @@ import Agent from './agent' +import ProxyAgent from './proxy-agent' import Dispatcher from './dispatcher' export default EnvHttpProxyAgent @@ -10,7 +11,7 @@ declare class EnvHttpProxyAgent extends Dispatcher { } declare namespace EnvHttpProxyAgent { - export interface Options extends Agent.Options { + export interface Options extends Omit { /** Overrides the value of the HTTP_PROXY environment variable */ httpProxy?: string; /** Overrides the value of the HTTPS_PROXY environment variable */ diff --git a/deps/undici/undici.js b/deps/undici/undici.js index fbabc70fe2a5..184ba2126876 100644 --- a/deps/undici/undici.js +++ b/deps/undici/undici.js @@ -6226,8 +6226,20 @@ var require_client_h1 = __commonJS({ finish() { assert(currentParser === null); assert(this.ptr != null); - assert(!this.paused); const { llhttp } = this; + if (this.paused) { + let data; + do { + llhttp.llhttp_resume(this.ptr); + this.paused = false; + data = this.socket.read() || EMPTY_BUF; + this.execute(data); + } while (this.paused && data.length > 0); + if (this.paused) { + llhttp.llhttp_resume(this.ptr); + this.paused = false; + } + } let ret; try { currentParser = this; @@ -7809,8 +7821,12 @@ var require_redirect_handler = __commonJS({ } const { origin, pathname, search } = util.parseURL(new URL(this.location, this.opts.origin && new URL(this.opts.path, this.opts.origin))); const path = search ? `${pathname}${search}` : pathname; - this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303, this.opts.origin !== origin); + const crossOrigin = !this.opts.origin || new URL(this.opts.origin).origin !== origin; + this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303, crossOrigin); this.opts.path = path; + if (crossOrigin && this.opts.servername != null) { + this.opts.servername = null; + } this.opts.origin = origin; this.opts.maxRedirections = 0; this.opts.query = null; @@ -8629,6 +8645,7 @@ var require_global2 = __commonJS({ var globalDispatcher = Symbol.for("undici.globalDispatcher.1"); var { InvalidArgumentError } = require_errors(); var Agent = require_agent(); + var fallbackDispatcher; if (getGlobalDispatcher2() === void 0) { setGlobalDispatcher2(new Agent()); } @@ -8636,16 +8653,24 @@ var require_global2 = __commonJS({ if (!agent || typeof agent.dispatch !== "function") { throw new InvalidArgumentError("Argument agent must implement Agent"); } - Object.defineProperty(globalThis, globalDispatcher, { - value: agent, - writable: true, - enumerable: false, - configurable: false - }); + try { + Object.defineProperty(globalThis, globalDispatcher, { + value: agent, + writable: true, + enumerable: false, + configurable: false + }); + } catch (err) { + if (err instanceof TypeError) { + fallbackDispatcher = agent; + return; + } + throw err; + } } __name(setGlobalDispatcher2, "setGlobalDispatcher"); function getGlobalDispatcher2() { - return globalThis[globalDispatcher]; + return globalThis[globalDispatcher] ?? fallbackDispatcher; } __name(getGlobalDispatcher2, "getGlobalDispatcher"); module2.exports = { @@ -8760,7 +8785,7 @@ var require_proxy_agent = __commonJS({ this[kInterceptors] = opts.interceptors?.ProxyAgent && Array.isArray(opts.interceptors.ProxyAgent) ? opts.interceptors.ProxyAgent : []; this[kRequestTls] = opts.requestTls; this[kProxyTls] = opts.proxyTls; - this[kProxyHeaders] = opts.headers || {}; + this[kProxyHeaders] = { ...opts.headers }; this[kTunnelProxy] = proxyTunnel; if (opts.auth && opts.token) { throw new InvalidArgumentError("opts.auth cannot be used in combination with opts.token"); @@ -8920,16 +8945,22 @@ var require_env_http_proxy_agent = __commonJS({ }); } const { httpProxy, httpsProxy, noProxy, ...agentOpts } = opts; - this[kNoProxyAgent] = new Agent(agentOpts); + const { connect, tls, requestTls } = agentOpts; + const connectOpts = typeof connect === "function" ? void 0 : connect; + const tlsOpts = typeof tls === "object" ? tls : void 0; + this[kNoProxyAgent] = new Agent( + requestTls && typeof connect !== "function" ? { ...agentOpts, connect: { ...requestTls, ...connect } } : agentOpts + ); + const proxyOpts = connectOpts || tlsOpts ? { ...agentOpts, requestTls: { ...tlsOpts, ...requestTls, ...connectOpts } } : agentOpts; const HTTP_PROXY = httpProxy ?? process.env.http_proxy ?? process.env.HTTP_PROXY; if (HTTP_PROXY) { - this[kHttpProxyAgent] = new ProxyAgent({ ...agentOpts, uri: HTTP_PROXY }); + this[kHttpProxyAgent] = new ProxyAgent({ ...proxyOpts, uri: HTTP_PROXY }); } else { this[kHttpProxyAgent] = this[kNoProxyAgent]; } const HTTPS_PROXY = httpsProxy ?? process.env.https_proxy ?? process.env.HTTPS_PROXY; if (HTTPS_PROXY) { - this[kHttpsProxyAgent] = new ProxyAgent({ ...agentOpts, uri: HTTPS_PROXY }); + this[kHttpsProxyAgent] = new ProxyAgent({ ...proxyOpts, uri: HTTPS_PROXY }); } else { this[kHttpsProxyAgent] = this[kHttpProxyAgent]; } @@ -12087,7 +12118,7 @@ var require_util3 = __commonJS({ "use strict"; var { kReadyState, kController, kResponse, kBinaryType, kWebSocketURL } = require_symbols3(); var { states, opcodes } = require_constants4(); - var { ErrorEvent: ErrorEvent2, createFastMessageEvent: createFastMessageEvent2 } = require_events(); + var { ErrorEvent: ErrorEvent2, CloseEvent: CloseEvent2, createFastMessageEvent: createFastMessageEvent2 } = require_events(); var { isUtf8 } = require("node:buffer"); var { collectASequenceOfCodePointsFast, removeHTTPWhitespace } = require_data_url(); function isConnecting(ws) { @@ -12188,12 +12219,22 @@ var require_util3 = __commonJS({ if (response?.socket && !response.socket.destroyed) { response.socket.destroy(); } + const wasConnecting = isConnecting(ws); + if (wasConnecting) { + ws[kReadyState] = states.CLOSED; + } if (reason) { fireEvent("error", ws, (type, init) => new ErrorEvent2(type, init), { error: new Error(reason), message: reason }); } + if (wasConnecting) { + fireEvent("close", ws, (type, init) => new CloseEvent2(type, init), { + wasClean: false, + code: 1006 + }); + } } __name(failWebsocketConnection, "failWebsocketConnection"); function isControlFrame(opcode) { @@ -12363,7 +12404,7 @@ var require_connection = __commonJS({ kReceivedClose, kResponse } = require_symbols3(); - var { fireEvent, failWebsocketConnection, isClosing, isClosed, isEstablished, parseExtensions } = require_util3(); + var { fireEvent, failWebsocketConnection, isConnecting, isClosing, isClosed, isEstablished, parseExtensions } = require_util3(); var { channels } = require_diagnostics(); var { CloseEvent: CloseEvent2 } = require_events(); var { makeRequest } = require_request2(); @@ -12407,7 +12448,9 @@ var require_connection = __commonJS({ dispatcher: options.dispatcher, processResponse(response) { if (response.type === "error" || response.status !== 101) { - failWebsocketConnection(ws, "Received network error or non-101 status code."); + if (isConnecting(ws)) { + failWebsocketConnection(ws, "Received network error or non-101 status code."); + } return; } if (protocols.length !== 0 && !response.headersList.get("Sec-WebSocket-Protocol")) { @@ -14911,6 +14954,7 @@ var require_api_stream = __commonJS({ this.body = body; this.onInfo = onInfo || null; this.throwOnError = throwOnError || false; + this.responseStarted = false; if (util.isStream(body)) { body.on("error", (err) => { this.onError(err); @@ -14968,11 +15012,14 @@ var require_api_stream = __commonJS({ util.destroy(res2, err); } this.callback = null; - this.runInAsyncScope(callback2, null, err || null, { opaque: opaque2, trailers }); + if (callback2) { + this.runInAsyncScope(callback2, null, err || null, { opaque: opaque2, trailers }); + } if (err) { abort(); } }); + this.responseStarted = true; } res.on("drain", resume); this.res = res; @@ -14999,7 +15046,7 @@ var require_api_stream = __commonJS({ if (res) { this.res = null; util.destroy(res, err); - } else if (callback) { + } else if (callback && !this.responseStarted) { this.callback = null; queueMicrotask(() => { this.runInAsyncScope(callback, null, err, { opaque }); diff --git a/src/undici_version.h b/src/undici_version.h index 84431a7e5615..0825f1246419 100644 --- a/src/undici_version.h +++ b/src/undici_version.h @@ -2,5 +2,5 @@ // Refer to tools/dep_updaters/update-undici.sh #ifndef SRC_UNDICI_VERSION_H_ #define SRC_UNDICI_VERSION_H_ -#define UNDICI_VERSION "6.29.1" +#define UNDICI_VERSION "6.29.2" #endif // SRC_UNDICI_VERSION_H_