Skip to content

Commit

Permalink
fix to allow zero length client identifiers in MQTT v5.0 (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkp7 authored Feb 12, 2021
1 parent 77bfc1c commit 1d89973
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
31 changes: 31 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,37 @@ testParseGenerate('no clientId with 3.1.1', {
0, 0 // Client ID length
]))

testParseGenerateDefaults('no clientId with 5.0', {
cmd: 'connect',
protocolId: 'MQTT',
protocolVersion: 5,
clean: true,
keepalive: 60,
properties:
{
receiveMaximum: 20
},
clientId: ''
}, Buffer.from(
[16, 16, 0, 4, 77, 81, 84, 84, 5, 2, 0, 60, 3, 33, 0, 20, 0, 0]
), {
cmd: 'connect',
retain: false,
qos: 0,
dup: false,
length: 16,
topic: null,
payload: null,
protocolId: 'MQTT',
protocolVersion: 5,
clean: true,
keepalive: 60,
properties: {
receiveMaximum: 20
},
clientId: ''
}, { protocolVersion: 5 })

testParseGenerateDefaults('default connect', {
cmd: 'connect',
clientId: 'test'
Expand Down
4 changes: 2 additions & 2 deletions writeToStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ function connect (packet, stream, opts) {
return false
} else length += 1

// ClientId might be omitted in 3.1.1, but only if cleanSession is set to 1
// ClientId might be omitted in 3.1.1 and 5, but only if cleanSession is set to 1
if ((typeof clientId === 'string' || Buffer.isBuffer(clientId)) &&
(clientId || protocolVersion === 4) && (clientId || clean)) {
(clientId || protocolVersion >= 4) && (clientId || clean)) {
length += clientId.length + 2
} else {
if (protocolVersion < 4) {
Expand Down

0 comments on commit 1d89973

Please sign in to comment.