Skip to content

Commit b658d13

Browse files
committed
Handle stream errors in Connection, and handle messages over maximum size
1 parent cc48935 commit b658d13

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/connection.js

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const debug = require('debug')('abci')
66
const { varint } = require('protocol-buffers-encodings')
77
const { Request, Response } = require('../types.js').abci
88

9+
const MAX_MESSAGE_SIZE = 104857600 // 100mb
10+
911
class Connection extends EventEmitter {
1012
constructor (stream, onMessage) {
1113
super()
@@ -16,6 +18,12 @@ class Connection extends EventEmitter {
1618
this.waiting = false
1719

1820
stream.on('data', this.onData.bind(this))
21+
stream.on('error', this.error.bind(this))
22+
}
23+
24+
error (err) {
25+
this.close()
26+
this.emit('error', err)
1927
}
2028

2129
async onData (data) {
@@ -28,6 +36,11 @@ class Connection extends EventEmitter {
2836
let length = varint.decode(this.recvBuf.slice(0, 8)) >> 1
2937
let lengthLength = varint.decode.bytes
3038

39+
if (length > MAX_MESSAGE_SIZE) {
40+
this.error(Error('message is longer than maximum size'))
41+
return
42+
}
43+
3144
if (lengthLength + length > this.recvBuf.length) {
3245
// buffering message, don't read yet
3346
return

0 commit comments

Comments
 (0)