Skip to content

Commit 3afa5d7

Browse files
committed
crypto: improve error handling in parseKeyEncoding
This change only affects KeyObject.export(). PR-URL: #26455 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 1acf3b1 commit 3afa5d7

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/internal/crypto/keys.js

+3
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ function isStringOrBuffer(val) {
178178
}
179179

180180
function parseKeyEncoding(enc, keyType, isPublic, objName) {
181+
if (enc === null || typeof enc !== 'object')
182+
throw new ERR_INVALID_ARG_TYPE('options', 'object', enc);
183+
181184
const isInput = keyType === undefined;
182185

183186
const {

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

+10
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
107107
assert.strictEqual(derivedPublicKey.asymmetricKeyType, 'rsa');
108108
assert.strictEqual(derivedPublicKey.symmetricKeySize, undefined);
109109

110+
// Test exporting with an invalid options object, this should throw.
111+
for (const opt of [undefined, null, 'foo', 0, NaN]) {
112+
common.expectsError(() => publicKey.export(opt), {
113+
type: TypeError,
114+
code: 'ERR_INVALID_ARG_TYPE',
115+
message: 'The "options" argument must be of type object. Received type ' +
116+
typeof opt
117+
});
118+
}
119+
110120
const publicDER = publicKey.export({
111121
format: 'der',
112122
type: 'pkcs1'

0 commit comments

Comments
 (0)