From 134ed0eea379e66504ce488c86a3e17f47f607b4 Mon Sep 17 00:00:00 2001 From: Ben Bucksch Date: Wed, 20 May 2020 14:13:29 +0200 Subject: [PATCH] crypto: fix wrong error message When calling `crypto.sign()`, if the `key` parameter object is missing the `key` property, the error message is wrong. Before the fix: TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject. Received an instance of Object Expected: TypeError [ERR_INVALID_ARG_TYPE]: The "key.key property" argument must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject. Received undefined This seems like a copy&paste bug. Somebody copied from the end of the function, where this is correct, to here, where it's wrong. PR-URL: https://github.com/nodejs/node/pull/33482 Fixes: https://github.com/nodejs/node/issues/33480 Reviewed-By: Ruben Bridgewater Reviewed-By: Ujjwal Sharma --- lib/internal/crypto/keys.js | 4 ++-- test/parallel/test-crypto-sign-verify.js | 30 +++++++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index c0a133d5d429a3..bfb30dda3fd752 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -268,10 +268,10 @@ function prepareAsymmetricKey(key, ctx) { // Either PEM or DER using PKCS#1 or SPKI. if (!isStringOrBuffer(data)) { throw new ERR_INVALID_ARG_TYPE( - 'key', + 'key.key', ['string', 'Buffer', 'TypedArray', 'DataView', ...(ctx !== kCreatePrivate ? ['KeyObject'] : [])], - key); + data); } const isPublic = diff --git a/test/parallel/test-crypto-sign-verify.js b/test/parallel/test-crypto-sign-verify.js index b70bfccae47eef..ff410dcf00fa6a 100644 --- a/test/parallel/test-crypto-sign-verify.js +++ b/test/parallel/test-crypto-sign-verify.js @@ -391,12 +391,18 @@ assert.throws( }); [1, {}, [], Infinity].forEach((input) => { + let prop = '"key" argument'; + let value = input; + if (typeof input === 'object') { + prop = '"key.key" property'; + value = undefined; + } const errObj = { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', - message: 'The "key" argument must be of type string or an instance of ' + - 'Buffer, TypedArray, DataView, or KeyObject.' + - common.invalidArgTypeHelper(input) + message: `The ${prop} must be of type string or ` + + 'an instance of Buffer, TypedArray, DataView, or KeyObject.' + + common.invalidArgTypeHelper(value) }; assert.throws(() => sign.sign(input), errObj); @@ -478,25 +484,33 @@ assert.throws( [1, {}, [], true, Infinity].forEach((input) => { const data = Buffer.alloc(1); const sig = Buffer.alloc(1); - const received = common.invalidArgTypeHelper(input); const errObj = { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: 'The "data" argument must be an instance of Buffer, ' + - `TypedArray, or DataView.${received}` + 'TypedArray, or DataView.' + + common.invalidArgTypeHelper(input) }; assert.throws(() => crypto.sign(null, input, 'asdf'), errObj); assert.throws(() => crypto.verify(null, input, 'asdf', sig), errObj); - errObj.message = 'The "key" argument must be of type string or an instance ' + - `of Buffer, TypedArray, DataView, or KeyObject.${received}`; + let prop = '"key" argument'; + let value = input; + if (typeof input === 'object') { + prop = '"key.key" property'; + value = undefined; + } + errObj.message = `The ${prop} must be of type string or ` + + 'an instance of Buffer, TypedArray, DataView, or KeyObject.' + + common.invalidArgTypeHelper(value); assert.throws(() => crypto.sign(null, data, input), errObj); assert.throws(() => crypto.verify(null, data, input, sig), errObj); errObj.message = 'The "signature" argument must be an instance of ' + - `Buffer, TypedArray, or DataView.${received}`; + 'Buffer, TypedArray, or DataView.' + + common.invalidArgTypeHelper(input); assert.throws(() => crypto.verify(null, data, 'test', input), errObj); });