Skip to content

Commit

Permalink
Add validate for topic name
Browse files Browse the repository at this point in the history
  • Loading branch information
tigercl committed Apr 28, 2020
1 parent a8b4940 commit 84e5a52
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/emqx_mgmt_api_topic_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@
list(#{topic := Topic0}, _Params) ->
execute_when_enabled(fun() ->
Topic = http_uri:decode(Topic0),
case emqx_mgmt:get_topic_metrics(Topic) of
{error, Reason} -> return({error, Reason});
Metrics -> return({ok, maps:from_list(Metrics)})
case safe_validate(Topic) of
true ->
case emqx_mgmt:get_topic_metrics(Topic) of
{error, Reason} -> return({error, Reason});
Metrics -> return({ok, maps:from_list(Metrics)})
end;
false ->
return({error, invalid_topic_name})
end
end);

Expand All @@ -76,8 +81,13 @@ register(_Bindings, Params) ->
undefined ->
return({error, missing_required_params});
Topic ->
emqx_mgmt:register_topic_metrics(Topic),
return(ok)
case safe_validate(Topic) of
true ->
emqx_mgmt:register_topic_metrics(Topic),
return(ok);
false ->
return({error, invalid_topic_name})
end
end
end).

Expand All @@ -90,8 +100,13 @@ unregister(Bindings, _Params) when map_size(Bindings) =:= 0 ->
unregister(#{topic := Topic0}, _Params) ->
execute_when_enabled(fun() ->
Topic = http_uri:decode(Topic0),
emqx_mgmt:unregister_topic_metrics(Topic),
return(ok)
case safe_validate(Topic) of
true ->
emqx_mgmt:unregister_topic_metrics(Topic),
return(ok);
false ->
return({error, invalid_topic_name})
end
end).

execute_when_enabled(Fun) ->
Expand All @@ -106,4 +121,10 @@ execute_when_enabled(Fun) ->
return({error, module_not_loaded})
end.


safe_validate(Topic) ->
try emqx_topic:validate(name, Topic) of
true -> true
catch
error:_Error ->
false
end.

0 comments on commit 84e5a52

Please sign in to comment.