From eb946eb80c170c4eab3b076ae27f76bc8afc9a52 Mon Sep 17 00:00:00 2001 From: Zaiming Shi Date: Mon, 26 Apr 2021 12:02:49 +0200 Subject: [PATCH] fix(emqx_trie): do not try to match wildcard topics --- src/emqx_trie.erl | 15 +++++++++++++-- test/emqx_trie_SUITE.erl | 10 ++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/emqx_trie.erl b/src/emqx_trie.erl index 30e5bd7b2..1c6e3be58 100644 --- a/src/emqx_trie.erl +++ b/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()). diff --git a/test/emqx_trie_SUITE.erl b/test/emqx_trie_SUITE.erl index e74b7352e..7516b58a0 100644 --- a/test/emqx_trie_SUITE.erl +++ b/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() ->