Skip to content

Commit d705f9d

Browse files
committed
Fix parsing of MQTT PUBACK packet
The MQTT v5 spec is a bit contradictory and imprecise when it comes to the Property Length. It first mandates that if there are no properties, this MUST be indidated by including a Property Length of 0: ``` 2.2.2.1 Property Length The Property Length is encoded as a Variable Byte Integer. The Property Length does not include the bytes used to encode itself, but includes the length of the Properties. If there are no properties, this MUST be indicated by including a Property Length of zero [MQTT-2.2.2-1]. ``` All MQTT packet's Property Length sections that only mention: ``` The length of the Properties in the PUBLISH packet Variable Header encoded as a Variable Byte Integer. ``` So, they follow above requirement. However, MQTT packets PUBACK, PUBREC, PUBREL, PUBCOMP, and DISCONNECT seem to be exceptions to this rule: ``` 3.4.2.2 PUBACK Properties 3.4.2.2.1 Property Length The length of the Properties in the PUBACK packet Variable Header encoded as a Variable Byte Integer. If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used. ``` ``` 3.14.2.2 DISCONNECT Properties 3.14.2.2.1 Property Length The length of Properties in the DISCONNECT packet Variable Header encoded as a Variable Byte Integer. If the Remaining Length is less than 2, a value of 0 is used. ``` Since this special case has already been implemented for DISCONNECT, and RabbitMQ does not support QoS 2, this commit implements this special case for the PUBACK packet. Some MQTT clients (e.g. mqttjs) indeed set a Remaining Length of 3 in the PUBACK packet. See #2554 (comment)
1 parent fa25e4c commit d705f9d

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

deps/rabbitmq_mqtt/src/rabbit_mqtt_packet.erl

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
-module(rabbit_mqtt_packet).
99

1010
-include("rabbit_mqtt_packet.hrl").
11-
-include("rabbit_mqtt.hrl").
1211
-include_lib("kernel/include/logger.hrl").
1312

1413
-export([init_state/0, parse/2, serialise/2]).
@@ -119,6 +118,11 @@ parse_packet(Bin, #mqtt_packet_fixed{type = Type,
119118
{ReasonCode, Props} = case PacketBin of
120119
<<>> ->
121120
{?RC_SUCCESS, #{}};
121+
<<Rc>> when ProtoVer =:= 5 ->
122+
%% "If the Remaining Length is less than 4 there is
123+
%% no Property Length and the value of 0 is used."
124+
%% [v5 3.4.2.2.1]
125+
{Rc, #{}};
122126
<<Rc, PropsBin/binary>> when ProtoVer =:= 5 ->
123127
{Props0, <<>>} = parse_props(PropsBin, ProtoVer),
124128
{Rc, Props0}

0 commit comments

Comments
 (0)