소스 검색

fix(emqx_trie): do not try to match wildcard topics

Zaiming Shi 4 년 전
부모
커밋
eb946eb80c
2개의 변경된 파일23개의 추가작업 그리고 2개의 파일을 삭제
  1. 13 2
      src/emqx_trie.erl
  2. 10 0
      test/emqx_trie_SUITE.erl

+ 13 - 2
src/emqx_trie.erl

@@ -105,8 +105,19 @@ delete(Topic) when is_binary(Topic) ->
 -spec(match(emqx_topic:topic()) -> list(emqx_topic:topic())).
 match(Topic) when is_binary(Topic) ->
     Words = emqx_topic:words(Topic),
-    false = emqx_topic:wildcard(Words), % assert
-    do_match(Words).
+    case emqx_topic:wildcard(Words) of
+        true ->
+            %% In MQTT spec, clients are not allowed to
+            %% publish messages to a wildcard topic.
+            %% Here we refuse to match wildcard topic.
+            %%
+            %% NOTE: this does not imply emqx allows clients
+            %% publishing to wildcard topics.
+            %% Such clients will get disconnected.
+            [];
+        false ->
+            do_match(Words)
+    end.
 
 %% @doc Is the trie empty?
 -spec(empty() -> boolean()).

+ 10 - 0
test/emqx_trie_SUITE.erl

@@ -81,6 +81,16 @@ t_match(_) ->
             end),
     ?assertEqual(Machted, lists:sort(?TRIE:match(<<"sensor/1">>))).
 
+t_match_invalid(_) ->
+    trans(fun() ->
+              ?TRIE:insert(<<"sensor/1/metric/2">>),
+              ?TRIE:insert(<<"sensor/+/#">>),
+              ?TRIE:insert(<<"sensor/#">>)
+            end),
+    ?assertEqual([], lists:sort(?TRIE:match(<<"sensor/+">>))),
+    ?assertEqual([], lists:sort(?TRIE:match(<<"#">>))).
+
+
 t_match2(_) ->
     Matched = [<<"#">>, <<"+/#">>, <<"+/+/#">>],
     trans(fun() ->