eclipse / paho.mqtt.java

Eclipse Paho Java MQTT client library. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
2.14k stars 887 forks source link

Mqtt topic filtering does not conform to spec: filter foo/+/bar/# won't catch messages published to foo/something/bar #918

Open marco-kaeferbeck opened 2 years ago

marco-kaeferbeck commented 2 years ago

In the case that there is a single level wildcard somewhere in the topic the fixes provided by https://github.com/eclipse/paho.mqtt.java/issues/418 won't work

For example when i subscribe to foo/+/bar/# and publish on foo/something/bar

-> if (topicName.charAt(topicPos - 1) == '/' && topicFilter.charAt(filterPos) == '#') this returns false, because the topicFilter at filterPos is / and not #

-> if (topicFilter.length() - filterPos > 1 && topicFilter.substring(filterPos, filterPos + 2).equals("/#")) returns false, because topicFilter length - filterPos is exactly 1

-> if ((topicFilter.length() - topicName.length()) == 2 && topicFilter.substring(topicFilter.length() - 2, topicFilter.length()).equals("/#")) returns false, because topicFilter length - topicName length is negative the length of the word that replaces the single level wildcard plus 3 ( 1 for the single level wildcard and 2 for the /#)

czulehner commented 2 years ago

Added another test at org.eclipse.paho.mqttv5.common.utils.MqttTopicTest.testMatchedTopicFilterWildcards()

{ "sport/tennis/+/ranking/#", "sport/tennis/player1/ranking"} fails.