From 1508093c6761d08087c8d7b083b7d2038fcd58e1 Mon Sep 17 00:00:00 2001 From: bkp7 Date: Fri, 12 Feb 2021 17:30:42 +0000 Subject: [PATCH] fix to allow zero length client identifiers in MQTT v5.0 --- test.js | 31 +++++++++++++++++++++++++++++++ writeToStream.js | 4 ++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index de83970..5cb7e2e 100644 --- a/test.js +++ b/test.js @@ -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' diff --git a/writeToStream.js b/writeToStream.js index 980294e..2416d7a 100644 --- a/writeToStream.js +++ b/writeToStream.js @@ -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) {