Skip to content

Commit 08c2512

Browse files
Trottdanielleadams
authored andcommittedFeb 1, 2022
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 b9567d9 commit 08c2512

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
@@ -172,6 +172,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
172172
// Back slashes before the query string get converted to forward slashes
173173
// See: https://code.google.com/p/chromium/issues/detail?id=25916
174174
let hasHash = false;
175+
let hasAt = false;
175176
let start = -1;
176177
let end = -1;
177178
let rest = '';
@@ -204,6 +205,9 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
204205
// Only convert backslashes while we haven't seen a split character
205206
if (!split) {
206207
switch (code) {
208+
case CHAR_AT:
209+
hasAt = true;
210+
break;
207211
case CHAR_HASH:
208212
hasHash = true;
209213
// Fall through
@@ -244,7 +248,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
244248
}
245249
}
246250

247-
if (!slashesDenoteHost && !hasHash) {
251+
if (!slashesDenoteHost && !hasHash && !hasAt) {
248252
// Try fast path regexp
249253
const simplePath = simplePathPattern.exec(rest);
250254
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+
'//fhqwhgads@example.com/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: '//fhqwhgads@example.com/everybody-to-the-limit'
964+
},
965+
966+
'//fhqwhgads@example.com/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: '//fhqwhgads@example.com/everybody#to-the-limit'
979+
},
950980
};
951981

952982
for (const u in parseTests) {

0 commit comments

Comments
 (0)
Please sign in to comment.