Skip to content

Commit ab64d74

Browse files
Lxxyxtargos
authored andcommitted
crypto: throw error on invalid object in diffieHellman()
PR-URL: #37016 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent b533485 commit ab64d74

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/internal/crypto/diffiehellman.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,7 @@ function getFormat(format) {
290290
const dhEnabledKeyTypes = new SafeSet(['dh', 'ec', 'x448', 'x25519']);
291291

292292
function diffieHellman(options) {
293-
if (typeof options !== 'object')
294-
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
293+
validateObject(options, 'options');
295294

296295
const { privateKey, publicKey } = options;
297296
if (!(privateKey instanceof KeyObject))

test/parallel/test-crypto-dh-stateless.js

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ assert.throws(() => crypto.diffieHellman(), {
1212
message: 'The "options" argument must be of type object. Received undefined'
1313
});
1414

15+
assert.throws(() => crypto.diffieHellman(null), {
16+
name: 'TypeError',
17+
code: 'ERR_INVALID_ARG_TYPE',
18+
message: 'The "options" argument must be of type object. Received null'
19+
});
20+
21+
assert.throws(() => crypto.diffieHellman([]), {
22+
name: 'TypeError',
23+
code: 'ERR_INVALID_ARG_TYPE',
24+
message:
25+
'The "options" argument must be of type object. ' +
26+
'Received an instance of Array',
27+
});
28+
1529
function test({ publicKey: alicePublicKey, privateKey: alicePrivateKey },
1630
{ publicKey: bobPublicKey, privateKey: bobPrivateKey },
1731
expectedValue) {

0 commit comments

Comments
 (0)