Skip to content

Commit 4390674

Browse files
cjihrigBethGriggs
authored andcommitted
url: handle quasi-WHATWG URLs in urlToOptions()
PR-URL: #26226 Backport-PR-URL: #32446 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 89692ff commit 4390674

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/internal/url.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1256,13 +1256,13 @@ function domainToUnicode(domain) {
12561256
function urlToOptions(url) {
12571257
var options = {
12581258
protocol: url.protocol,
1259-
hostname: url.hostname.startsWith('[') ?
1259+
hostname: typeof url.hostname === 'string' && url.hostname.startsWith('[') ?
12601260
url.hostname.slice(1, -1) :
12611261
url.hostname,
12621262
hash: url.hash,
12631263
search: url.search,
12641264
pathname: url.pathname,
1265-
path: `${url.pathname}${url.search}`,
1265+
path: `${url.pathname || ''}${url.search || ''}`,
12661266
href: url.href
12671267
};
12681268
if (url.port !== '') {

test/parallel/test-whatwg-url-custom-properties.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ assert.strictEqual(url.searchParams, oldParams);
133133

134134
// Test urlToOptions
135135
{
136-
const opts =
137-
urlToOptions(new URL('http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test'));
136+
const urlObj = new URL('http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test');
137+
const opts = urlToOptions(urlObj);
138138
assert.strictEqual(opts instanceof URL, false);
139139
assert.strictEqual(opts.protocol, 'http:');
140140
assert.strictEqual(opts.auth, 'user:pass');
@@ -147,6 +147,23 @@ assert.strictEqual(url.searchParams, oldParams);
147147

148148
const { hostname } = urlToOptions(new URL('http://[::1]:21'));
149149
assert.strictEqual(hostname, '::1');
150+
151+
// If a WHATWG URL object is copied, it is possible that the resulting copy
152+
// contains the Symbols that Node uses for brand checking, but not the data
153+
// properties, which are getters. Verify that urlToOptions() can handle such
154+
// a case.
155+
const copiedUrlObj = Object.assign({}, urlObj);
156+
const copiedOpts = urlToOptions(copiedUrlObj);
157+
assert.strictEqual(copiedOpts instanceof URL, false);
158+
assert.strictEqual(copiedOpts.protocol, undefined);
159+
assert.strictEqual(copiedOpts.auth, undefined);
160+
assert.strictEqual(copiedOpts.hostname, undefined);
161+
assert.strictEqual(copiedOpts.port, NaN);
162+
assert.strictEqual(copiedOpts.path, '');
163+
assert.strictEqual(copiedOpts.pathname, undefined);
164+
assert.strictEqual(copiedOpts.search, undefined);
165+
assert.strictEqual(copiedOpts.hash, undefined);
166+
assert.strictEqual(copiedOpts.href, undefined);
150167
}
151168

152169
// Test special origins

0 commit comments

Comments
 (0)