bip151: add minimum packet size.

This commit is contained in:
Christopher Jeffrey 2016-07-27 01:40:07 -07:00
parent 594bbc8b1d
commit 01471d6e63
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -289,7 +289,7 @@ BIP151Stream.prototype.verify = function verify(tag) {
*/
BIP151Stream.prototype.feed = function feed(data) {
var chunk, payload, tag, p, cmd, body;
var chunk, size, payload, tag, p, cmd, body;
while (data) {
if (!this.hasHeader) {
@ -305,18 +305,21 @@ BIP151Stream.prototype.feed = function feed(data) {
this.pendingHeaderTotal = 0;
this.pendingHeader.length = 0;
this.waiting = this.decryptSize(chunk) + 16;
size = this.decryptSize(chunk);
// Allow 3 batched packets of max message size.
// Allow 3 batched packets of max message size (12mb).
// Not technically standard, but this protects us
// from buffering tons of data due to either an
// potential dos'er or a cipher state mismatch.
if (this.waiting - 32 > constants.MAX_MESSAGE * 3) {
this.waiting = 0;
this.emit('error', new Error('Packet too large.'));
// Note that 6 is the minimum size:
// cmd=varint(1) string(1) length(4) data(0)
if (size < 6 || size > constants.MAX_MESSAGE * 3) {
assert(this.waiting === 0);
this.emit('error', new Error('Bad packet size.'));
continue;
}
this.waiting = size + 16;
this.hasHeader = true;
data = chunk.slice(4);