Check for lowest var_int length

This commit is contained in:
Kagami Hiiragi 2014-12-18 20:16:21 +03:00
parent 1db64dcc55
commit c055a8d155
2 changed files with 11 additions and 1 deletions

View File

@ -18,16 +18,19 @@ exports.decode = function(buf) {
switch (buf[0]) { switch (buf[0]) {
case 253: case 253:
value = buf.readUInt16BE(1); value = buf.readUInt16BE(1);
assert(value >= 253, "Impractical var_int");
length = 3; length = 3;
break; break;
case 254: case 254:
value = buf.readUInt32BE(1); value = buf.readUInt32BE(1);
assert(value >= 65536, "Impractical var_int");
length = 5; length = 5;
break; break;
case 255: case 255:
var hi = buf.readUInt32BE(1); var hi = buf.readUInt32BE(1);
var lo = buf.readUInt32BE(5); var lo = buf.readUInt32BE(5);
value = new Int64(hi, lo); value = new Int64(hi, lo);
assert(value >= 4294967296, "Impractical var_int");
length = 9; length = 9;
break; break;
default: default:

View File

@ -9,7 +9,8 @@ var bmcrypto = require("./lib/crypto");
describe("var_int", function() { describe("var_int", function() {
it("should decode", function() { it("should decode", function() {
var res; var res;
expect(varint.decode.bind(Buffer([]))).to.throw(Error); expect(varint.decode.bind(null, Buffer([]))).to.throw(Error);
expect(varint.decode.bind(null, Buffer("fd00", "hex"))).to.throw(Error);
res = varint.decode(Buffer([123])); res = varint.decode(Buffer([123]));
expect(res.value).to.equal(123); expect(res.value).to.equal(123);
@ -31,6 +32,12 @@ describe("var_int", function() {
expect(res.length).to.equal(9); expect(res.length).to.equal(9);
expect(res.rest.length).to.equal(0); expect(res.rest.length).to.equal(0);
}); });
it("should check for lowest length on decode", function() {
expect(varint.decode.bind(null, Buffer("fd00fc", "hex"))).to.throw(Error);
expect(varint.decode.bind(null, Buffer("fe0000ffff", "hex"))).to.throw(Error);
expect(varint.decode.bind(null, Buffer("ff00000000ffffffff", "hex"))).to.throw(Error);
});
}); });
describe("Crypto", function() { describe("Crypto", function() {