fix: fix newly found unsafe `binary_to_atom`

This commit is contained in:
firest 2022-11-23 11:07:20 +08:00
parent f44e70a026
commit 19405114e2
2 changed files with 24 additions and 9 deletions

View File

@ -62,8 +62,15 @@ bridge_id(BridgeType, BridgeName) ->
-spec parse_bridge_id(list() | binary() | atom()) -> {atom(), binary()}. -spec parse_bridge_id(list() | binary() | atom()) -> {atom(), binary()}.
parse_bridge_id(BridgeId) -> parse_bridge_id(BridgeId) ->
case string:split(bin(BridgeId), ":", all) of case string:split(bin(BridgeId), ":", all) of
[Type, Name] -> {binary_to_atom(Type, utf8), Name}; [Type, Name] ->
_ -> error({invalid_bridge_id, BridgeId}) case emqx_misc:safe_to_existing_atom(Type, utf8) of
{ok, Type1} ->
{Type1, Name};
_ ->
error({invalid_bridge_id, BridgeId})
end;
_ ->
error({invalid_bridge_id, BridgeId})
end. end.
reset_metrics(ResourceId) -> reset_metrics(ResourceId) ->

View File

@ -121,13 +121,21 @@ fields(sampler_current) ->
monitor(get, #{query_string := QS, bindings := Bindings}) -> monitor(get, #{query_string := QS, bindings := Bindings}) ->
Latest = maps:get(<<"latest">>, QS, infinity), Latest = maps:get(<<"latest">>, QS, infinity),
Node = binary_to_atom(maps:get(node, Bindings, <<"all">>)), RawNode = maps:get(node, Bindings, all),
case emqx_dashboard_monitor:samplers(Node, Latest) of case emqx_misc:safe_to_existing_atom(RawNode, utf8) of
{badrpc, {Node, Reason}} -> {ok, Node} ->
Message = list_to_binary(io_lib:format("Bad node ~p, rpc failed ~p", [Node, Reason])), case emqx_dashboard_monitor:samplers(Node, Latest) of
{400, 'BAD_RPC', Message}; {badrpc, {Node, Reason}} ->
Samplers -> Message = list_to_binary(
{200, Samplers} io_lib:format("Bad node ~p, rpc failed ~p", [Node, Reason])
),
{400, 'BAD_RPC', Message};
Samplers ->
{200, Samplers}
end;
_ ->
Message = list_to_binary(io_lib:format("Bad node ~p", [RawNode])),
{400, 'BAD_RPC', Message}
end. end.
monitor_current(get, #{bindings := Bindings}) -> monitor_current(get, #{bindings := Bindings}) ->