GeezerVison is showing github.com — url: restrict option copying in urlToHttpOptions to URL instances · nodejs/node@d264d67
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/url.md +14Lines changed: 14 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1517,6 +1517,12 @@ added: | 1517 | 1517 | - v15.7.0 | 1518 | 1518 | - v14.18.0 | 1519 | 1519 | 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. | 1520 | 1526 | - version: | 1521 | 1527 | - v19.9.0 | 1522 | 1528 | - v18.17.0 | @@ -1545,6 +1551,14 @@ changes: | 1545 | 1551 | This utility function converts a URL object into an ordinary options object as | 1546 | 1552 | expected by the [`http.request()`][] and [`https.request()`][] APIs. | 1547 | 1553 |
| | 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 | +
| 1548 | 1562 | ```mjs | 1549 | 1563 | import { urlToHttpOptions } from 'node:url'; | 1550 | 1564 | const myURL = new URL('https://a:b@測試?abc#foo'); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/url.js +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1476,7 +1476,12 @@ function urlToHttpOptions(url) { | 1476 | 1476 | const { hostname, pathname, port, username, password, search } = url; | 1477 | 1477 | const options = { | 1478 | 1478 | __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), | 1480 | 1485 | protocol: url.protocol, | 1481 | 1486 | hostname: hostname && hostname[0] === '[' ? | 1482 | 1487 | StringPrototypeSlice(hostname, 1, -1) : | |
Collapse file Copy file name to clipboard +54Lines changed: 54 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +})); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-url-urltooptions.js +38Lines changed: 38 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,44 @@ assert.strictEqual(copiedOpts.search, undefined); | 36 | 36 | assert.strictEqual(copiedOpts.hash, undefined); | 37 | 37 | assert.strictEqual(copiedOpts.href, undefined); | 38 | 38 |
| | 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 | + | | 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 | + | 39 | 77 | // Test when passed an invalid argument | 40 | 78 | assert.throws(() => { | 41 | 79 | urlToHttpOptions('http://127.0.0.1'); | |
|