fix(message validation): limit the length of validation name

Fixes https://emqx.atlassian.net/browse/EMQX-12299
This commit is contained in:
Thales Macedo Garitezi 2024-05-07 10:04:56 -03:00
parent 93e31d088a
commit 96bb6e7d88
2 changed files with 4 additions and 15 deletions

View File

@ -18,8 +18,6 @@
api_schema/1 api_schema/1
]). ]).
-export([validate_name/1]).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Type declarations %% Type declarations
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
@ -55,7 +53,7 @@ fields(validation) ->
binary(), binary(),
#{ #{
required => true, required => true,
validator => fun validate_name/1, validator => fun emqx_resource:validate_name/1,
desc => ?DESC("name") desc => ?DESC("name")
} }
)}, )},
@ -200,16 +198,6 @@ ensure_array(undefined, _) -> undefined;
ensure_array(L, _) when is_list(L) -> L; ensure_array(L, _) when is_list(L) -> L;
ensure_array(B, _) -> [B]. ensure_array(B, _) -> [B].
validate_name(Name) ->
%% see `MAP_KEY_RE' in hocon_tconf
RE = <<"^[A-Za-z0-9]+[A-Za-z0-9-_]*$">>,
case re:run(Name, RE, [{capture, none}]) of
match ->
ok;
nomatch ->
{error, <<"must conform to regex: ", RE/binary>>}
end.
validate_sql(SQL) -> validate_sql(SQL) ->
case emqx_message_validation:parse_sql_check(SQL) of case emqx_message_validation:parse_sql_check(SQL) of
{ok, _Parsed} -> {ok, _Parsed} ->

View File

@ -195,7 +195,6 @@ invalid_names_test_() ->
?_assertThrow( ?_assertThrow(
{_Schema, [ {_Schema, [
#{ #{
reason := <<"must conform to regex:", _/binary>>,
kind := validation_error, kind := validation_error,
path := "message_validation.validations.1.name" path := "message_validation.validations.1.name"
} }
@ -209,7 +208,9 @@ invalid_names_test_() ->
<<"name!">>, <<"name!">>,
<<"some name">>, <<"some name">>,
<<"nãme"/utf8>>, <<"nãme"/utf8>>,
<<"test_哈哈"/utf8>> <<"test_哈哈"/utf8>>,
%% long name
binary:copy(<<"a">>, 256)
] ]
]. ].