Przeglądaj źródła

fix: don't allow empty username if username_as_clientid is true

Zhongwen Deng 3 lat temu
rodzic
commit
2987bd47ad

+ 9 - 3
apps/emqx/src/emqx_channel.erl

@@ -1525,7 +1525,7 @@ check_connect(ConnPkt, #channel{clientinfo = #{zone := Zone}}) ->
 %% Enrich Client Info
 
 enrich_client(ConnPkt, Channel = #channel{clientinfo = ClientInfo}) ->
-    {ok, NConnPkt, NClientInfo} = pipeline(
+    Pipe = pipeline(
         [
             fun set_username/2,
             fun set_bridge_mode/2,
@@ -1536,7 +1536,12 @@ enrich_client(ConnPkt, Channel = #channel{clientinfo = ClientInfo}) ->
         ConnPkt,
         ClientInfo
     ),
-    {ok, NConnPkt, Channel#channel{clientinfo = NClientInfo}}.
+    case Pipe of
+        {ok, NConnPkt, NClientInfo} ->
+            {ok, NConnPkt, Channel#channel{clientinfo = NClientInfo}};
+        {error, ReasonCode, NClientInfo} ->
+            {error, ReasonCode, Channel#channel{clientinfo = NClientInfo}}
+    end.
 
 set_username(
     #mqtt_packet_connect{username = Username},
@@ -1561,7 +1566,8 @@ maybe_username_as_clientid(
     }
 ) ->
     case get_mqtt_conf(Zone, use_username_as_clientid) of
-        true -> {ok, ClientInfo#{clientid => Username}};
+        true when Username =/= <<>> -> {ok, ClientInfo#{clientid => Username}};
+        true -> {error, ?RC_CLIENT_IDENTIFIER_NOT_VALID, ClientInfo};
         false -> ok
     end.
 

+ 8 - 0
apps/emqx/test/emqx_client_SUITE.erl

@@ -301,6 +301,14 @@ t_username_as_clientid(_) ->
     {ok, C} = emqtt:start_link([{username, Username}]),
     {ok, _} = emqtt:connect(C),
     #{clientinfo := #{clientid := Username}} = emqx_cm:get_chan_info(Username),
+    erlang:process_flag(trap_exit, true),
+    {ok, C1} = emqtt:start_link([{username, <<>>}]),
+    ?assertEqual({error, {client_identifier_not_valid, undefined}}, emqtt:connect(C1)),
+    receive
+        {'EXIT', _, {shutdown, client_identifier_not_valid}} -> ok
+    after 100 ->
+        throw({error, "expect_client_identifier_not_valid"})
+    end,
     emqtt:disconnect(C).
 
 t_certcn_as_clientid_default_config_tls(_) ->