Check for number of addr entries

This commit is contained in:
Kagami Hiiragi 2015-01-15 23:19:52 +03:00
parent 75d1c1f2f4
commit ec616a0151
2 changed files with 7 additions and 0 deletions

View File

@ -112,6 +112,7 @@ exports.addr = {
decode: function(buf) {
var decoded = structs.var_int.decode(buf);
var listLength = decoded.value;
assert(listLength <= 1000, "Too many address entires");
var length = decoded.length + listLength * 38;
assert(buf.length >= length, "Buffer is too small");
var rest = decoded.rest;
@ -132,6 +133,7 @@ exports.addr = {
* @return {Buffer} Encoded `addr` payload.
*/
encode: function(addrs) {
assert(addrs.length <= 1000, "Too many address entires");
var addrsBuf = Buffer.concat(addrs.map(structs.net_addr.encode));
return Buffer.concat([structs.var_int.encode(addrs.length), addrsBuf]);
},

View File

@ -320,6 +320,11 @@ describe("Message types", function() {
expect(res.addrs[1].host).to.equal("ff:0:0:0:0:0:0:1");
expect(res.addrs[1].port).to.equal(18444);
});
it("shouldn't encode/decode more than 1000 entires", function() {
expect(addr.encode.bind(null, Array(2000))).to.throw(/too many/i);
expect(addr.decode.bind(null, var_int.encode(2000))).to.throw(/too many/i);
});
});
});