Skip to content

Commit db77780

Browse files
Trottdanielleadams
authored andcommitted
url: detect hostname more reliably in url.parse()
Based on existing tests and code comments, url.parse() is expected to treat any URL containing user@host as having a hostname. However, it turns out this behavior relies on the URL having a hash which is surprising, to put it mildly. Detect the host even without the hash. PR-URL: #41031 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Myles Borins <[email protected]>
1 parent de1748a commit db77780

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/url.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
187187
// Back slashes before the query string get converted to forward slashes
188188
// See: https://code.google.com/p/chromium/issues/detail?id=25916
189189
let hasHash = false;
190+
let hasAt = false;
190191
let start = -1;
191192
let end = -1;
192193
let rest = '';
@@ -219,6 +220,9 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
219220
// Only convert backslashes while we haven't seen a split character
220221
if (!split) {
221222
switch (code) {
223+
case CHAR_AT:
224+
hasAt = true;
225+
break;
222226
case CHAR_HASH:
223227
hasHash = true;
224228
// Fall through
@@ -259,7 +263,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
259263
}
260264
}
261265

262-
if (!slashesDenoteHost && !hasHash) {
266+
if (!slashesDenoteHost && !hasHash && !hasAt) {
263267
// Try fast path regexp
264268
const simplePath = simplePathPattern.exec(rest);
265269
if (simplePath) {

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

+31-1
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,37 @@ const parseTests = {
946946
pathname: '/',
947947
path: '/',
948948
href: 'wss://www.example.com/'
949-
}
949+
},
950+
951+
'//[email protected]/everybody-to-the-limit': {
952+
protocol: null,
953+
slashes: true,
954+
auth: 'fhqwhgads',
955+
host: 'example.com',
956+
port: null,
957+
hostname: 'example.com',
958+
hash: null,
959+
search: null,
960+
query: null,
961+
pathname: '/everybody-to-the-limit',
962+
path: '/everybody-to-the-limit',
963+
href: '//[email protected]/everybody-to-the-limit'
964+
},
965+
966+
'//[email protected]/everybody#to-the-limit': {
967+
protocol: null,
968+
slashes: true,
969+
auth: 'fhqwhgads',
970+
host: 'example.com',
971+
port: null,
972+
hostname: 'example.com',
973+
hash: '#to-the-limit',
974+
search: null,
975+
query: null,
976+
pathname: '/everybody',
977+
path: '/everybody',
978+
href: '//[email protected]/everybody#to-the-limit'
979+
},
950980
};
951981

952982
for (const u in parseTests) {

0 commit comments

Comments
 (0)