-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add guard against long topics in (un)subscribe #164
Comments
The But looking at the spec, and other elements of the code, I would argue for just supporting any length for (un)subscribe. The logic is simple, and (un)subscribes happen rarely enough that it shouldn't really impact performance. The downside is that it cannot be optimized away with MicroPython optimization levels. In my opinion, this small tradeoff is worth it for being able to use any length topic. |
As a side note: if methods premsg = bytearray(b"\x10\0\0\0\0\0")
msg = bytearray(b"\x04MQTT\x00\0\0\0") into premsg = bytearray(b"\x10\0\0\0\0")
msg = bytearray(b"\0\x04MQTT\x00\0\0\0") (which requires an update of all indices in byte-array i = 1
while sz > 0x7F:
premsg[i] = (sz & 0x7F) | 0x80
sz >>= 7
i += 1
premsg[i] = sz
await self._as_write(premsg, i + 2) in which the last line looks like a coding error, but is not with the current definitions of the aforementioned byte-arrays. |
Seems like you have a good handle on the situation. I wish I could give more input at this time, but I am leaving for a holiday soon and do not have time at the moment. I can investigate further (or implement the feature) in about 2 weeks. |
[EDIT] The Remaining Length byte
|
The specification I've used is taken from https://mqtt.org/mqtt-specification/. I read in the specification of v3.1.1 that field |
The V5 spec 3.8.1 is specific:
However I've not spotted anything to that effect in the V3.1.1. It would simplify the code if both versions use VBI and remove the need for a guard. Please can you point me at a specific statement to that effect in the V3.1.1 spec? |
I only have access to my phone at the moment (and MQTT docs are not ideal to read on a phone) But I think section 2.2.3 in the v3.1.1 spec is clear on the matter.
I think that it is fair to assume this applies to all packets. The only reason to doubt this is that a lot of packets say EDIT: I just looked at the source code of paho.mqtt (python) and it seems like they use a variable length header for (un)subscribe on both v5 and v3.1.1. |
Using paho mqtt client, version 2.1.0, selecting MQTT version 3.1.1, a subscription to a topic with a name of 131 (ASCII) characters was performed. The subscription was successful. Using tcpdump and wireshark the network packets are captured and analyzed. Wireshark showed a |
I now have a build which supports long topics, both for publication and subscription. This works under V3.1.1 and V5. A historical note.
|
I have raised this issue against the official library. |
In file
mqtt_as/__init__.py
, in both methodssubscribe
andunsubscribe
, the fieldremaining length
is encoded with a single octet. Lines 536 and 560 both read:The (implicit) assumption is that the payload, a topic in both cases, is short enough to have the message fit in at most 127 octets. A check to see if this assumption is correct, might be needed.
Personally, I would prefer a check like:
Once the application using this module is tested and it is known that topics in use are not too long, the assert-statements can be effectively removed by increasing the optimization level of microPython, thus decreasing the size of the byte-code.
The text was updated successfully, but these errors were encountered: