Merge pull request #6936 from terry-xiaoyu/empty_rule_id

fix(rule): deny POST empty rule ids
This commit is contained in:
Shawn 2022-02-09 10:16:27 +08:00 committed by GitHub
commit ce05c14ef2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 15 deletions

View File

@ -199,21 +199,25 @@ param_path_id() ->
{200, format_rule_resp(Records)}; {200, format_rule_resp(Records)};
'/rules'(post, #{body := Params0}) -> '/rules'(post, #{body := Params0}) ->
Id = maps:get(<<"id">>, Params0, list_to_binary(emqx_misc:gen_id(8))), case maps:get(<<"id">>, Params0, list_to_binary(emqx_misc:gen_id(8))) of
Params = filter_out_request_body(Params0), <<>> ->
ConfPath = emqx_rule_engine:config_key_path() ++ [Id], {400, #{code => 'BAD_ARGS', message => <<"empty rule id is not allowed">>}};
case emqx_rule_engine:get_rule(Id) of Id ->
{ok, _Rule} -> Params = filter_out_request_body(Params0),
{400, #{code => 'BAD_ARGS', message => <<"rule id already exists">>}}; ConfPath = emqx_rule_engine:config_key_path() ++ [Id],
not_found -> case emqx_rule_engine:get_rule(Id) of
case emqx_conf:update(ConfPath, Params, #{}) of {ok, _Rule} ->
{ok, #{post_config_update := #{emqx_rule_engine := AllRules}}} -> {400, #{code => 'BAD_ARGS', message => <<"rule id already exists">>}};
[Rule] = get_one_rule(AllRules, Id), not_found ->
{201, format_rule_resp(Rule)}; case emqx_conf:update(ConfPath, Params, #{}) of
{error, Reason} -> {ok, #{post_config_update := #{emqx_rule_engine := AllRules}}} ->
?SLOG(error, #{msg => "create_rule_failed", [Rule] = get_one_rule(AllRules, Id),
id => Id, reason => Reason}), {201, format_rule_resp(Rule)};
{400, #{code => 'BAD_ARGS', message => ?ERR_BADARGS(Reason)}} {error, Reason} ->
?SLOG(error, #{msg => "create_rule_failed",
id => Id, reason => Reason}),
{400, #{code => 'BAD_ARGS', message => ?ERR_BADARGS(Reason)}}
end
end end
end. end.