Skip to content

Commit 5023255

Browse files
authored
Add mandatory payload checks (#121)
* Add mandatory payload checks * Fix logic implementation
1 parent aae4e2c commit 5023255

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

parser.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ class Parser extends EventEmitter {
347347
}
348348
}
349349

350+
if (packet.length <= 0) { return this._emitError(new Error('Malformed subscribe, no payload specified')) }
351+
350352
while (this._pos < packet.length) {
351353
// Parse topic
352354
topic = this._parseString()
@@ -411,6 +413,8 @@ class Parser extends EventEmitter {
411413
}
412414
}
413415

416+
if (packet.length <= 0) { return this._emitError(new Error('Malformed suback, no payload specified')) }
417+
414418
// Parse granted QoSes
415419
while (this._pos < this.packet.length) {
416420
const code = this._list.readUInt8(this._pos++)
@@ -444,6 +448,8 @@ class Parser extends EventEmitter {
444448
}
445449
}
446450

451+
if (packet.length <= 0) { return this._emitError(new Error('Malformed unsubscribe, no payload specified')) }
452+
447453
while (this._pos < packet.length) {
448454
// Parse topic
449455
const topic = this._parseString()
@@ -459,6 +465,13 @@ class Parser extends EventEmitter {
459465
debug('_parseUnsuback')
460466
const packet = this.packet
461467
if (!this._parseMessageId()) return this._emitError(new Error('Cannot parse messageId'))
468+
469+
if ((this.settings.protocolVersion === 3 ||
470+
this.settings.protocolVersion === 4) && packet.length !== 2) {
471+
return this._emitError(new Error('Malformed unsuback, payload length must be 2'))
472+
}
473+
if (packet.length <= 0) { return this._emitError(new Error('Malformed unsuback, no payload specified')) }
474+
462475
// Properties mqtt 5
463476
if (this.settings.protocolVersion === 5) {
464477
const properties = this._parseProperties()
@@ -467,6 +480,7 @@ class Parser extends EventEmitter {
467480
}
468481
// Parse granted QoSes
469482
packet.granted = []
483+
470484
while (this._pos < this.packet.length) {
471485
const code = this._list.readUInt8(this._pos++)
472486
if (!constants.MQTT5_UNSUBACK_CODES[code]) {

test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,53 @@ testParseError('Will QoS must be set to zero when Will Flag is set to 0', Buffer
12741274
0, 30 // Keepalive
12751275
]))
12761276

1277+
// CONNECT, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK (v.5) packets must have payload
1278+
// CONNECT
1279+
testParseError('Packet too short', Buffer.from([
1280+
16, // Header
1281+
8, // Packet length
1282+
0, 4, // Protocol ID length
1283+
77, 81, 84, 84, // MQTT
1284+
5, // Version
1285+
2, // Clean Start enabled
1286+
0, 0, // Keep-Alive
1287+
0, // Property Length
1288+
0, 0 // Properties
1289+
// No payload
1290+
]), { protocolVersion: 5 })
1291+
// SUBSCRIBE
1292+
testParseError('Malformed subscribe, no payload specified', Buffer.from([
1293+
130, // Header
1294+
0 // Packet length
1295+
]), { protocolVersion: 5 })
1296+
// SUBACK
1297+
testParseError('Malformed suback, no payload specified', Buffer.from([
1298+
144, // Header
1299+
0 // Packet length
1300+
]), { protocolVersion: 5 })
1301+
// UNSUBSCRIBE
1302+
testParseError('Malformed unsubscribe, no payload specified', Buffer.from([
1303+
162, // Header
1304+
0 // Packet length
1305+
]), { protocolVersion: 5 })
1306+
// UNSUBACK (v.5)
1307+
testParseError('Malformed unsuback, no payload specified', Buffer.from([
1308+
176, // Header
1309+
0 // Packet length
1310+
]), { protocolVersion: 5 })
1311+
// UNSUBACK (v.4)
1312+
testParseError('Malformed unsuback, payload length must be 2', Buffer.from([
1313+
176, // Header
1314+
1, // Packet length
1315+
1
1316+
]), { protocolVersion: 4 })
1317+
// UNSUBACK (v.3)
1318+
testParseError('Malformed unsuback, payload length must be 2', Buffer.from([
1319+
176, // Header
1320+
1, // Packet length
1321+
1
1322+
]), { protocolVersion: 3 })
1323+
12771324
testParseGenerate('connack with return code 0', {
12781325
cmd: 'connack',
12791326
retain: false,

0 commit comments

Comments
 (0)