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: improve url.parse() compliance with WHATWG URL #45011

Merged
merged 1 commit into from
Oct 17, 2022
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
27 changes: 7 additions & 20 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,6 @@ const {
CHAR_LEFT_CURLY_BRACKET,
CHAR_RIGHT_CURLY_BRACKET,
CHAR_QUESTION_MARK,
CHAR_LOWERCASE_A,
CHAR_LOWERCASE_Z,
CHAR_UPPERCASE_A,
CHAR_UPPERCASE_Z,
CHAR_DOT,
CHAR_0,
CHAR_9,
CHAR_HYPHEN_MINUS,
CHAR_PLUS,
CHAR_UNDERSCORE,
CHAR_DOUBLE_QUOTE,
CHAR_SINGLE_QUOTE,
CHAR_PERCENT,
Expand All @@ -147,6 +137,7 @@ const {
CHAR_GRAVE_ACCENT,
CHAR_VERTICAL_LINE,
CHAR_AT,
CHAR_COLON,
} = require('internal/constants');

function urlParse(url, parseQueryString, slashesDenoteHost) {
Expand Down Expand Up @@ -514,16 +505,12 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
function getHostname(self, rest, hostname) {
for (let i = 0; i < hostname.length; ++i) {
const code = hostname.charCodeAt(i);
const isValid = (code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z) ||
code === CHAR_DOT ||
(code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) ||
(code >= CHAR_0 && code <= CHAR_9) ||
code === CHAR_HYPHEN_MINUS ||
code === CHAR_PLUS ||
code === CHAR_UNDERSCORE ||
code > 127;

// Invalid host character
const isValid = (code !== CHAR_FORWARD_SLASH &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is now too lax. It allows code points like U+005E (^), or U+007C (|) forbidden by the URL Standard.

Copy link
Member Author

@Trott Trott Oct 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before and after this change url.parse() returns the same result for foo.com^bar.com and foo.com|bar.com. (I agree, though, that it would be better if it threw errors in those cases like it does for WHATWG URL, but that's probably a subsequent change. Small changes to url.parse() will make it easier to back out if we mess up and break something in the ecosystem.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done in #45046.

code !== CHAR_BACKWARD_SLASH &&
code !== CHAR_HASH &&
code !== CHAR_QUESTION_MARK &&
code !== CHAR_COLON);

if (!isValid) {
self.hostname = hostname.slice(0, i);
return `/${hostname.slice(i)}${rest}`;
Expand Down
27 changes: 21 additions & 6 deletions test/parallel/test-url-parse-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,15 +885,15 @@ const parseTests = {
protocol: 'https:',
slashes: true,
auth: null,
host: '',
host: '*',
port: null,
hostname: '',
hostname: '*',
hash: null,
search: null,
query: null,
pathname: '/*',
path: '/*',
href: 'https:///*'
pathname: '/',
path: '/',
href: 'https://*/'
},

// The following two URLs are the same, but they differ for a capital A.
Expand Down Expand Up @@ -991,7 +991,22 @@ const parseTests = {
pathname: '/',
path: '/',
href: 'http://example.com/'
}
},

'https://evil.com$.example.com': {
protocol: 'https:',
slashes: true,
auth: null,
host: 'evil.com$.example.com',
port: null,
hostname: 'evil.com$.example.com',
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: 'https://evil.com$.example.com/'
},
};

for (const u in parseTests) {
Expand Down