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

lib: validate argument for punycode.encode and decode #10990

Closed

Conversation

hiroppy
Copy link
Member

@hiroppy hiroppy commented Jan 25, 2017

Purpose

Unify error wording and make it easy to understand.

encode

Current behavior

punycode.encode()

argument's type result
number ''
string ''
boolean ''
undefined error
null error
object ''
array ''
function ''

punycode.encode is occurred an error when the argument is undefined and null.

Error log
TypeError: Cannot read property 'length' of undefined
    at ucs2decode (punycode.js:104:23)
    at Object.encode (punycode.js:291:10)
    at repl:1:10
    at realRunInThisContextScript (vm.js:22:35)
    at sigintHandlersWrap (vm.js:98:12)
    at ContextifyScript.Script.runInThisContext (vm.js:24:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:544:10)

Modify error's wording like below.

TypeError: Argument must not be "undefined" and "null"
    at Object.encode (punycode.js:292:11)
    at repl:1:10
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at REPLServer.defaultEval (repl.js:340:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:537:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:189:7)
    at REPLServer.Interface._onLine (readline.js:238:10)

decode

Current behavior

punycode.decode()

argument's type result
number error
string ''
boolean error
undefined error
null error
object error
array ''(only empty arrays are accepted)
function error

punycode.decode is occurred an error when the argument isn't string and array.
We accpeted only a string.

Error log

undefined, null

TypeError: Cannot read property 'length' of undefined
    at Object.decode (punycode.js:199:27)
    at repl:1:10
    at realRunInThisContextScript (vm.js:22:35)
    at sigintHandlersWrap (vm.js:98:12)
    at ContextifyScript.Script.runInThisContext (vm.js:24:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:544:10)
    at emitOne (events.js:101:20)

number, boolean, object, function

TypeError: input.lastIndexOf is not a function
    at Object.decode (punycode.js:208:20)
    at repl:1:10
    at realRunInThisContextScript (vm.js:22:35)
    at sigintHandlersWrap (vm.js:98:12)
    at ContextifyScript.Script.runInThisContext (vm.js:24:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:544:10)
    at emitOne (events.js:101:20)

Modify error's wording like below.

TypeError: Argument must be a string
    at Object.decode (punycode.js:198:11)
    at repl:1:10
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at REPLServer.defaultEval (repl.js:340:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:537:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:189:7)
    at REPLServer.Interface._onLine (readline.js:238:10)
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

punycode, test

@mscdex mscdex added punycode Issues and PRs related to the punycode module bundled in Node.js. and removed dont-land-on-v7.x labels Jan 25, 2017
@sam-github sam-github added the semver-major PRs that contain breaking changes and should be released in the next major version. label Jan 25, 2017
@sam-github
Copy link
Contributor

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.

@cjihrig
Copy link
Contributor

cjihrig commented Jan 25, 2017

Shouldn't the lib/punycode.js changes be upstreamed first?

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');
Copy link
Member

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.

Copy link
Member Author

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...

@hiroppy hiroppy force-pushed the feature/add-typeerror-to-punycode branch from 9433fce to ad37922 Compare January 25, 2017 20:42
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');
Copy link
Member

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.

Copy link
Member Author

@hiroppy hiroppy Jan 26, 2017

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.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I agree.

Copy link
Member Author

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.

@hiroppy hiroppy force-pushed the feature/add-typeerror-to-punycode branch from ad37922 to c6596d7 Compare January 26, 2017 22:45
@hiroppy
Copy link
Member Author

hiroppy commented Jan 26, 2017

@TimothyGu Updated, PTAL.

@hiroppy hiroppy force-pushed the feature/add-typeerror-to-punycode branch from c6596d7 to a971d0e Compare January 26, 2017 22:50
@cjihrig
Copy link
Contributor

cjihrig commented Jan 27, 2017

Just reiterating. Shouldn't the changes to lib/punycode.js be upstreamed first? Or, do we not take that file as is anymore?

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.
@hiroppy hiroppy force-pushed the feature/add-typeerror-to-punycode branch from a971d0e to 463f756 Compare January 27, 2017 15:58
@hiroppy
Copy link
Member Author

hiroppy commented Jan 27, 2017

@cjihrig Oops, sorry. Is this ok?

@cjihrig
Copy link
Contributor

cjihrig commented Jan 27, 2017

Historically, we have taken punycode.js as is from upstream and didn't really maintain or change it at all. If that is still the case, then I don't think we should make modifications. If that isn't the case anymore, then I have no strong opinion.

@gibfahn
Copy link
Member

gibfahn commented Jan 27, 2017

Just reiterating. Shouldn't the changes to lib/punycode.js be upstreamed first? Or, do we not take that file as is anymore?

Guessing from the history of that file, maybe @mathiasbynens ?

@mathiasbynens
Copy link
Contributor

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?

Copy link
Member

@jasnell jasnell left a 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.

@joyeecheung
Copy link
Member

Just curious: why isn't punycode.js in deps and then forwarded to userland in lib?

@addaleax
Copy link
Member

addaleax commented Feb 6, 2017

Just curious: why isn't punycode.js in deps and then forwarded to userland in lib?

Yeah, we should do that. I can open a PR later this week if nobody else does. :)

@jasnell jasnell added the stalled Issues and PRs that are stalled. label Mar 1, 2017
@jasnell
Copy link
Member

jasnell commented May 1, 2017

Closing this. Can discuss reopening if necessary.

@jasnell jasnell closed this May 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
punycode Issues and PRs related to the punycode module bundled in Node.js. semver-major PRs that contain breaking changes and should be released in the next major version. stalled Issues and PRs that are stalled.
Projects
None yet
Development

Successfully merging this pull request may close these issues.