Skip to content

Commit 134ed0e

Browse files
benbuckschcodebytere
authored andcommitted
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: #33482 Fixes: #33480 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]>
1 parent 4cc391b commit 134ed0e

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

lib/internal/crypto/keys.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,10 @@ function prepareAsymmetricKey(key, ctx) {
268268
// Either PEM or DER using PKCS#1 or SPKI.
269269
if (!isStringOrBuffer(data)) {
270270
throw new ERR_INVALID_ARG_TYPE(
271-
'key',
271+
'key.key',
272272
['string', 'Buffer', 'TypedArray', 'DataView',
273273
...(ctx !== kCreatePrivate ? ['KeyObject'] : [])],
274-
key);
274+
data);
275275
}
276276

277277
const isPublic =

test/parallel/test-crypto-sign-verify.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,18 @@ assert.throws(
391391
});
392392

393393
[1, {}, [], Infinity].forEach((input) => {
394+
let prop = '"key" argument';
395+
let value = input;
396+
if (typeof input === 'object') {
397+
prop = '"key.key" property';
398+
value = undefined;
399+
}
394400
const errObj = {
395401
code: 'ERR_INVALID_ARG_TYPE',
396402
name: 'TypeError',
397-
message: 'The "key" argument must be of type string or an instance of ' +
398-
'Buffer, TypedArray, DataView, or KeyObject.' +
399-
common.invalidArgTypeHelper(input)
403+
message: `The ${prop} must be of type string or ` +
404+
'an instance of Buffer, TypedArray, DataView, or KeyObject.' +
405+
common.invalidArgTypeHelper(value)
400406
};
401407

402408
assert.throws(() => sign.sign(input), errObj);
@@ -478,25 +484,33 @@ assert.throws(
478484
[1, {}, [], true, Infinity].forEach((input) => {
479485
const data = Buffer.alloc(1);
480486
const sig = Buffer.alloc(1);
481-
const received = common.invalidArgTypeHelper(input);
482487
const errObj = {
483488
code: 'ERR_INVALID_ARG_TYPE',
484489
name: 'TypeError',
485490
message: 'The "data" argument must be an instance of Buffer, ' +
486-
`TypedArray, or DataView.${received}`
491+
'TypedArray, or DataView.' +
492+
common.invalidArgTypeHelper(input)
487493
};
488494

489495
assert.throws(() => crypto.sign(null, input, 'asdf'), errObj);
490496
assert.throws(() => crypto.verify(null, input, 'asdf', sig), errObj);
491497

492-
errObj.message = 'The "key" argument must be of type string or an instance ' +
493-
`of Buffer, TypedArray, DataView, or KeyObject.${received}`;
498+
let prop = '"key" argument';
499+
let value = input;
500+
if (typeof input === 'object') {
501+
prop = '"key.key" property';
502+
value = undefined;
503+
}
504+
errObj.message = `The ${prop} must be of type string or ` +
505+
'an instance of Buffer, TypedArray, DataView, or KeyObject.' +
506+
common.invalidArgTypeHelper(value);
494507

495508
assert.throws(() => crypto.sign(null, data, input), errObj);
496509
assert.throws(() => crypto.verify(null, data, input, sig), errObj);
497510

498511
errObj.message = 'The "signature" argument must be an instance of ' +
499-
`Buffer, TypedArray, or DataView.${received}`;
512+
'Buffer, TypedArray, or DataView.' +
513+
common.invalidArgTypeHelper(input);
500514
assert.throws(() => crypto.verify(null, data, 'test', input), errObj);
501515
});
502516

0 commit comments

Comments
 (0)