Skip to content

Commit e13a611

Browse files
committed
Web MQTT: Send CONNACK error code before closing connection
"If the Client supplies a zero-byte ClientId with CleanSession set to 0, the Server MUST respond to the CONNECT Packet with a CONNACK return code 0x02 (Identifier rejected) and then close the Network Connection" [MQTT-3.1.3-8]. In Web MQTT, the CONNACK was not sent to the client because the Web MQTT connection process terminated before being sending the CONNACK to the client. (cherry picked from commit 0058380)
1 parent a4633ed commit e13a611

File tree

4 files changed

+16
-21
lines changed

4 files changed

+16
-21
lines changed

deps/rabbitmq_mqtt/src/rabbit_mqtt.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,5 @@ persist_static_configuration() ->
106106
rabbit_mqtt_util:init_sparkplug(),
107107

108108
{ok, MailboxSoftLimit} = application:get_env(?APP_NAME, mailbox_soft_limit),
109-
?assert(is_number(MailboxSoftLimit)),
109+
?assert(is_integer(MailboxSoftLimit)),
110110
ok = persistent_term:put(?PERSISTENT_TERM_MAILBOX_SOFT_LIMIT, MailboxSoftLimit).

deps/rabbitmq_mqtt/test/reader_SUITE.erl

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ groups() ->
3737
quorum_clean_session_true,
3838
classic_clean_session_true,
3939
classic_clean_session_false,
40-
non_clean_sess_empty_client_id,
4140
event_authentication_failure,
4241
rabbit_mqtt_qos0_queue_overflow
4342
]}
@@ -218,22 +217,6 @@ classic_clean_session_true(Config) ->
218217
classic_clean_session_false(Config) ->
219218
validate_durable_queue_type(Config, <<"classicCleanSessionFalse">>, false, rabbit_classic_queue).
220219

221-
%% "If the Client supplies a zero-byte ClientId with CleanSession set to 0,
222-
%% the Server MUST respond to the CONNECT Packet with a CONNACK return code 0x02
223-
%% (Identifier rejected) and then close the Network Connection" [MQTT-3.1.3-8].
224-
non_clean_sess_empty_client_id(Config) ->
225-
{ok, C} = emqtt:start_link(
226-
[{clientid, <<>>},
227-
{clean_start, false},
228-
{proto_ver, v4},
229-
{host, "localhost"},
230-
{port, rabbit_ct_broker_helpers:get_node_config(Config, 0, tcp_port_mqtt)}
231-
]),
232-
process_flag(trap_exit, true),
233-
?assertMatch({error, {client_identifier_not_valid, _}},
234-
emqtt:connect(C)),
235-
ok = await_exit(C).
236-
237220
event_authentication_failure(Config) ->
238221
{ok, C} = emqtt:start_link(
239222
[{username, <<"Trudy">>},

deps/rabbitmq_mqtt/test/shared_SUITE.erl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ subgroups() ->
7979
,non_clean_sess_reconnect_qos1
8080
,non_clean_sess_reconnect_qos0
8181
,non_clean_sess_reconnect_qos0_and_qos1
82+
,non_clean_sess_empty_client_id
8283
,subscribe_same_topic_same_qos
8384
,subscribe_same_topic_different_qos
8485
,subscribe_multiple
@@ -852,6 +853,16 @@ non_clean_sess_reconnect_qos0_and_qos1(Config) ->
852853
C3 = connect(ClientId, Config, [{clean_start, true}]),
853854
ok = emqtt:disconnect(C3).
854855

856+
%% "If the Client supplies a zero-byte ClientId with CleanSession set to 0,
857+
%% the Server MUST respond to the CONNECT Packet with a CONNACK return code 0x02
858+
%% (Identifier rejected) and then close the Network Connection" [MQTT-3.1.3-8].
859+
non_clean_sess_empty_client_id(Config) ->
860+
{C, Connect} = util:start_client(<<>>, Config, 0, [{clean_start, false}]),
861+
process_flag(trap_exit, true),
862+
?assertMatch({error, {client_identifier_not_valid, _}},
863+
Connect(C)),
864+
ok = await_exit(C).
865+
855866
subscribe_same_topic_same_qos(Config) ->
856867
C = connect(?FUNCTION_NAME, Config),
857868
Topic = <<"a/b">>,

deps/rabbitmq_web_mqtt/src/rabbit_web_mqtt_handler.erl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ websocket_info({bump_credit, Msg}, State) ->
139139
handle_credits(State);
140140
websocket_info({reply, Data}, State) ->
141141
{[{binary, Data}], State, hibernate};
142+
websocket_info({stop, CloseCode, Error}, State) ->
143+
stop(State, CloseCode, Error);
142144
websocket_info({'EXIT', _, _}, State) ->
143145
stop(State);
144146
websocket_info({'$gen_cast', QueueEvent = {queue_event, _, _}},
@@ -271,9 +273,8 @@ handle_data1(Data, State = #state{socket = Socket,
271273
proc_state = ProcState1});
272274
{error, Reason} ->
273275
?LOG_ERROR("Rejected Web MQTT connection ~ts: ~p", [ConnName, Reason]),
274-
stop_mqtt_protocol_error({_SendWill = false, State},
275-
connect_packet_rejected,
276-
ConnName)
276+
self() ! {stop, ?CLOSE_PROTOCOL_ERROR, connect_packet_rejected},
277+
{[], {_SendWill = false, State}}
277278
end;
278279
_ ->
279280
case rabbit_mqtt_processor:process_packet(Packet, ProcState) of

0 commit comments

Comments
 (0)