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 <ruben@bridgewater.de>
Reviewed-By: Ujjwal Sharma <ryzokuken@disroot.org>
This commit is contained in:
Ben Bucksch 2020-05-20 14:13:29 +02:00 committed by Ruben Bridgewater
parent 5632ff66cd
commit 2f00ca42bf
2 changed files with 24 additions and 10 deletions

View File

@ -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 =

View File

@ -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);
});