Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0017855

Browse filesBrowse files
cjihrigaddaleax
authored andcommitted
url: handle quasi-WHATWG URLs in urlToOptions()
PR-URL: #26226 Refs: #26198 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 0080350 commit 0017855
Copy full SHA for 0017855

File tree

Expand file treeCollapse file tree

2 files changed

+21
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+21
-4
lines changed
Open diff view settings
Collapse file

‎lib/internal/url.js‎

Copy file name to clipboardExpand all lines: lib/internal/url.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,13 +1257,13 @@ function domainToUnicode(domain) {
12571257
function urlToOptions(url) {
12581258
var options = {
12591259
protocol: url.protocol,
1260-
hostname: url.hostname.startsWith('[') ?
1260+
hostname: typeof url.hostname === 'string' && url.hostname.startsWith('[') ?
12611261
url.hostname.slice(1, -1) :
12621262
url.hostname,
12631263
hash: url.hash,
12641264
search: url.search,
12651265
pathname: url.pathname,
1266-
path: `${url.pathname}${url.search}`,
1266+
path: `${url.pathname || ''}${url.search || ''}`,
12671267
href: url.href
12681268
};
12691269
if (url.port !== '') {
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-whatwg-url-custom-properties.js
+19-2Lines changed: 19 additions & 2 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.