Переглянути джерело

fix(bridges_v2): check bridge name length before attempting atom conversion

Fixes https://emqx.atlassian.net/browse/EMQX-11289
Thales Macedo Garitezi 2 роки тому
батько
коміт
b420b53075

+ 1 - 1
apps/emqx/src/emqx_config_handler.erl

@@ -703,7 +703,7 @@ atom(Bin) when is_binary(Bin), size(Bin) > 255 ->
     erlang:throw(
         iolist_to_binary(
             io_lib:format(
-                "Name is is too long."
+                "Name is too long."
                 " Please provide a shorter name (<= 255 bytes)."
                 " The name that is too long: \"~s\"",
                 [Bin]

+ 2 - 4
apps/emqx_bridge/src/emqx_bridge_v2.erl

@@ -1092,15 +1092,13 @@ split_bridge_v1_config_and_create_helper(BridgeV1Type, BridgeName, RawConf) ->
         bridge_v2_conf := NewBridgeV2RawConf
     } =
         split_and_validate_bridge_v1_config(BridgeV1Type, BridgeName, RawConf),
-    %% TODO should we really create an atom here?
-    ConnectorNameAtom = binary_to_atom(NewConnectorName),
-    case emqx_connector:create(ConnectorType, ConnectorNameAtom, NewConnectorRawConf) of
+    case emqx_connector:create(ConnectorType, NewConnectorName, NewConnectorRawConf) of
         {ok, _} ->
             case create(BridgeType, BridgeName, NewBridgeV2RawConf) of
                 {ok, _} = Result ->
                     Result;
                 {error, Reason1} ->
-                    case emqx_connector:remove(ConnectorType, ConnectorNameAtom) of
+                    case emqx_connector:remove(ConnectorType, NewConnectorName) of
                         ok ->
                             {error, Reason1};
                         {error, Reason2} ->

+ 9 - 0
apps/emqx_bridge/test/emqx_bridge_v1_compatibility_layer_SUITE.erl

@@ -444,6 +444,15 @@ bridge_node_operation_http_api_v2(Name, Node0, Op0) ->
 %% Test cases
 %%------------------------------------------------------------------------------
 
+t_name_too_long(_Config) ->
+    LongName = list_to_binary(lists:duplicate(256, $a)),
+    ?assertMatch(
+        {error,
+            {{_, 400, _}, _, #{<<"message">> := #{<<"reason">> := <<"Name is too long", _/binary>>}}}},
+        create_bridge_http_api_v1(#{name => LongName})
+    ),
+    ok.
+
 t_scenario_1(_Config) ->
     %% ===================================================================================
     %% Pre-conditions