Merge pull request #17 from feross/safe-buffer

Use safe-buffer
This commit is contained in:
Feross Aboukhadijeh 2016-05-30 01:38:07 -07:00
commit e6aafdb332
3 changed files with 8 additions and 7 deletions

View File

@ -46,9 +46,10 @@ module.exports = function (metadata) {
if (!handshake.metadata_size) { if (!handshake.metadata_size) {
return this.emit('warning', new Error('Peer does not have metadata')) return this.emit('warning', new Error('Peer does not have metadata'))
} }
if (typeof handshake.metadata_size !== 'number' ||
if (handshake.metadata_size > MAX_METADATA_SIZE) { MAX_METADATA_SIZE < handshake.metadata_size ||
return this.emit('warning', new Error('Peer gave maliciously large metadata size')) handshake.metadata_size <= 0) {
return this.emit('warning', new Error('Peer gave invalid metadata size'))
} }
this._metadataSize = handshake.metadata_size this._metadataSize = handshake.metadata_size
@ -200,8 +201,7 @@ module.exports = function (metadata) {
} }
utMetadata.prototype._requestPieces = function () { utMetadata.prototype._requestPieces = function () {
this.metadata = new Buffer(this._metadataSize) this.metadata = Buffer.alloc(this._metadataSize)
for (var piece = 0; piece < this._numPieces; piece++) { for (var piece = 0; piece < this._numPieces; piece++) {
this._request(piece) this._request(piece)
} }

View File

@ -15,6 +15,7 @@
"bitfield": "^1.0.1", "bitfield": "^1.0.1",
"debug": "^2.2.0", "debug": "^2.2.0",
"inherits": "^2.0.1", "inherits": "^2.0.1",
"safe-buffer": "^5.0.1",
"simple-sha1": "^2.0.0" "simple-sha1": "^2.0.0"
}, },
"devDependencies": { "devDependencies": {

View File

@ -4,8 +4,8 @@ var Protocol = require('bittorrent-protocol')
var test = require('tape') var test = require('tape')
var utMetadata = require('../') var utMetadata = require('../')
var id1 = new Buffer('01234567890123456789') var id1 = Buffer.from('01234567890123456789')
var id2 = new Buffer('12345678901234567890') var id2 = Buffer.from('12345678901234567890')
test('fetch()', function (t) { test('fetch()', function (t) {
t.plan(3) t.plan(3)