From 48aaa382491eb2ae4b12a89d3cca4fe7263434d3 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 20 Feb 2019 20:47:25 -0500 Subject: [PATCH] url: handle quasi-WHATWG URLs in urlToOptions() Backport-PR-URL: https://github.com/nodejs/node/pull/32444 --- lib/internal/url.js | 4 ++-- .../test-whatwg-url-custom-properties.js | 21 +++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 434214a3124eae..946660f784833a 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -1256,13 +1256,13 @@ function domainToUnicode(domain) { function urlToOptions(url) { var options = { protocol: url.protocol, - hostname: url.hostname.startsWith('[') ? + hostname: typeof url.hostname === 'string' && url.hostname.startsWith('[') ? url.hostname.slice(1, -1) : url.hostname, hash: url.hash, search: url.search, pathname: url.pathname, - path: `${url.pathname}${url.search}`, + path: `${url.pathname || ''}${url.search || ''}`, href: url.href }; if (url.port !== '') { diff --git a/test/parallel/test-whatwg-url-custom-properties.js b/test/parallel/test-whatwg-url-custom-properties.js index 4a35215bc4ac96..045955e334d4cc 100644 --- a/test/parallel/test-whatwg-url-custom-properties.js +++ b/test/parallel/test-whatwg-url-custom-properties.js @@ -133,8 +133,8 @@ assert.strictEqual(url.searchParams, oldParams); // Test urlToOptions { - const opts = - urlToOptions(new URL('http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test')); + const urlObj = new URL('http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test'); + const opts = urlToOptions(urlObj); assert.strictEqual(opts instanceof URL, false); assert.strictEqual(opts.protocol, 'http:'); assert.strictEqual(opts.auth, 'user:pass'); @@ -147,6 +147,23 @@ assert.strictEqual(url.searchParams, oldParams); const { hostname } = urlToOptions(new URL('http://[::1]:21')); assert.strictEqual(hostname, '::1'); + + // If a WHATWG URL object is copied, it is possible that the resulting copy + // contains the Symbols that Node uses for brand checking, but not the data + // properties, which are getters. Verify that urlToOptions() can handle such + // a case. + const copiedUrlObj = Object.assign({}, urlObj); + const copiedOpts = urlToOptions(copiedUrlObj); + assert.strictEqual(copiedOpts instanceof URL, false); + assert.strictEqual(copiedOpts.protocol, undefined); + assert.strictEqual(copiedOpts.auth, undefined); + assert.strictEqual(copiedOpts.hostname, undefined); + assert.strictEqual(copiedOpts.port, NaN); + assert.strictEqual(copiedOpts.path, ''); + assert.strictEqual(copiedOpts.pathname, undefined); + assert.strictEqual(copiedOpts.search, undefined); + assert.strictEqual(copiedOpts.hash, undefined); + assert.strictEqual(copiedOpts.href, undefined); } // Test special origins