diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index ef2668e588bc00..1eb4a6f7be7006 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -172,6 +172,9 @@ function isStringOrBuffer(val) { } function parseKeyEncoding(enc, keyType, isPublic, objName) { + if (enc === null || typeof enc !== 'object') + throw new ERR_INVALID_ARG_TYPE('options', 'object', enc); + const isInput = keyType === undefined; const { diff --git a/test/parallel/test-crypto-key-objects.js b/test/parallel/test-crypto-key-objects.js index 1ec24e5f6adaa8..a7d7f32cb39f65 100644 --- a/test/parallel/test-crypto-key-objects.js +++ b/test/parallel/test-crypto-key-objects.js @@ -104,6 +104,16 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii'); assert.strictEqual(derivedPublicKey.asymmetricKeyType, 'rsa'); assert.strictEqual(derivedPublicKey.symmetricKeySize, undefined); + // Test exporting with an invalid options object, this should throw. + for (const opt of [undefined, null, 'foo', 0, NaN]) { + common.expectsError(() => publicKey.export(opt), { + type: TypeError, + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "options" argument must be of type object. Received type ' + + typeof opt + }); + } + const publicDER = publicKey.export({ format: 'der', type: 'pkcs1'