Skip to content

Commit

Permalink
crypto: fix wrong error message
Browse files Browse the repository at this point in the history
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: #33482
Fixes: #33480
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Ujjwal Sharma <ryzokuken@disroot.org>
  • Loading branch information
benbucksch authored and codebytere committed Jul 8, 2020
1 parent 7ea6b07 commit 18dc03d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
30 changes: 22 additions & 8 deletions test/parallel/test-crypto-sign-verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});

Expand Down

0 comments on commit 18dc03d

Please sign in to comment.