Skip to content

Commit 9aeda47

Browse files
Trottsxa
authored andcommitted
url: fix url.parse() for @hostname
Make url.parse() behave more like browsers and WHATWHG URL when dealing with URLs that of the format `http:@example.com`. This is the same as `http://example.com`. This issue was reported by P0cas. https://github.com/P0cas PR-URL: #42136 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent ee02739 commit 9aeda47

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

lib/url.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -294,22 +294,29 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
294294
rest = rest.slice(proto.length);
295295
}
296296

297-
// Figure out if it's got a host
298-
// user@server is *always* interpreted as a hostname, and url
297+
// Figure out if it's got a host.
298+
// user@server is *always* interpreted as a hostname, and URL
299299
// resolution will treat //foo/bar as host=foo,path=bar because that's
300-
// how the browser resolves relative URLs.
300+
// how the browser resolves relative URLs. http:@example.com is treated
301+
// the same as http://example.com.
301302
let slashes;
303+
let at;
302304
if (slashesDenoteHost || proto || hostPattern.test(rest)) {
303305
slashes = rest.charCodeAt(0) === CHAR_FORWARD_SLASH &&
304-
rest.charCodeAt(1) === CHAR_FORWARD_SLASH;
305-
if (slashes && !(proto && hostlessProtocol.has(lowerProto))) {
306-
rest = rest.slice(2);
307-
this.slashes = true;
306+
rest.charCodeAt(1) === CHAR_FORWARD_SLASH;
307+
at = rest.charCodeAt(0) === CHAR_AT;
308+
if (!(proto && hostlessProtocol.has(lowerProto))) {
309+
if (slashes) {
310+
rest = rest.slice(2);
311+
this.slashes = true;
312+
} else if (at) {
313+
rest = rest.slice(1);
314+
}
308315
}
309316
}
310317

311318
if (!hostlessProtocol.has(lowerProto) &&
312-
(slashes || (proto && !slashedProtocol.has(proto)))) {
319+
(slashes || at || (proto && !slashedProtocol.has(proto)))) {
313320

314321
// there's a hostname.
315322
// the first instance of /, ?, ;, or # ends the host.

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,22 @@ const parseTests = {
975975
query: null,
976976
pathname: '/everybody',
977977
path: '/everybody',
978-
href: '//[email protected]/everybody#to-the-limit'
978+
href: '//[email protected]/everybody#to-the-limit',
979+
},
980+
981+
'http:@localhost': {
982+
protocol: 'http:',
983+
slashes: null,
984+
auth: null,
985+
host: 'localhost',
986+
port: null,
987+
hostname: 'localhost',
988+
hash: null,
989+
search: null,
990+
query: null,
991+
pathname: '/',
992+
path: '/',
993+
href: 'http://localhost/',
979994
},
980995
};
981996

0 commit comments

Comments
 (0)