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
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/punycode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 14 additions & 2 deletions test/parallel/test-punycode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 = [
Expand Down