소스 검색

Merge pull request #11997 from zmstone/1121-do-not-allow-special-char-as-prefix-for-resource-name

1121 do not allow special char as prefix for resource name
Zaiming (Stone) Shi 2 년 전
부모
커밋
e065217e9a

+ 1 - 1
apps/emqx_bridge/test/emqx_bridge_SUITE.erl

@@ -199,7 +199,7 @@ t_create_with_bad_name(_Config) ->
     ?assertMatch(
         {error,
             {pre_config_update, emqx_bridge_app, #{
-                reason := <<"only 0-9a-zA-Z_- is allowed in resource name", _/binary>>,
+                reason := <<"Invalid name format.", _/binary>>,
                 kind := validation_error
             }}},
         emqx:update_config(Path, Conf)

+ 1 - 1
apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl

@@ -1365,7 +1365,7 @@ t_create_with_bad_name(Config) ->
     ?assertMatch(
         #{
             <<"kind">> := <<"validation_error">>,
-            <<"reason">> := <<"only 0-9a-zA-Z_- is allowed in resource name", _/binary>>
+            <<"reason">> := <<"Invalid name format.", _/binary>>
         },
         Msg
     ),

+ 1 - 1
apps/emqx_bridge/test/emqx_bridge_v1_compatibility_layer_SUITE.erl

@@ -829,7 +829,7 @@ t_create_with_bad_name(_Config) ->
             <<"code">> := <<"BAD_REQUEST">>,
             <<"message">> := #{
                 <<"kind">> := <<"validation_error">>,
-                <<"reason">> := <<"only 0-9a-zA-Z_- is allowed in resource name", _/binary>>
+                <<"reason">> := <<"Invalid name format.", _/binary>>
             }
         }}} = create_bridge_http_api_v1(Opts),
     ok.

+ 1 - 3
apps/emqx_bridge/test/emqx_bridge_v2_api_SUITE.erl

@@ -1034,10 +1034,8 @@ t_bad_name(Config) ->
     Msg = emqx_utils_json:decode(Msg0, [return_maps]),
     ?assertMatch(
         #{
-            <<"got">> := [<<"_bad_name">>],
             <<"kind">> := <<"validation_error">>,
-            <<"path">> := <<"actions.kafka_producer">>,
-            <<"reason">> := <<"invalid_map_key">>
+            <<"reason">> := <<"Invalid name format.", _/binary>>
         },
         Msg
     ),

+ 1 - 1
apps/emqx_connector/test/emqx_connector_SUITE.erl

@@ -229,7 +229,7 @@ t_create_with_bad_name_direct_path(_Config) ->
         {error,
             {pre_config_update, _ConfigHandlerMod, #{
                 kind := validation_error,
-                reason := <<"only 0-9a-zA-Z_- is allowed in resource name", _/binary>>
+                reason := <<"Invalid name format.", _/binary>>
             }}},
         emqx:update_config(Path, ConnConfig)
     ),

+ 9 - 4
apps/emqx_resource/src/emqx_resource.erl

@@ -812,11 +812,11 @@ validate_name(Name) ->
     ok.
 
 validate_name(<<>>, _Opts) ->
-    invalid_data("name cannot be empty string");
+    invalid_data("Name cannot be empty string");
 validate_name(Name, _Opts) when size(Name) >= 255 ->
-    invalid_data("name length must be less than 255");
+    invalid_data("Name length must be less than 255");
 validate_name(Name, Opts) ->
-    case re:run(Name, <<"^[-0-9a-zA-Z_]+$">>, [{capture, none}]) of
+    case re:run(Name, <<"^[0-9a-zA-Z][-0-9a-zA-Z_]*$">>, [{capture, none}]) of
         match ->
             case maps:get(atom_name, Opts, true) of
                 %% NOTE
@@ -827,7 +827,12 @@ validate_name(Name, Opts) ->
             end;
         nomatch ->
             invalid_data(
-                <<"only 0-9a-zA-Z_- is allowed in resource name, got: ", Name/binary>>
+                <<
+                    "Invalid name format. The name must begin with a letter or number "
+                    "(0-9, a-z, A-Z) and can only include underscores and hyphens as "
+                    "non-initial characters. Got: ",
+                    Name/binary
+                >>
             )
     end.