Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down
7 changes: 6 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,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) :
Expand Down
54 changes: 54 additions & 0 deletions test/parallel/test-http-client-request-options-injection.js
Original file line number Diff line number Diff line change
@@ -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();
}));
38 changes: 38 additions & 0 deletions test/parallel/test-url-urltooptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading