crypto: improve error handling in parseKeyEncoding

This change only affects KeyObject.export().

PR-URL: https://github.com/nodejs/node/pull/26455
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
Tobias Nießen 2019-03-05 18:35:50 +01:00
parent 1acf3b155f
commit 3afa5d7ba8
No known key found for this signature in database
GPG Key ID: 718207F8FD156B70
2 changed files with 13 additions and 0 deletions

View File

@ -178,6 +178,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 {

View File

@ -107,6 +107,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'