Reject MQTT topics which include control- or non-characters (#71263)

This commit is contained in:
Erik Montnemery 2022-05-03 21:19:43 +02:00 committed by GitHub
parent 3717ec8811
commit e9abfad361
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 5 deletions

View file

@ -31,6 +31,15 @@ def valid_topic(value: Any) -> str:
) )
if "\0" in value: if "\0" in value:
raise vol.Invalid("MQTT topic name/filter must not contain null character.") raise vol.Invalid("MQTT topic name/filter must not contain null character.")
if any(char <= "\u001F" for char in value):
raise vol.Invalid("MQTT topic name/filter must not contain control characters.")
if any("\u007f" <= char <= "\u009F" for char in value):
raise vol.Invalid("MQTT topic name/filter must not contain control characters.")
if any("\ufdd0" <= char <= "\ufdef" for char in value):
raise vol.Invalid("MQTT topic name/filter must not contain non-characters.")
if any((ord(char) & 0xFFFF) in (0xFFFE, 0xFFFF) for char in value):
raise vol.Invalid("MQTT topic name/filter must not contain noncharacters.")
return value return value

View file

@ -522,11 +522,29 @@ def test_validate_topic():
# Topics "SHOULD NOT" include these special characters # Topics "SHOULD NOT" include these special characters
# (not MUST NOT, RFC2119). The receiver MAY close the connection. # (not MUST NOT, RFC2119). The receiver MAY close the connection.
mqtt.util.valid_topic("\u0001") # We enforce this because mosquitto does: https://github.com/eclipse/mosquitto/commit/94fdc9cb44c829ff79c74e1daa6f7d04283dfffd
mqtt.util.valid_topic("\u001F") with pytest.raises(vol.Invalid):
mqtt.util.valid_topic("\u009F") mqtt.util.valid_topic("\u0001")
mqtt.util.valid_topic("\u009F") with pytest.raises(vol.Invalid):
mqtt.util.valid_topic("\uffff") mqtt.util.valid_topic("\u001F")
with pytest.raises(vol.Invalid):
mqtt.util.valid_topic("\u007F")
with pytest.raises(vol.Invalid):
mqtt.util.valid_topic("\u009F")
with pytest.raises(vol.Invalid):
mqtt.util.valid_topic("\ufdd0")
with pytest.raises(vol.Invalid):
mqtt.util.valid_topic("\ufdef")
with pytest.raises(vol.Invalid):
mqtt.util.valid_topic("\ufffe")
with pytest.raises(vol.Invalid):
mqtt.util.valid_topic("\ufffe")
with pytest.raises(vol.Invalid):
mqtt.util.valid_topic("\uffff")
with pytest.raises(vol.Invalid):
mqtt.util.valid_topic("\U0001fffe")
with pytest.raises(vol.Invalid):
mqtt.util.valid_topic("\U0001ffff")
def test_validate_subscribe_topic(): def test_validate_subscribe_topic():