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: #33482Fixes: #33480 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Ujjwal Sharma <ryzokuken@disroot.org>
1 parent 4cc391b commit 134ed0e

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

‎lib/internal/crypto/keys.js‎

Lines changed: 2 additions & 2 deletions
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
thrownewERR_INVALID_ARG_TYPE(
271-
'key',
271+
'key.key',
272272
['string','Buffer','TypedArray','DataView',
273273
...(ctx!==kCreatePrivate ? ['KeyObject'] : [])],
274-
key);
274+
data);
275275
}
276276

277277
constisPublic=

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,18 @@ assert.throws(
391391
});
392392

393393
[1,{},[],Infinity].forEach((input)=>{
394+
letprop='"key" argument';
395+
letvalue=input;
396+
if(typeofinput==='object'){
397+
prop='"key.key" property';
398+
value=undefined;
399+
}
394400
consterrObj={
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
constdata=Buffer.alloc(1);
480486
constsig=Buffer.alloc(1);
481-
constreceived=common.invalidArgTypeHelper(input);
482487
consterrObj={
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+
letprop='"key" argument';
499+
letvalue=input;
500+
if(typeofinput==='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)