Skip to content

Commit f2a07df

Browse files
tniessentargos
authored andcommitted
crypto: improve error handling in parseKeyEncoding
This change only affects KeyObject.export(). Backport-PR-URL: #26688 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 ed7599b commit f2a07df

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Diff for: lib/internal/crypto/keys.js

+3
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ function isStringOrBuffer(val) {
172172
}
173173

174174
function parseKeyEncoding(enc, keyType, isPublic, objName) {
175+
if (enc === null || typeof enc !== 'object')
176+
throw new ERR_INVALID_ARG_TYPE('options', 'object', enc);
177+
175178
const isInput = keyType === undefined;
176179

177180
const {

Diff for: test/parallel/test-crypto-key-objects.js

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

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

0 commit comments

Comments
 (0)