feat(message_validation): impose restrictions on validation name
This commit is contained in:
parent
74c03377f2
commit
e8a8416e44
|
@ -18,6 +18,8 @@
|
||||||
api_schema/1
|
api_schema/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
-export([validate_name/1]).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Type declarations
|
%% Type declarations
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
@ -48,7 +50,15 @@ fields(validation) ->
|
||||||
{tags, emqx_schema:tags_schema()},
|
{tags, emqx_schema:tags_schema()},
|
||||||
{description, emqx_schema:description_schema()},
|
{description, emqx_schema:description_schema()},
|
||||||
{enable, mk(boolean(), #{desc => ?DESC("config_enable"), default => true})},
|
{enable, mk(boolean(), #{desc => ?DESC("config_enable"), default => true})},
|
||||||
{name, mk(binary(), #{required => true, desc => ?DESC("name")})},
|
{name,
|
||||||
|
mk(
|
||||||
|
binary(),
|
||||||
|
#{
|
||||||
|
required => true,
|
||||||
|
validator => fun validate_name/1,
|
||||||
|
desc => ?DESC("name")
|
||||||
|
}
|
||||||
|
)},
|
||||||
{topics,
|
{topics,
|
||||||
mk(
|
mk(
|
||||||
hoconsc:union([binary(), hoconsc:array(binary())]),
|
hoconsc:union([binary(), hoconsc:array(binary())]),
|
||||||
|
@ -186,6 +196,16 @@ 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} ->
|
||||||
|
|
|
@ -177,6 +177,30 @@ schema_test_() ->
|
||||||
)}
|
)}
|
||||||
].
|
].
|
||||||
|
|
||||||
|
invalid_names_test_() ->
|
||||||
|
[
|
||||||
|
{InvalidName,
|
||||||
|
?_assertThrow(
|
||||||
|
{_Schema, [
|
||||||
|
#{
|
||||||
|
reason := <<"must conform to regex:", _/binary>>,
|
||||||
|
kind := validation_error,
|
||||||
|
path := "message_validation.validations.1.name"
|
||||||
|
}
|
||||||
|
]},
|
||||||
|
parse_and_check([validation(InvalidName, [sql_check()])])
|
||||||
|
)}
|
||||||
|
|| InvalidName <- [
|
||||||
|
<<"">>,
|
||||||
|
<<"_name">>,
|
||||||
|
<<"name$">>,
|
||||||
|
<<"name!">>,
|
||||||
|
<<"some name">>,
|
||||||
|
<<"nãme"/utf8>>,
|
||||||
|
<<"test_哈哈"/utf8>>
|
||||||
|
]
|
||||||
|
].
|
||||||
|
|
||||||
check_test_() ->
|
check_test_() ->
|
||||||
[
|
[
|
||||||
{"denied by payload 1",
|
{"denied by payload 1",
|
||||||
|
|
|
@ -129,7 +129,7 @@
|
||||||
emqx_exproto_pb
|
emqx_exproto_pb
|
||||||
]}.
|
]}.
|
||||||
|
|
||||||
{eunit_opts, [verbose]}.
|
{eunit_opts, [verbose, {print_depth, 100}]}.
|
||||||
|
|
||||||
{project_plugins, [
|
{project_plugins, [
|
||||||
{erlfmt, "1.3.0"},
|
{erlfmt, "1.3.0"},
|
||||||
|
|
|
@ -55,7 +55,7 @@ emqx_message_validation_schema {
|
||||||
"""Topic filter(s)"""
|
"""Topic filter(s)"""
|
||||||
|
|
||||||
name.desc:
|
name.desc:
|
||||||
"""The name for this validation. Must be unique among all validations."""
|
"""The name for this validation. Must be unique among all validations. It must be a combination of alphanumeric characters and underscores, and cannot start with neither number nor an underscore."""
|
||||||
name.desc:
|
name.desc:
|
||||||
"""Name"""
|
"""Name"""
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue