Skip to content

Commit

Permalink
crypto: throw error on invalid object in diffieHellman()
Browse files Browse the repository at this point in the history
PR-URL: #37016
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
Lxxyx authored and targos committed Feb 2, 2021
1 parent b533485 commit ab64d74
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-crypto-dh-stateless.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ab64d74

Please sign in to comment.