Skip to content

Commit

Permalink
fix(security): prevent _parseByte to throw (#107)
Browse files Browse the repository at this point in the history
* fix(security): prevent _parseByte to throw

Ref: moscajs/aedes#612

* fix: emit error when type parse fails

* fix: add test
  • Loading branch information
robertsLando authored Apr 16, 2021
1 parent a2a97dd commit f35c6e7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
11 changes: 9 additions & 2 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,11 @@ class Parser extends EventEmitter {
}

_parseByte () {
const result = this._list.readUInt8(this._pos)
this._pos++
let result
if (this._pos < this._list.length) {
result = this._list.readUInt8(this._pos)
this._pos++
}
debug('_parseByte: result: %o', result)
return result
}
Expand Down Expand Up @@ -646,6 +649,10 @@ class Parser extends EventEmitter {
const result = {}
while (this._pos < end) {
const type = this._parseByte()
if (!type) {
this._emitError(new Error('Cannot parse property code type'))
return false
}
const name = constants.propertiesCodes[type]
if (!name) {
this._emitError(new Error('Unknown property'))
Expand Down
27 changes: 24 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,9 @@ testParseGenerateDefaults('no clientId with 5.0', {
clean: true,
keepalive: 60,
properties:
{
receiveMaximum: 20
},
{
receiveMaximum: 20
},
clientId: ''
}, Buffer.from(
[16, 16, 0, 4, 77, 81, 84, 84, 5, 2, 0, 60, 3, 33, 0, 20, 0, 0]
Expand Down Expand Up @@ -2690,6 +2690,27 @@ testParseError('Malformed Subscribe Payload', Buffer.from([
0 // requested QoS
]))

test('Cannot parse property code type', t => {
const packets = Buffer.from([
16, 16, 0, 4, 77, 81, 84, 84, 5, 2, 0, 60, 3, 33, 0, 20, 0, 0, 98, 2, 211, 1, 224, 2, 0, 32
])

t.plan(3)

const parser = mqtt.parser()

parser.on('error', err => {
t.equal(err.message, 'Cannot parse property code type', 'expected error message')
t.end()
})

parser.on('packet', (packet) => {
t.pass('Packet parsed')
})

parser.parse(packets)
})

testWriteToStreamError('Invalid command', {
cmd: 'invalid'
})
Expand Down

0 comments on commit f35c6e7

Please sign in to comment.