-
Notifications
You must be signed in to change notification settings - Fork 137
Add guard against long topics in (un)subscribe #164
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
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. |
I have pushed a branch Changes in V0.8.3Scope
Files changedmqtt_v5_properties.pyNo code changes.
|
@bobveringa Have you had a chance to look at this? |
@peterhinch I have not yet had a chance to look at this yet. I've added an item to look at this next week. |
It, took me a bit more time to get this set up than desired. Partially because If possible, I'd like to keep Other than that, I seem to have no issues running the new version and performance is either better, or not noticeably different. |
Thanks for testing. Re I'd be interested to hear your views on testing. The kind of scenario where these tests would be run is where a bug affecting the protocol was identified. With suitable tests I could fix the bug and run the tests. My thought is that beyond notifying you, there would be no need for you to be involved. Examples of protocol bugs are the one in this thread's OP, and the fact that unsubscribe didn't actually work. The scope of testing could be enhanced to test all possible properties (using Pickle in place of JSON), but I'm not sure this is necessary. Again your thoughts would be welcome. |
The use of I've been giving testing some thought as part of our own testing strategy. Ideally, there is some form of automated unit testing/integration testing for these protocols (maybe through GitHub actions). This is more difficult than for most other projects as mqtt_as uses both a lot of MicroPython modules and has some hardware-specific behavior (but that is mostly for Wi-Fi). Once I have time again (currently in the process of moving, so I have even less time than normal). I would like to take a look at introducing this kind of testing with GitHub actions. Once some initial tests are in place, it will be easier to expand with more and more complicated tests. |
That sounds good, but GitHub actions are outside of my experience. I'll revert the Good luck with the move! |
I have now pushed an update. Closing this as the issue in the OP is now resolved (by correcting the code rather than by guarding). |
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: