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 <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 33236b7 commit 9220a68

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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
throw new ERR_INVALID_ARG_VALUE('type', type);
4646
if (typeof handle !== 'object')
47-
throw new ERR_INVALID_ARG_TYPE('handle', 'string', handle);
47+
throw new ERR_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+
const TYPE = 'wrong_type';
46+
47+
common.expectsError(() => new KeyObject(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(() => new KeyObject('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
const keybuf = randomBytes(32);
4466
const key = createSecretKey(keybuf);

0 commit comments

Comments
 (0)