Validate feed keys in MQTT client to prevent malformed topic subscriptions#170
Validate feed keys in MQTT client to prevent malformed topic subscriptions#170
Conversation
Co-authored-by: tyeth <6692083+tyeth@users.noreply.github.com>
Co-authored-by: tyeth <6692083+tyeth@users.noreply.github.com>
Co-authored-by: tyeth <6692083+tyeth@users.noreply.github.com>
Co-authored-by: tyeth <6692083+tyeth@users.noreply.github.com>
2c5b07d to
ee4ea8f
Compare
tyeth
left a comment
There was a problem hiding this comment.
LGTM, fixed temp_humidity example where humidity was using incorrect argument number in format string (ruff removed argument instead of fixing arg number - fixed)
There was a problem hiding this comment.
This is because some function arguments have been renamed for clarity, technically a breaking change.
Adafruit_IO/client.py
Outdated
| import platform | ||
| import pkg_resources | ||
| try: | ||
| from importlib.metadata import version as package_version |
There was a problem hiding this comment.
Should probably add the <3.8 fallback, not sure our min version policy, but we say 3.6+ for now, so update like in #162 or retest with 3.6 + 3.7 then maybe safe to not need it.
try:
from importlib.metadata import version as pkg_version # Python 3.8+
except ImportError:
try:
from importlib_metadata import version as pkg_version # Backport for <3.8
except ImportError:
pkg_version = None
import pkg_blah
|
It might make sense to prepopulate the feed key to the subscribe mqtt function, to be "#" and update the regex, as technically wildcards can be supported (and possible expected default behaviour without supplying a feed key). -EDIT- The docstrings suggest only a single feed should be monitored with the subscribe method, so no action needed. |
MQTT client methods accepted empty or invalid feed keys, resulting in malformed topic subscriptions like
tyeth/feeds/(trailing slash). This affected 9 production users.Changes
Added
validate_feed_key()function enforcing Adafruit IO rules:^[a-zA-Z0-9-]+((\/|\.)[a-zA-Z0-9-]+)?$TypeErrorfor invalid patterns,ValueErrorfor length violationsValidation applied to:
subscribe(),publish(),unsubscribe(),receive()Renamed parameter:
feed_id→feed_key(clarifies string key vs numeric ID)Added tests covering valid keys, empty strings, invalid characters, and invalid patterns
Example
Reference: https://learn.adafruit.com/naming-things-in-adafruit-io/the-two-feed-identifiers
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
io.adafruit.com/usr/bin/python python -m unittest test_mqtt_client.TestFeedKeyValidation test_mqtt_client.TestMQTTClientFeedKeyValidation test_model test_errors -v(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
/<user>/feeds/, but technically shouldn't be... #169💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.