Skip to content

Commit c47b8d8

Browse files
authored
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: #46989 Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #67454 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
1 parent cdd0687 commit c47b8d8

4 files changed

Lines changed: 112 additions & 1 deletion

File tree

‎doc/api/url.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,12 @@ added:
15171517
- v15.7.0
15181518
- v14.18.0
15191519
changes:
1520+
- version: REPLACEME
1521+
pr-url: https://github.com/nodejs/node/pull/67454
1522+
description: The own enumerable properties of the `url` argument are now
1523+
only carried over when `url` is a WHATWG `URL` instance created
1524+
by this realm's `URL` constructor, rather than for any
1525+
duck-typed object.
15201526
- version:
15211527
- v19.9.0
15221528
- v18.17.0
@@ -1545,6 +1551,14 @@ changes:
15451551
This utility function converts a URL object into an ordinary options object as
15461552
expected by the [`http.request()`][] and [`https.request()`][] APIs.
15471553
1554+
If `url` is a WHATWG `URL` instance created by this realm's `URL` constructor,
1555+
the returned object will also contain all of its own enumerable properties. This
1556+
allows a `URL` object to be extended with additional request options (for
1557+
example `url.headers = { 'X-Foo': 'bar' }`). Objects that merely share the
1558+
shape of a `URL` (as accepted by the duck-typed `isURL()` check used by
1559+
`http.request()`) are **not** treated this way, so they cannot smuggle
1560+
connection/transport options such as `socketPath` into the request.
1561+
15481562
```mjs
15491563
import { urlToHttpOptions } from 'node:url';
15501564
const myURL = new URL('https://a:b@測試?abc#foo');

‎lib/internal/url.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,12 @@ function urlToHttpOptions(url) {
14761476
const { hostname, pathname, port, username, password, search } = url;
14771477
const options = {
14781478
__proto__: null,
1479-
...url, // In case the url object was extended by the user.
1479+
// Only spread own enumerable properties for genuine WHATWG URL instances so
1480+
// that a URL object can still be extended with additional request options
1481+
// (e.g. `url.headers = ...`). Arbitrary duck-typed objects accepted by
1482+
// `isURL()` must not be able to smuggle transport/connection options (e.g.
1483+
// `socketPath`) into a request and bypass hostname-based SSRF defenses.
1484+
...(isURLInstance(url) ? url : undefined),
14801485
protocol: url.protocol,
14811486
hostname: hostname && hostname[0] === '[' ?
14821487
StringPrototypeSlice(hostname, 1, -1) :
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('node:assert');
5+
const http = require('node:http');
6+
const path = require('node:path');
7+
const tmpdir = require('../common/tmpdir');
8+
9+
// Regression test: an ordinary object that happens to satisfy `isURL()` (truthy
10+
// `href`/`protocol`, with `auth`/`path` absent) must not be able to smuggle
11+
// HTTP request options such as `socketPath`, `method`, `headers`, or `agent`
12+
// through `urlToHttpOptions()`. Doing so would let an attacker redirect an
13+
// otherwise hostname-validated request to a Unix domain socket (e.g. the
14+
// Docker daemon) and defeat hostname-based SSRF defenses.
15+
16+
const server = http.createServer(common.mustCall((req, res) => {
17+
assert.strictEqual(req.method, 'GET'); // `method` must not be smuggled in.
18+
assert.strictEqual(req.headers['x-injected'], undefined); // `headers` too.
19+
assert.strictEqual(req.url, '/ping');
20+
res.writeHead(200);
21+
res.end('pong');
22+
}));
23+
24+
server.listen(0, common.localhostIPv4, common.mustCall(() => {
25+
const { port } = server.address();
26+
// A duck-typed object that `isURL()` would accept, but which is not a genuine
27+
// WHATWG URL instance.
28+
const duckTypedUrl = {
29+
href: 'http://127.0.0.1/verify',
30+
protocol: 'http:',
31+
hostname: '127.0.0.1',
32+
pathname: '/ping',
33+
port: String(port),
34+
username: '',
35+
password: '',
36+
search: '',
37+
hash: '',
38+
socketPath: path.join(tmpdir.path, 'should-not-be-used.sock'),
39+
method: 'POST',
40+
headers: { 'x-injected': 'yes' },
41+
agent: false,
42+
};
43+
const req = http.request(duckTypedUrl, common.mustCall((res) => {
44+
let body = '';
45+
res.on('data', (chunk) => { body += chunk; });
46+
res.on('end', common.mustCall(() => {
47+
assert.strictEqual(body, 'pong');
48+
server.close();
49+
}));
50+
}));
51+
req.on('error', common.mustNotCall(
52+
'request connected to the injected socketPath instead of the hostname'));
53+
req.end();
54+
}));

‎test/parallel/test-url-urltooptions.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,44 @@ assert.strictEqual(copiedOpts.search, undefined);
3636
assert.strictEqual(copiedOpts.hash, undefined);
3737
assert.strictEqual(copiedOpts.href, undefined);
3838

39+
// A genuine URL instance can still be extended with own enumerable properties
40+
// (e.g. extra request options) and those properties are carried through.
41+
const extendedUrl = new URL('http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test');
42+
extendedUrl.headers = { foo: 'Bar' };
43+
extendedUrl.method = 'POST';
44+
const extendedOpts = urlToHttpOptions(extendedUrl);
45+
assert.deepStrictEqual(extendedOpts.headers, { foo: 'Bar' });
46+
assert.strictEqual(extendedOpts.method, 'POST');
47+
48+
// A duck-typed object that satisfies `isURL()` (truthy `href` and `protocol`,
49+
// `auth`/`path` absent) must not be able to smuggle transport/connection
50+
// options (e.g. `socketPath`) into the request options. Only genuine WHATWG
51+
// URL instances may carry additional own enumerable properties.
52+
const duckTypedUrl = {
53+
href: 'http://safe-webhook.com/verify',
54+
protocol: 'http:',
55+
hostname: 'safe-webhook.com',
56+
pathname: '/v1.41/version',
57+
port: '',
58+
username: '',
59+
password: '',
60+
search: '',
61+
hash: '',
62+
socketPath: '/var/run/docker.sock',
63+
method: 'GET',
64+
headers: { 'Content-Type': 'application/json' },
65+
agent: false,
66+
};
67+
const duckTypedOpts = urlToHttpOptions(duckTypedUrl);
68+
assert.strictEqual(duckTypedOpts.socketPath, undefined);
69+
assert.strictEqual(duckTypedOpts.method, undefined);
70+
assert.strictEqual(duckTypedOpts.headers, undefined);
71+
assert.strictEqual(duckTypedOpts.agent, undefined);
72+
// URL-derived fields are still populated from the duck-typed object.
73+
assert.strictEqual(duckTypedOpts.hostname, 'safe-webhook.com');
74+
assert.strictEqual(duckTypedOpts.path, '/v1.41/version');
75+
assert.strictEqual(duckTypedOpts.protocol, 'http:');
76+
3977
// Test when passed an invalid argument
4078
assert.throws(() => {
4179
urlToHttpOptions('http://127.0.0.1');

0 commit comments

Comments
 (0)