Commit c47b8d8781b for nodejs
commit c47b8d8781bc7ac17d363c8cd089ff18573ee482
Author: Matteo Collina <hello@matteocollina.com>
Date: Thu Nov 19 09:20:21 2026 +0100
url: restrict option copying in urlToHttpOptions to URL instances
Only spread own enumerable properties for URL instances created by this
realm's URL constructor. Duck-typed objects accepted by isURL() can no
longer smuggle connection options (socketPath, method, headers, agent)
into a request.
This fixes a footgun where a hostname-validated http.request() could be
redirected to a Unix socket (e.g. the Docker daemon) via a URL-shaped
object, bypassing hostname-based SSRF checks. Extending a genuine URL
object with extra options is preserved.
Refs: https://github.com/nodejs/node/pull/46989
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/67454
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
diff --git a/doc/api/url.md b/doc/api/url.md
index 7759c03a15e..1688042c762 100644
--- a/doc/api/url.md
+++ b/doc/api/url.md
@@ -1517,6 +1517,12 @@ added:
- v15.7.0
- v14.18.0
changes:
+ - version: REPLACEME
+ pr-url: https://github.com/nodejs/node/pull/67454
+ description: The own enumerable properties of the `url` argument are now
+ only carried over when `url` is a WHATWG `URL` instance created
+ by this realm's `URL` constructor, rather than for any
+ duck-typed object.
- version:
- v19.9.0
- v18.17.0
@@ -1545,6 +1551,14 @@ changes:
This utility function converts a URL object into an ordinary options object as
expected by the [`http.request()`][] and [`https.request()`][] APIs.
+If `url` is a WHATWG `URL` instance created by this realm's `URL` constructor,
+the returned object will also contain all of its own enumerable properties. This
+allows a `URL` object to be extended with additional request options (for
+example `url.headers = { 'X-Foo': 'bar' }`). Objects that merely share the
+shape of a `URL` (as accepted by the duck-typed `isURL()` check used by
+`http.request()`) are **not** treated this way, so they cannot smuggle
+connection/transport options such as `socketPath` into the request.
+
```mjs
import { urlToHttpOptions } from 'node:url';
const myURL = new URL('https://a:b@測試?abc#foo');
diff --git a/lib/internal/url.js b/lib/internal/url.js
index 2d1339702b9..95a43b1c53a 100644
--- a/lib/internal/url.js
+++ b/lib/internal/url.js
@@ -1476,7 +1476,12 @@ function urlToHttpOptions(url) {
const { hostname, pathname, port, username, password, search } = url;
const options = {
__proto__: null,
- ...url, // In case the url object was extended by the user.
+ // Only spread own enumerable properties for genuine WHATWG URL instances so
+ // that a URL object can still be extended with additional request options
+ // (e.g. `url.headers = ...`). Arbitrary duck-typed objects accepted by
+ // `isURL()` must not be able to smuggle transport/connection options (e.g.
+ // `socketPath`) into a request and bypass hostname-based SSRF defenses.
+ ...(isURLInstance(url) ? url : undefined),
protocol: url.protocol,
hostname: hostname && hostname[0] === '[' ?
StringPrototypeSlice(hostname, 1, -1) :
diff --git a/test/parallel/test-http-client-request-options-injection.js b/test/parallel/test-http-client-request-options-injection.js
new file mode 100644
index 00000000000..47067f1895b
--- /dev/null
+++ b/test/parallel/test-http-client-request-options-injection.js
@@ -0,0 +1,54 @@
+'use strict';
+
+const common = require('../common');
+const assert = require('node:assert');
+const http = require('node:http');
+const path = require('node:path');
+const tmpdir = require('../common/tmpdir');
+
+// Regression test: an ordinary object that happens to satisfy `isURL()` (truthy
+// `href`/`protocol`, with `auth`/`path` absent) must not be able to smuggle
+// HTTP request options such as `socketPath`, `method`, `headers`, or `agent`
+// through `urlToHttpOptions()`. Doing so would let an attacker redirect an
+// otherwise hostname-validated request to a Unix domain socket (e.g. the
+// Docker daemon) and defeat hostname-based SSRF defenses.
+
+const server = http.createServer(common.mustCall((req, res) => {
+ assert.strictEqual(req.method, 'GET'); // `method` must not be smuggled in.
+ assert.strictEqual(req.headers['x-injected'], undefined); // `headers` too.
+ assert.strictEqual(req.url, '/ping');
+ res.writeHead(200);
+ res.end('pong');
+}));
+
+server.listen(0, common.localhostIPv4, common.mustCall(() => {
+ const { port } = server.address();
+ // A duck-typed object that `isURL()` would accept, but which is not a genuine
+ // WHATWG URL instance.
+ const duckTypedUrl = {
+ href: 'http://127.0.0.1/verify',
+ protocol: 'http:',
+ hostname: '127.0.0.1',
+ pathname: '/ping',
+ port: String(port),
+ username: '',
+ password: '',
+ search: '',
+ hash: '',
+ socketPath: path.join(tmpdir.path, 'should-not-be-used.sock'),
+ method: 'POST',
+ headers: { 'x-injected': 'yes' },
+ agent: false,
+ };
+ const req = http.request(duckTypedUrl, common.mustCall((res) => {
+ let body = '';
+ res.on('data', (chunk) => { body += chunk; });
+ res.on('end', common.mustCall(() => {
+ assert.strictEqual(body, 'pong');
+ server.close();
+ }));
+ }));
+ req.on('error', common.mustNotCall(
+ 'request connected to the injected socketPath instead of the hostname'));
+ req.end();
+}));
diff --git a/test/parallel/test-url-urltooptions.js b/test/parallel/test-url-urltooptions.js
index 8e7e5dc8940..5df48afe042 100644
--- a/test/parallel/test-url-urltooptions.js
+++ b/test/parallel/test-url-urltooptions.js
@@ -36,6 +36,44 @@ assert.strictEqual(copiedOpts.search, undefined);
assert.strictEqual(copiedOpts.hash, undefined);
assert.strictEqual(copiedOpts.href, undefined);
+// A genuine URL instance can still be extended with own enumerable properties
+// (e.g. extra request options) and those properties are carried through.
+const extendedUrl = new URL('http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test');
+extendedUrl.headers = { foo: 'Bar' };
+extendedUrl.method = 'POST';
+const extendedOpts = urlToHttpOptions(extendedUrl);
+assert.deepStrictEqual(extendedOpts.headers, { foo: 'Bar' });
+assert.strictEqual(extendedOpts.method, 'POST');
+
+// A duck-typed object that satisfies `isURL()` (truthy `href` and `protocol`,
+// `auth`/`path` absent) must not be able to smuggle transport/connection
+// options (e.g. `socketPath`) into the request options. Only genuine WHATWG
+// URL instances may carry additional own enumerable properties.
+const duckTypedUrl = {
+ href: 'http://safe-webhook.com/verify',
+ protocol: 'http:',
+ hostname: 'safe-webhook.com',
+ pathname: '/v1.41/version',
+ port: '',
+ username: '',
+ password: '',
+ search: '',
+ hash: '',
+ socketPath: '/var/run/docker.sock',
+ method: 'GET',
+ headers: { 'Content-Type': 'application/json' },
+ agent: false,
+};
+const duckTypedOpts = urlToHttpOptions(duckTypedUrl);
+assert.strictEqual(duckTypedOpts.socketPath, undefined);
+assert.strictEqual(duckTypedOpts.method, undefined);
+assert.strictEqual(duckTypedOpts.headers, undefined);
+assert.strictEqual(duckTypedOpts.agent, undefined);
+// URL-derived fields are still populated from the duck-typed object.
+assert.strictEqual(duckTypedOpts.hostname, 'safe-webhook.com');
+assert.strictEqual(duckTypedOpts.path, '/v1.41/version');
+assert.strictEqual(duckTypedOpts.protocol, 'http:');
+
// Test when passed an invalid argument
assert.throws(() => {
urlToHttpOptions('http://127.0.0.1');