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

This commit is contained in:
Zhongwen Deng 2022-05-05 10:30:59 +08:00
parent 8ff552c9cf
commit 2987bd47ad
2 changed files with 17 additions and 3 deletions

View File

@ -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.

View File

@ -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(_) ->