crypto: improve error handling in parseKeyEncoding · nodejs/node@f2a07df (original) (raw)

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -172,6 +172,9 @@ function isStringOrBuffer(val) {
172 172 }
173 173
174 174 function parseKeyEncoding(enc, keyType, isPublic, objName) {
175 +if (enc === null |
176 +throw new ERR_INVALID_ARG_TYPE('options', 'object', enc);
177 +
175 178 const isInput = keyType === undefined;
176 179
177 180 const {
Original file line number Diff line number Diff line change
@@ -104,6 +104,16 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
104 104 assert.strictEqual(derivedPublicKey.asymmetricKeyType, 'rsa');
105 105 assert.strictEqual(derivedPublicKey.symmetricKeySize, undefined);
106 106
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 +
107 117 const publicDER = publicKey.export({
108 118 format: 'der',
109 119 type: 'pkcs1'