From 25c1751d937f350fe2a9923b91d9626fcecff1a5 Mon Sep 17 00:00:00 2001 From: Yuta Hiroto Date: Thu, 26 Jan 2017 05:37:07 +0900 Subject: [PATCH 1/2] lib: validate argument for punycode.encode and decode 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. --- lib/punycode.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/punycode.js b/lib/punycode.js index 34da3ca5ad13b6..ac32cfd4f49571 100644 --- a/lib/punycode.js +++ b/lib/punycode.js @@ -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') { + throw new TypeError('Argument must be a string'); + } // Don't use UCS-2. const output = []; const inputLength = input.length; @@ -285,6 +288,9 @@ const decode = function(input) { * @returns {String} The resulting Punycode string of ASCII-only symbols. */ const encode = function(input) { + if (input === undefined || input === null) { + throw new TypeError('Argument must not be "undefined" and "null"'); + } const output = []; // Convert the input in UCS-2 to an array of Unicode code points. From 463f7567451546965982e5d6fd9cf485d74c6843 Mon Sep 17 00:00:00 2001 From: Yuta Hiroto Date: Thu, 26 Jan 2017 05:39:02 +0900 Subject: [PATCH 2/2] test: add exception tests to punycode --- test/parallel/test-punycode.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-punycode.js b/test/parallel/test-punycode.js index 60175557042ff9..1edb3aded00118 100644 --- a/test/parallel/test-punycode.js +++ b/test/parallel/test-punycode.js @@ -13,6 +13,12 @@ assert.strictEqual( 'Willst du die Blthe des frhen, die Frchte des spteren Jahres-x9e96lkal' ); assert.strictEqual(punycode.encode('日本語'), 'wgv71a119e'); +assert.throws(() => { + punycode.encode(); +}, /^TypeError: Argument must not be "undefined" and "null"$/); +assert.throws(() => { + punycode.encode(null); +}, /^TypeError: Argument must not be "undefined" and "null"$/); assert.strictEqual(punycode.decode('tda'), 'ü'); assert.strictEqual(punycode.decode('Goethe-'), 'Goethe'); @@ -25,14 +31,20 @@ assert.strictEqual( ); assert.strictEqual(punycode.decode('wgv71a119e'), '日本語'); assert.throws(() => { - punycode.decode(' '); -}, /^RangeError: Invalid input$/); + punycode.decode(); +}, /^TypeError: Argument must be a string$/); +assert.throws(() => { + punycode.decode([]); +}, /^TypeError: Argument must be a string$/); assert.throws(() => { punycode.decode('α-'); }, /^RangeError: Illegal input >= 0x80 \(not a basic code point\)$/); assert.throws(() => { punycode.decode('あ'); }, /^RangeError: Overflow: input needs wider integers to process$/); +assert.throws(() => { + punycode.decode(' '); +}, /^RangeError: Invalid input$/); // http://tools.ietf.org/html/rfc3492#section-7.1 const tests = [