fix: check willretain and willqos when WillFlag set to `true`

This commit is contained in:
JimMoen 2024-06-11 03:06:25 +08:00
parent 02a6ee1ef4
commit cf9d6943d5
No known key found for this signature in database
3 changed files with 21 additions and 4 deletions

View File

@ -287,14 +287,25 @@ parse_connect(FrameBin, StrictMode) ->
% Note: return malformed if reserved flag is not 0. % Note: return malformed if reserved flag is not 0.
parse_connect2( parse_connect2(
ProtoName, ProtoName,
<<BridgeTag:4, ProtoVer:4, UsernameFlag:1, PasswordFlag:1, WillRetain:1, WillQoS:2, WillFlag:1, <<BridgeTag:4, ProtoVer:4, UsernameFlag:1, PasswordFlag:1, WillRetainB:1, WillQoS:2,
CleanStart:1, Reserved:1, KeepAlive:16/big, Rest2/binary>>, WillFlagB:1, CleanStart:1, Reserved:1, KeepAlive:16/big, Rest2/binary>>,
StrictMode StrictMode
) -> ) ->
case Reserved of case Reserved of
0 -> ok; 0 -> ok;
1 -> ?PARSE_ERR(reserved_connect_flag) 1 -> ?PARSE_ERR(reserved_connect_flag)
end, end,
WillFlag = bool(WillFlagB),
WillRetain = bool(WillRetainB),
case WillFlag of
%% MQTT-v3.1.1-[MQTT-3.1.2-13], MQTT-v5.0-[MQTT-3.1.2-11]
false when WillQoS > 0 -> ?PARSE_ERR(invalid_will_qos);
%% MQTT-v3.1.1-[MQTT-3.1.2-14], MQTT-v5.0-[MQTT-3.1.2-12]
true when WillQoS > 2 -> ?PARSE_ERR(invalid_will_qos);
%% MQTT-v3.1.1-[MQTT-3.1.2-15], MQTT-v5.0-[MQTT-3.1.2-13]
false when WillRetain -> ?PARSE_ERR(invalid_will_retain);
_ -> ok
end,
{Properties, Rest3} = parse_properties(Rest2, ProtoVer, StrictMode), {Properties, Rest3} = parse_properties(Rest2, ProtoVer, StrictMode),
{ClientId, Rest4} = parse_utf8_string_with_cause(Rest3, StrictMode, invalid_clientid), {ClientId, Rest4} = parse_utf8_string_with_cause(Rest3, StrictMode, invalid_clientid),
ConnPacket = #mqtt_packet_connect{ ConnPacket = #mqtt_packet_connect{
@ -304,9 +315,9 @@ parse_connect2(
%% Invented by mosquitto, named 'try_private': https://mosquitto.org/man/mosquitto-conf-5.html %% Invented by mosquitto, named 'try_private': https://mosquitto.org/man/mosquitto-conf-5.html
is_bridge = (BridgeTag =:= 8), is_bridge = (BridgeTag =:= 8),
clean_start = bool(CleanStart), clean_start = bool(CleanStart),
will_flag = bool(WillFlag), will_flag = WillFlag,
will_qos = WillQoS, will_qos = WillQoS,
will_retain = bool(WillRetain), will_retain = WillRetain,
keepalive = KeepAlive, keepalive = KeepAlive,
properties = Properties, properties = Properties,
clientid = ClientId clientid = ClientId

View File

@ -3491,6 +3491,7 @@ mqtt_general() ->
)}, )},
{"max_clientid_len", {"max_clientid_len",
sc( sc(
%% MQTT-v3.1.1-[MQTT-3.1.3-5], MQTT-v5.0-[MQTT-3.1.3-5]
range(23, 65535), range(23, 65535),
#{ #{
default => 65535, default => 65535,

View File

@ -0,0 +1,5 @@
Fix the flags check and error handling related to the Will message in the `CONNECT` packet.
See also:
- MQTT-v3.1.1-[MQTT-3.1.2-13], MQTT-v5.0-[MQTT-3.1.2-11]
- MQTT-v3.1.1-[MQTT-3.1.2-14], MQTT-v5.0-[MQTT-3.1.2-12]
- MQTT-v3.1.1-[MQTT-3.1.2-15], MQTT-v5.0-[MQTT-3.1.2-13]