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 5cb8b16

Browse filesBrowse files
Lxxyxdanielleadams
authored andcommitted
url: fix url.format with ipv6 hostname
Fixes: #36654 PR-URL: #36665 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Yash Ladha <yash@yashladha.in>
1 parent f0dfe57 commit 5cb8b16
Copy full SHA for 5cb8b16

File tree

Expand file treeCollapse file tree

2 files changed

+17
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+17
-3
lines changed
Open diff view settings
Collapse file

‎lib/url.js‎

Copy file name to clipboardExpand all lines: lib/url.js
+11-3Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {
2626
ObjectCreate,
2727
ObjectKeys,
2828
SafeSet,
29+
StringPrototypeCharCodeAt,
2930
} = primordials;
3031

3132
const { toASCII } = require('internal/idna');
@@ -156,6 +157,14 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
156157
return urlObject;
157158
}
158159

160+
function isIpv6Hostname(hostname) {
161+
return (
162+
StringPrototypeCharCodeAt(hostname, 0) === CHAR_LEFT_SQUARE_BRACKET &&
163+
StringPrototypeCharCodeAt(hostname, hostname.length - 1) ===
164+
CHAR_RIGHT_SQUARE_BRACKET
165+
);
166+
}
167+
159168
Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
160169
validateString(url, 'url');
161170

@@ -364,8 +373,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
364373

365374
// If hostname begins with [ and ends with ]
366375
// assume that it's an IPv6 address.
367-
const ipv6Hostname = hostname.charCodeAt(0) === CHAR_LEFT_SQUARE_BRACKET &&
368-
hostname.charCodeAt(hostname.length - 1) === CHAR_RIGHT_SQUARE_BRACKET;
376+
const ipv6Hostname = isIpv6Hostname(hostname);
369377

370378
// validate a little.
371379
if (!ipv6Hostname) {
@@ -590,7 +598,7 @@ Url.prototype.format = function format() {
590598
host = auth + this.host;
591599
} else if (this.hostname) {
592600
host = auth + (
593-
this.hostname.includes(':') ?
601+
this.hostname.includes(':') && !isIpv6Hostname(this.hostname) ?
594602
'[' + this.hostname + ']' :
595603
this.hostname
596604
);
Collapse file

‎test/parallel/test-url-format.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-url-format.js
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ const formatTests = {
148148
host: '[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:61616',
149149
pathname: '/s/stopButton'
150150
},
151+
'http://[::]/': {
152+
href: 'http://[::]/',
153+
protocol: 'http:',
154+
hostname: '[::]',
155+
pathname: '/'
156+
},
151157

152158
// Encode context-specific delimiters in path and query, but do not touch
153159
// other non-delimiter chars like `%`.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.