From 03c117b009da0c6eb1db62d47b9a3f1185bed2a2 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 20 Jan 2016 12:04:38 -0800 Subject: [PATCH] more DOS protection. --- lib/bcoin/pool.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 9720d4c2..5b3e18b8 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -490,8 +490,16 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer) { for (i = 0; i < hashes.length; i++) { hash = hashes[i]; - // Resolve orphan chain if (this.chain.hasOrphan(hash)) { + // Make sure the peer doesn't send us + // more than 200 orphans every 3 minutes. + if (this.orphaning(peer)) { + this.emit('debug', 'Peer is orphaning (%s)', peer.host); + this.misbehaving(peer, 100); + return; + } + + // Resolve orphan chain. peer.loadBlocks( this.chain.locatorHashes(), this.chain.getOrphanRoot(hash) @@ -542,7 +550,7 @@ Pool.prototype._handleInv = function _handleInv(hashes, peer) { Pool.prototype._handleBlock = function _handleBlock(block, peer) { var self = this; - var requested, hasPrev; + var requested; // Fulfill our request. requested = this._response(block); @@ -566,7 +574,9 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer) { // Ensure this is not a continuation // of an invalid chain. if (this.block.invalid[block.prevBlock]) { - this.emit('debug', 'Peer is sending an invalid chain (%s)', peer.host); + this.emit('debug', + 'Peer is sending an invalid continuation chain (%s)', + peer.host); this.misbehaving(peer, 100); return false; } @@ -604,7 +614,7 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer) { return false; // Make sure the peer doesn't send us - // more than 100 orphans every 3 minutes. + // more than 200 orphans every 3 minutes. if (this.orphaning(peer)) { this.emit('debug', 'Peer is orphaning (%s)', peer.host); this.misbehaving(peer, 100); @@ -618,6 +628,7 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer) { if (this._addIndex(block, peer)) this.emit('pool block', block, peer); + // Resolve orphan chain. this.peers.load.loadBlocks( this.chain.locatorHashes(), this.chain.getOrphanRoot(block) @@ -629,6 +640,11 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer) { } } else { if (!this.chain.hasBlock(block.prevBlock)) { + // Special case for genesis block. + if (block.isGenesis()) + return false; + + // Increase banscore by 10 if we're using getheaders. if (!this.options.multiplePeers) { if (this.misbehaving(peer, 10)) return false; @@ -1688,7 +1704,7 @@ Pool.prototype.orphaning = function orphaning(peer) { peer._orphans += 1; - if (peer._orphans > 100) + if (peer._orphans > 200) return true; return false;