From a0be38f0742d4d70086282a7c068d978b02ad1f0 Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Wed, 16 Sep 2015 12:04:44 -0400 Subject: [PATCH] check for timestamp out of bounds --- lib/services/db.js | 12 ++++++++++-- test/services/db.unit.js | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/services/db.js b/lib/services/db.js index 6e4f8b93..faf42c8b 100644 --- a/lib/services/db.js +++ b/lib/services/db.js @@ -209,9 +209,16 @@ DB.prototype.getBlockHashesByTimestamp = function(high, low, callback) { var self = this; var hashes = []; + try { + var lowKey = this._encodeBlockIndexKey(low); + var highKey = this._encodeBlockIndexKey(high); + } catch(e) { + return callback(e); + } + var stream = this.store.createReadStream({ - gte: this._encodeBlockIndexKey(low), - lte: this._encodeBlockIndexKey(high), + gte: lowKey, + lte: highKey, reverse: true, valueEncoding: 'binary', keyEncoding: 'binary' @@ -456,6 +463,7 @@ DB.prototype.runAllBlockHandlers = function(block, add, callback) { }; DB.prototype._encodeBlockIndexKey = function(timestamp) { + $.checkArgument(timestamp >= 0 && timestamp <= 4294967295, 'timestamp out of bounds'); var timestampBuffer = new Buffer(4); timestampBuffer.writeUInt32BE(timestamp); return Buffer.concat([DB.PREFIXES.BLOCKS, timestampBuffer]); diff --git a/test/services/db.unit.js b/test/services/db.unit.js index 82148b5d..c3106145 100644 --- a/test/services/db.unit.js +++ b/test/services/db.unit.js @@ -423,6 +423,20 @@ describe('DB Service', function() { readStream.emit('close'); }); + + it('should give an error if the timestamp is out of range', function(done) { + var db = new DB(baseConfig); + var readStream = new EventEmitter(); + db.store = { + createReadStream: sinon.stub().returns(readStream) + }; + + db.getBlockHashesByTimestamp(-1, -5, function(err, hashes) { + should.exist(err); + err.message.should.equal('Invalid Argument: timestamp out of bounds'); + done(); + }); + }); }); describe('#getPrevHash', function() {