-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
lib: validate argument for punycode.encode and decode #10990
Conversation
Could you do this as two commits, one which adds tests asserting the current behaviour, then a follow on commit showing the change in-behaviour? Its easier to track semver-major changes when they show up as changes in the test suites. |
Shouldn't the |
lib/punycode.js
Outdated
@@ -194,6 +194,9 @@ const adapt = function(delta, numPoints, firstTime) { | |||
* @returns {String} The resulting string of Unicode symbols. | |||
*/ | |||
const decode = function(input) { | |||
if (typeof input !== 'string' && !Array.isArray(input)) { | |||
throw new TypeError('Argument must be a string or an array'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what reason are arrays allowed? As far as I can tell only the empty array would work as input
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.This is my overlook...
9433fce
to
ad37922
Compare
lib/punycode.js
Outdated
@@ -194,6 +194,9 @@ const adapt = function(delta, numPoints, firstTime) { | |||
* @returns {String} The resulting string of Unicode symbols. | |||
*/ | |||
const decode = function(input) { | |||
if (!(typeof input === 'string' || Array.isArray(input) && input.length === 0)) { | |||
throw new TypeError('Argument must be a string or an empty array'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for not being clear, but why are any arrays allowed? The documentation says only strings are allowed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, arrays are allowed behavior so I accepted it.
Can we disable it?
I think decode should be changed to accept only strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I accepted only a string.
ad37922
to
c6596d7
Compare
@TimothyGu Updated, PTAL. |
c6596d7
to
a971d0e
Compare
Just reiterating. Shouldn't the changes to |
Unify error wording and make it easy to understand. `punycode.encode` is occurred an error when the argument is `undefined` and `null`. `punycode.decode` is occurred an error when the argument isn't `string` and `array`. We accepted only a string.
a971d0e
to
463f756
Compare
@cjihrig Oops, sorry. Is this ok? |
Historically, we have taken |
Guessing from the history of that file, maybe @mathiasbynens ? |
I can confirm that until now Node.js just used the upstream Punycode.js version. What if I don’t want those changes in Punycode.js, though? Would that be a dealbreaker for Node? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm -1 on this. The punycode module is a vendored in dependency. This change, if made at all, should go into the upstream version. Also consider that the punycode module is considered to be docs-deprecated. Users should be moving to the userland version.
Just curious: why isn't punycode.js in |
Yeah, we should do that. I can open a PR later this week if nobody else does. :) |
Closing this. Can discuss reopening if necessary. |
Purpose
Unify error wording and make it easy to understand.
encode
Current behavior
punycode.encode()
''
''
''
''
''
''
punycode.encode
is occurred an error when the argument isundefined
andnull
.Error log
Modify error's wording like below.
decode
Current behavior
punycode.decode()
''
''
(only empty arrays are accepted)punycode.decode
is occurred an error when the argument isn'tstring
andarray
.We accpeted only a string.
Error log
undefined, null
number, boolean, object, function
Modify error's wording like below.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
punycode, test