From ab64d74791b043bfafab5f6693fc3f57fbc7ec20 Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Thu, 21 Jan 2021 19:19:50 +0800 Subject: [PATCH] crypto: throw error on invalid object in diffieHellman() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/37016 Reviewed-By: Colin Ihrig Reviewed-By: Michaƫl Zasso Reviewed-By: Luigi Pinca Reviewed-By: Darshan Sen Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/internal/crypto/diffiehellman.js | 3 +-- test/parallel/test-crypto-dh-stateless.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/internal/crypto/diffiehellman.js b/lib/internal/crypto/diffiehellman.js index a82c03eb76d65c..bcf9b1e5090d1a 100644 --- a/lib/internal/crypto/diffiehellman.js +++ b/lib/internal/crypto/diffiehellman.js @@ -290,8 +290,7 @@ function getFormat(format) { const dhEnabledKeyTypes = new SafeSet(['dh', 'ec', 'x448', 'x25519']); function diffieHellman(options) { - if (typeof options !== 'object') - throw new ERR_INVALID_ARG_TYPE('options', 'object', options); + validateObject(options, 'options'); const { privateKey, publicKey } = options; if (!(privateKey instanceof KeyObject)) diff --git a/test/parallel/test-crypto-dh-stateless.js b/test/parallel/test-crypto-dh-stateless.js index ca1bc83112b95b..3d0c1b497d7adc 100644 --- a/test/parallel/test-crypto-dh-stateless.js +++ b/test/parallel/test-crypto-dh-stateless.js @@ -12,6 +12,20 @@ assert.throws(() => crypto.diffieHellman(), { message: 'The "options" argument must be of type object. Received undefined' }); +assert.throws(() => crypto.diffieHellman(null), { + name: 'TypeError', + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "options" argument must be of type object. Received null' +}); + +assert.throws(() => crypto.diffieHellman([]), { + name: 'TypeError', + code: 'ERR_INVALID_ARG_TYPE', + message: + 'The "options" argument must be of type object. ' + + 'Received an instance of Array', +}); + function test({ publicKey: alicePublicKey, privateKey: alicePrivateKey }, { publicKey: bobPublicKey, privateKey: bobPrivateKey }, expectedValue) {