From 3afa5d7ba8d7b25ff3acda303c20e41230342e18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Tue, 5 Mar 2019 18:35:50 +0100 Subject: [PATCH] crypto: improve error handling in parseKeyEncoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change only affects KeyObject.export(). PR-URL: https://github.com/nodejs/node/pull/26455 Reviewed-By: Luigi Pinca Reviewed-By: Anna Henningsen Reviewed-By: Ruben Bridgewater Reviewed-By: Rich Trott Reviewed-By: Michaƫl Zasso --- lib/internal/crypto/keys.js | 3 +++ test/parallel/test-crypto-key-objects.js | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index 15a359be9662d7..2f4dce8a3b93b6 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -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 { diff --git a/test/parallel/test-crypto-key-objects.js b/test/parallel/test-crypto-key-objects.js index 9819bb7914141e..85157439920e66 100644 --- a/test/parallel/test-crypto-key-objects.js +++ b/test/parallel/test-crypto-key-objects.js @@ -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'