Skip to content

Commit 9220a68

Browse files
pinguinjkeketargos
authored andcommitted
crypto: fix KeyObject handle type error message
Fix KeyObject handle type error message. Add test to cover KeyObject ERR_INVALID_ARG_TYPE exception. PR-URL: #27904 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 33236b7 commit 9220a68

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎lib/internal/crypto/keys.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class KeyObject {
4444
if(type!=='secret'&&type!=='public'&&type!=='private')
4545
thrownewERR_INVALID_ARG_VALUE('type',type);
4646
if(typeofhandle!=='object')
47-
thrownewERR_INVALID_ARG_TYPE('handle','string',handle);
47+
thrownewERR_INVALID_ARG_TYPE('handle','object',handle);
4848

4949
this[kKeyType]=type;
5050

‎test/parallel/test-crypto-key-objects.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
createSecretKey,
1414
createPublicKey,
1515
createPrivateKey,
16+
KeyObject,
1617
randomBytes,
1718
publicEncrypt,
1819
privateDecrypt
@@ -39,6 +40,27 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
3940
});
4041
}
4142

43+
{
44+
// Attempting to create a key of a wrong type should throw
45+
constTYPE='wrong_type';
46+
47+
common.expectsError(()=>newKeyObject(TYPE),{
48+
type: TypeError,
49+
code: 'ERR_INVALID_ARG_VALUE',
50+
message: `The argument 'type' is invalid. Received '${TYPE}'`
51+
});
52+
}
53+
54+
{
55+
// Attempting to create a key with non-object handle should throw
56+
common.expectsError(()=>newKeyObject('secret',''),{
57+
type: TypeError,
58+
code: 'ERR_INVALID_ARG_TYPE',
59+
message:
60+
'The "handle" argument must be of type object. Received type string'
61+
});
62+
}
63+
4264
{
4365
constkeybuf=randomBytes(32);
4466
constkey=createSecretKey(keybuf);

0 commit comments

Comments
 (0)