diff --git a/browser.js b/browser.js index e223c63..66806d5 100644 --- a/browser.js +++ b/browser.js @@ -100,6 +100,8 @@ exports.verify = function(publicKey, msg, sig) { var derive = exports.derive = function(privateKeyA, publicKeyB) { return new Promise(function(resolve) { + assert(Buffer.isBuffer(privateKeyA), "Bad input"); + assert(Buffer.isBuffer(publicKeyB), "Bad input"); assert(privateKeyA.length === 32, "Bad private key"); assert(publicKeyB.length === 65, "Bad public key"); assert(publicKeyB[0] === 4, "Bad public key"); diff --git a/ecdh.cc b/ecdh.cc index 840f8ca..9a21024 100644 --- a/ecdh.cc +++ b/ecdh.cc @@ -70,15 +70,15 @@ NAN_METHOD(Derive) { NanScope(); if (args.Length() != 2 || - !args[0]->IsObject() || // privkey_a - !args[1]->IsObject()) { // pubkey_b + !node::Buffer::HasInstance(args[0]) || // privkey_a + !node::Buffer::HasInstance(args[1])) { // pubkey_b return NanThrowError("Bad input"); } - char* privkey_a = node::Buffer::Data(args[0]->ToObject()); - size_t privkey_a_len = node::Buffer::Length(args[0]->ToObject()); - char* pubkey_b = node::Buffer::Data(args[1]->ToObject()); - size_t pubkey_b_len = node::Buffer::Length(args[1]->ToObject()); + char* privkey_a = node::Buffer::Data(args[0]); + size_t privkey_a_len = node::Buffer::Length(args[0]); + char* pubkey_b = node::Buffer::Data(args[1]); + size_t pubkey_b_len = node::Buffer::Length(args[1]); if (privkey_a == NULL || privkey_a_len != PRIVKEY_SIZE || pubkey_b == NULL || diff --git a/test.js b/test.js index d982616..93c3e77 100644 --- a/test.js +++ b/test.js @@ -142,6 +142,13 @@ describe("ECDH", function() { }); }); }); + + it("should reject promise on bad arguments", function(done) { + eccrypto.derive({}, {}).catch(function(e) { + expect(e.message).to.match(/bad input/i); + done(); + }); + }); }); describe("ECIES", function() {