From f0ea5c9ecdef87cd9add43a349a589a7275d6aa4 Mon Sep 17 00:00:00 2001 From: Christian <6939810+chkr1011@users.noreply.github.com> Date: Sat, 7 Sep 2024 10:52:53 +0200 Subject: [PATCH] Fix encoding and decoding of AUTH paket according to RFC. (#2076) --- .github/workflows/ReleaseNotes.md | 1 + .../MQTTnet/Formatter/V5/MqttV5PacketDecoder.cs | 4 ++-- .../MQTTnet/Formatter/V5/MqttV5PacketEncoder.cs | 15 +++++++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ReleaseNotes.md b/.github/workflows/ReleaseNotes.md index 8a10727ac..999e0b2f0 100644 --- a/.github/workflows/ReleaseNotes.md +++ b/.github/workflows/ReleaseNotes.md @@ -1,3 +1,4 @@ +* Core: Fixed issue when parsing AUTH packet with 0 length body (#2039). * nuget: Changed code signing and nuget certificate (**BREAKING CHANGE**). * TopicTemplates: Updated samples, parameter validation (#2022). * ManagedClient: Switch SubscribeAsync/UnsubscribeAsync to IEnumerable (#2026). diff --git a/Source/MQTTnet/Formatter/V5/MqttV5PacketDecoder.cs b/Source/MQTTnet/Formatter/V5/MqttV5PacketDecoder.cs index c9010cabe..e27f129fc 100644 --- a/Source/MQTTnet/Formatter/V5/MqttV5PacketDecoder.cs +++ b/Source/MQTTnet/Formatter/V5/MqttV5PacketDecoder.cs @@ -68,12 +68,12 @@ public MqttPacket Decode(ReceivedMqttPacket receivedMqttPacket) MqttPacket DecodeAuthPacket(ArraySegment body) { - ThrowIfBodyIsEmpty(body); - _bufferReader.SetBuffer(body.Array, body.Offset, body.Count); var packet = new MqttAuthPacket(); + // MQTT spec: The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success) and there are no Properties. + // In this case the AUTH has a Remaining Length of 0. if (_bufferReader.EndOfStream) { packet.ReasonCode = MqttAuthenticateReasonCode.Success; diff --git a/Source/MQTTnet/Formatter/V5/MqttV5PacketEncoder.cs b/Source/MQTTnet/Formatter/V5/MqttV5PacketEncoder.cs index 8e6804f3c..63e147f5e 100644 --- a/Source/MQTTnet/Formatter/V5/MqttV5PacketEncoder.cs +++ b/Source/MQTTnet/Formatter/V5/MqttV5PacketEncoder.cs @@ -13,7 +13,7 @@ namespace MQTTnet.Formatter.V5 public sealed class MqttV5PacketEncoder { const int FixedHeaderSize = 1; - + readonly MqttBufferWriter _bufferWriter; readonly MqttV5PropertiesWriter _propertiesWriter = new MqttV5PropertiesWriter(new MqttBufferWriter(1024, 4096)); @@ -64,13 +64,20 @@ public MqttPacketBuffer Encode(MqttPacket packet) byte EncodeAuthPacket(MqttAuthPacket packet) { - _bufferWriter.WriteByte((byte)packet.ReasonCode); - _propertiesWriter.WriteAuthenticationMethod(packet.AuthenticationMethod); _propertiesWriter.WriteAuthenticationData(packet.AuthenticationData); _propertiesWriter.WriteReasonString(packet.ReasonString); _propertiesWriter.WriteUserProperties(packet.UserProperties); + // MQTT spec: The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success) and there are no Properties. + // In this case the AUTH has a Remaining Length of 0. + if (packet.ReasonCode == MqttAuthenticateReasonCode.Success && _propertiesWriter.Length == 0) + { + return MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Auth); + } + + _bufferWriter.WriteByte((byte)packet.ReasonCode); + _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); @@ -526,7 +533,7 @@ byte EncodeUnsubscribePacket(MqttUnsubscribePacket packet) static void ThrowIfPacketIdentifierIsInvalid(ushort packetIdentifier, MqttPacket packet) { - // SUBSCRIBE, UNSUBSCRIBE, and PUBLISH(in cases where QoS > 0) Control Packets MUST contain a non-zero 16 - bit Packet Identifier[MQTT - 2.3.1 - 1]. + // SUBSCRIBE, UNSUBSCRIBE, and PUBLISH(in cases where QoS > 0) Control Packets MUST contain a non-zero 16 - bit Packet Identifier[MQTT - 2.3.1 - 1]. if (packetIdentifier == 0) {