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: stricter domainTo*() argument checking #12134

Closed
wants to merge 1 commit into from
Closed
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: 6 additions & 0 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,11 +1308,17 @@ function originFor(url, base) {
}

function domainToASCII(domain) {
if (arguments.length < 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you change these to checks against domain explicitly. No need to introduce arguments for no reason.

Copy link
Member Author

Choose a reason for hiding this comment

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

That would be against the semantics used by other WHATWG URL interface methods. Why the aversion to arguments in this case? arguments.length is perfectly fine and optimizable by V8.

Copy link
Member

Choose a reason for hiding this comment

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

Since url.host = undefined actually sets the domain to "undefined"...it makes sense for domainTo* too so arguments.length is needed

throw new TypeError('"domain" argument must be specified');

// toUSVString is not needed.
return binding.domainToASCII(`${domain}`);
}

function domainToUnicode(domain) {
if (arguments.length < 1)
throw new TypeError('"domain" argument must be specified');

// toUSVString is not needed.
return binding.domainToUnicode(`${domain}`);
}
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-whatwg-url-domainto.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ const { domainToASCII, domainToUnicode } = require('url');
// Tests below are not from WPT.
const tests = require('../fixtures/url-idna.js');

{
assert.throws(() => domainToASCII(),
/^TypeError: "domain" argument must be specified$/);
assert.throws(() => domainToUnicode(),
/^TypeError: "domain" argument must be specified$/);
assert.strictEqual(domainToASCII(undefined), 'undefined');
assert.strictEqual(domainToUnicode(undefined), 'undefined');
}

{
for (const [i, { ascii, unicode }] of tests.valid.entries()) {
assert.strictEqual(ascii, domainToASCII(unicode),
Expand Down