eclipse-paho / eclipse-paho/paho.mqtt.java
MqttTopic.validate is not invoked on subscribe and is invoked on unsubscribe
- Dominant language
- Java
- Stars
- 2.3k
- Forks
- 919
- PR merge metrics
- No merged PRs in 30d
Description
This issue involves a bug and an enhancement request. If I should create two separate issues, please let me know.
The bug is that MqttTopic.validate is invoked in MqttAsyncClient.subscribe only when logging at FINE level:
```
// Only Generate Log string if we are logging at FINE level
if (log.isLoggable(Logger.FINE)) {
StringBuffer subs = new StringBuffer();
for (int i = 0; i < topicFilters.length; i++) {
if (i > 0) {
subs.append(", ");
}
subs.append("topic=").append(topicFilters[i]).append(" qos=").append(qos[i]);
// Check if the topic filter is valid before subscribing
MqttTopic.validate(topicFilters[i], true/* allow wildcards */);
}
// @TRACE 106=Subscribe topicFilter={0} userContext={1} callback={2}
log.fine(CLASS_NAME, methodName, "106", new Object[] { subs.toString(), userContext, callback });
}
```
Whereas MqttAsyncClient.unsubscribe always validates the topic filter, regardless of log level:
```
// Only Generate Log string if we are logging at FINE level
if (log.isLoggable(Logger.FINE)) {
String subs = "";
for (int i = 0; i < topicFilters.length; i++) {
if (i > 0) {
subs += ", ";
}
subs += topicFilters[i];
}
// @TRACE 107=Unsubscribe topic={0} userContext={1} callback={2}
log.fine(CLASS_NAME, methodName, "107", new Object[] { subs, userContext, callback });
}
for (int i = 0; i < topicFilters.length; i++) {
// Check if the topic filter is valid before unsubscribing
// Although we already checked when subscribing, but invalid
// topic filter is meanless for unsubscribing, just prohibit it
// to reduce unnecessary control packet send to broker.
MqttTopic.validate(topicFilters[i], true/* allow wildcards */);
}
```
Therefore, you could subscribe to an "invalid" topic, but you cannot unsubscribe from that same "invalid" topic:
```
java.lang.IllegalArgumentException: Invalid usage of multi-level wildcard in topic string: a/b+/c
at org.eclipse.paho.client.mqttv3.MqttTopic.validate(MqttTopic.java:191)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.unsubscribe(MqttAsyncClient.java:1218)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.unsubscribe(MqttAsyncClient.java:1173)
```
Now, depending on how the bug above is fixed, the enhancement request is to have some way to disable topic validation. There are brokers that support non-standard MQTT subscription abilities for enhanced topic matching (i.e. suffix matching within a level), in addition to the standard ones described in the MQTT specification. As is, the Paho Java Client library does not allow these topics to be used, at least just on the unsubscribe for now until the bug described above is fixed.
- [X] Bug exists Release Version 1.2.0 ( Master Branch)
- [X] Bug exists in MQTTv3 Client on Snapshot Version 1.2.1-SNAPSHOT (Develop Branch)
- [X] Bug exists in MQTTv5 Client on Snapshot Version 1.2.1-SNAPSHOT (Develop Branch)
Contributor guide
Assessment
This issue has not been assessed yet.