fix(frame): improve some error reasons when parsing invalid packet

This commit is contained in:
firest 2023-08-28 18:43:17 +08:00
parent aad9b1c27d
commit 6722722522
3 changed files with 24 additions and 6 deletions

View File

@ -472,8 +472,8 @@ parse_packet(
) ->
{Properties, <<>>} = parse_properties(Rest, ?MQTT_PROTO_V5, StrictMode),
#mqtt_packet_auth{reason_code = ReasonCode, properties = Properties};
parse_packet(_Header, _FrameBin, _Options) ->
?PARSE_ERR(malformed_packet).
parse_packet(Header, _FrameBin, _Options) ->
?PARSE_ERR(#{hit => malformed_packet, header_type => Header#mqtt_packet_header.type}).
parse_will_message(
Packet = #mqtt_packet_connect{
@ -512,8 +512,16 @@ parse_properties(<<0, Rest/binary>>, ?MQTT_PROTO_V5, _StrictMode) ->
{#{}, Rest};
parse_properties(Bin, ?MQTT_PROTO_V5, StrictMode) ->
{Len, Rest} = parse_variable_byte_integer(Bin),
<<PropsBin:Len/binary, Rest1/binary>> = Rest,
{parse_property(PropsBin, #{}, StrictMode), Rest1}.
case Rest of
<<PropsBin:Len/binary, Rest1/binary>> ->
{parse_property(PropsBin, #{}, StrictMode), Rest1};
_ ->
?PARSE_ERR(#{
hint => user_property_not_enough_bytes,
parsed_key_length => Len,
remaining_bytes_length => byte_size(Rest)
})
end.
parse_property(<<>>, Props, _StrictMode) ->
Props;

View File

@ -540,8 +540,17 @@ t_parse_incoming_order(_) ->
t_parse_incoming_frame_error(_) ->
{Packets, _St} = ?ws_conn:parse_incoming(<<3, 2, 1, 0>>, [], st()),
FrameError = {frame_error, malformed_packet},
[{incoming, FrameError}] = Packets.
?assertMatch(
[
{incoming,
{frame_error, #{
header_type := _,
hit := malformed_packet
}}}
],
Packets
).
t_handle_incomming_frame_error(_) ->
FrameError = {frame_error, bad_qos},

View File

@ -0,0 +1 @@
Improve some error reasons for parsing with invalid packets.