Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url: detect hostname more reliably in url.parse() #41031

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
// Back slashes before the query string get converted to forward slashes
// See: https://code.google.com/p/chromium/issues/detail?id=25916
let hasHash = false;
let hasAt = false;
let start = -1;
let end = -1;
let rest = '';
Expand Down Expand Up @@ -219,6 +220,9 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
// Only convert backslashes while we haven't seen a split character
if (!split) {
switch (code) {
case CHAR_AT:
hasAt = true;
break;
case CHAR_HASH:
hasHash = true;
// Fall through
Expand Down Expand Up @@ -259,7 +263,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
}
}

if (!slashesDenoteHost && !hasHash) {
if (!slashesDenoteHost && !hasHash && !hasAt) {
// Try fast path regexp
const simplePath = simplePathPattern.exec(rest);
if (simplePath) {
Expand Down
32 changes: 31 additions & 1 deletion test/parallel/test-url-parse-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,37 @@ const parseTests = {
pathname: '/',
path: '/',
href: 'wss://www.example.com/'
}
},

'//fhqwhgads@example.com/everybody-to-the-limit': {
protocol: null,
slashes: true,
auth: 'fhqwhgads',
host: 'example.com',
port: null,
hostname: 'example.com',
hash: null,
search: null,
query: null,
pathname: '/everybody-to-the-limit',
path: '/everybody-to-the-limit',
href: '//fhqwhgads@example.com/everybody-to-the-limit'
},

'//fhqwhgads@example.com/everybody#to-the-limit': {
protocol: null,
slashes: true,
auth: 'fhqwhgads',
host: 'example.com',
port: null,
hostname: 'example.com',
hash: '#to-the-limit',
search: null,
query: null,
pathname: '/everybody',
path: '/everybody',
href: '//fhqwhgads@example.com/everybody#to-the-limit'
},
};

for (const u in parseTests) {
Expand Down