Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,18 +540,20 @@ class Parser extends EventEmitter {

if (this.settings.protocolVersion === 5) {
// response code
if (this._list.length > 0) {
if (packet.length > 0) {
packet.reasonCode = this._parseByte()
if (!constants.MQTT5_DISCONNECT_CODES[packet.reasonCode]) {
this._emitError(new Error('Invalid disconnect reason code'))
Comment thread
robertsLando marked this conversation as resolved.
Resolved
}
} else {
packet.reasonCode = 0
}
// properies mqtt 5
const properties = this._parseProperties()
if (Object.getOwnPropertyNames(properties).length) {
packet.properties = properties
// properies mqtt 5 (only present when the remaining length is >= 2)
if (packet.length >= 2) {
Comment thread
robertsLando marked this conversation as resolved.
Resolved
const properties = this._parseProperties()
if (Object.getOwnPropertyNames(properties).length) {
packet.properties = properties
}
}
}

Expand All @@ -569,18 +571,20 @@ class Parser extends EventEmitter {
}

// response code
if (this._list.length > 0) {
if (packet.length > 0) {
packet.reasonCode = this._parseByte()
if (!constants.MQTT5_AUTH_CODES[packet.reasonCode]) {
return this._emitError(new Error('Invalid auth reason code'))
}
} else {
packet.reasonCode = 0
}
// properies mqtt 5
const properties = this._parseProperties()
if (Object.getOwnPropertyNames(properties).length) {
packet.properties = properties
// properies mqtt 5 (only present when the remaining length is >= 2)
if (packet.length >= 2) {
Comment thread
robertsLando marked this conversation as resolved.
Resolved
const properties = this._parseProperties()
if (Object.getOwnPropertyNames(properties).length) {
packet.properties = properties
}
}

debug('_parseAuth: result: true')
Expand Down
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,27 @@ testParseOnly('Version 5 DISCONNECT test 2', {
]), { protocolVersion: 5 }
)

test('Version 5 minimal DISCONNECT/AUTH before another packet parses both', t => {
// A minimal DISCONNECT/AUTH (remaining length 0 or 1) must be bounded by its
// own remaining length, not by the whole parse buffer, so a following packet
// is not mis-read as its reason code or properties.
const cases = [
{ bytes: [224, 0, 192, 0], cmds: ['disconnect', 'pingreq'], rcs: [0, undefined] },
{ bytes: [224, 1, 128, 192, 0], cmds: ['disconnect', 'pingreq'], rcs: [128, undefined] },
{ bytes: [240, 0, 192, 0], cmds: ['auth', 'pingreq'], rcs: [0, undefined] }
]
Comment thread
robertsLando marked this conversation as resolved.
Resolved
for (const { bytes, cmds, rcs } of cases) {
const parser = mqtt.parser({ protocolVersion: 5 })
const got = []
parser.on('packet', p => got.push(p))
parser.on('error', e => t.fail(`unexpected error: ${e.message}`))
parser.parse(Buffer.from(bytes))
Comment thread
robertsLando marked this conversation as resolved.
Resolved
t.deepEqual(got.map(p => p.cmd), cmds, 'both packets parsed')
t.deepEqual(got.map(p => p.reasonCode), rcs, 'reason codes')
}
t.end()
})

testParseAndGenerate('Version 5 DISCONNECT test 3', {
cmd: 'disconnect',
retain: false,
Expand Down
Loading