From 1a4a189578944f62b3dfb6f4fc8dabe20a7535ee Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Wed, 8 Oct 2014 15:38:46 -0700 Subject: [PATCH] crypto: createDiffieHellman throw for bad args Previously crypto.createDiffieHellman() would fail silently when a bad argument was passed for prime/prime_length. Now throws TypeError. Fixes: https://github.com/joyent/node/issues/8480 Signed-off-by: Trevor Norris --- lib/crypto.js | 5 +++++ test/simple/test-crypto.js | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/crypto.js b/lib/crypto.js index a38ccb77c16..2f0a00b1523 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -376,6 +376,11 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) { if (!(this instanceof DiffieHellman)) return new DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding); + if (!util.isBuffer(sizeOrKey) && + typeof sizeOrKey !== 'number' && + typeof sizeOrKey !== 'string') + throw new TypeError('First argument should be number, string or Buffer'); + if (keyEncoding) { if (typeof keyEncoding !== 'string' || (!Buffer.isEncoding(keyEncoding) && keyEncoding !== 'buffer')) { diff --git a/test/simple/test-crypto.js b/test/simple/test-crypto.js index 4b623380b46..d3f1ade321c 100644 --- a/test/simple/test-crypto.js +++ b/test/simple/test-crypto.js @@ -719,6 +719,22 @@ assert.equal(secret1, secret2.toString('base64')); assert.equal(dh1.verifyError, 0); assert.equal(dh2.verifyError, 0); +assert.throws(function() { + crypto.createDiffieHellman([0x1, 0x2]); +}); + +assert.throws(function() { + crypto.createDiffieHellman(function() { }); +}); + +assert.throws(function() { + crypto.createDiffieHellman(/abc/); +}); + +assert.throws(function() { + crypto.createDiffieHellman({}); +}); + // Create "another dh1" using generated keys from dh1, // and compute secret again var dh3 = crypto.createDiffieHellman(p1, 'buffer');