eclipse / paho.mqtt.java

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

java.lang.IllegalArgumentException: Invalid UTF-8 char: any more "forgiveness" possible? #996

Open chk2902 opened 1 year ago

chk2902 commented 1 year ago

Please fill out the form below before submitting, thank you!

STACK_TRACE=java.lang.IllegalArgumentException: Invalid UTF-8 char: [0]
    at org.eclipse.paho.client.mqttv3.internal.wire.MqttWireMessage.validateUTF8String(MqttWireMessage.java:407)
    at org.eclipse.paho.client.mqttv3.internal.wire.MqttWireMessage.decodeUTF8(MqttWireMessage.java:365)
    at org.eclipse.paho.client.mqttv3.internal.wire.MqttPublish.<init>(MqttPublish.java:64)
    at org.eclipse.paho.client.mqttv3.internal.wire.MqttWireMessage.createWireMessage(MqttWireMessage.java:207)
    at org.eclipse.paho.client.mqttv3.internal.wire.MqttWireMessage.createWireMessage(MqttWireMessage.java:182)
    at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:119)
    at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:137)
    at java.lang.Thread.run(Thread.java:1012)

I use this PAHO client very happily, but now a customer got the message above. This might be a valid cause for an exception, but I don't have a change to "ignore" this crash in my Android app, so the user needs to re-install the app to get rid of the MQTT publishes he configured in my app as they will lead to the crash immediately after startup.

Is there a way to make the client code more "relaxed" about other clients publishing bad message texts to the MQTT server (I have not yet found where the code "0" is, but I guess it's a null terminator at the end of the string, which is included in the text - I don't know much about the inner format of MQTT messages and whether that's likely, but my C++ knowledge with 0-terminated strings forces that guess ;)).

A more tolerant client could help, or a way to "catch" such an event and allow the app to continue without crashing in a thread I did not create. It's OK to close the comm, of course.

I have read

The character data in a UTF-8 encoded string MUST be well-formed UTF-8 as defined by the Unicode specification [Unicode] and restated in RFC 3629 [RFC3629]. In particular this data MUST NOT include encodings of code points between U+D800 and U+DFFF. If a Server or Client receives a Control Packet containing ill-formed UTF-8 it MUST close the Network Connection [MQTT-1.5.3-1].

so maybe the way to go is to include an option to be able to ignore the exception and become informed by some clientState event?

IanBoden commented 1 year ago

Just to be clear what exactly is the scenario here?

Are you saying you have an app that keeps trying to publish an invalid message and the customer must reinstall your app to clear out the queued message? Where do the "other clients" come into it?

ye-landu commented 6 months ago

Hello, I also encountered this problem

In the MQTTv3 client, in line 64 of MqttPublish.java.The message producer pushes the message to an illegal topic, causing the consumer client to reconnect. This process is continuous, and it is difficult to analyze which client the error message comes from. I am serious about fixing this vulnerability. It can serve as a means of attack to prevent your consumers from receiving normal message data.

SpikeFJ commented 3 months ago

I also encountered this issue.

An MQTT publisher published an illegal topic, which caused the subscriber's thread responsible for reading data to crash. java Exception in thread "MQTT Rec: test" java.lang.IllegalArgumentException: Invalid UTF-8 char: [0000] at org.eclipse.paho.mqttv5.common.packet.MqttDataTypes.validateUTF8String(MqttDataTypes.java:199) at org.eclipse.paho.mqttv5.common.packet.MqttDataTypes.decodeUTF8(MqttDataTypes.java:157) at org.eclipse.paho.mqttv5.common.packet.MqttPublish.<init>(MqttPublish.java:103) at org.eclipse.paho.mqttv5.common.packet.MqttWireMessage.createWireMessage(MqttWireMessage.java:229) at org.eclipse.paho.mqttv5.common.packet.MqttWireMessage.createWireMessage(MqttWireMessage.java:198) at org.eclipse.paho.mqttv5.client.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:131) at org.eclipse.paho.mqttv5.client.internal.CommsReceiver.run(CommsReceiver.java:139) at java.lang.Thread.run(Thread.java:748) This doesn't make sense。