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) {