Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import socket
import time
import ssl
import re
import requests.certs

import voluptuous as vol
Expand Down Expand Up @@ -639,12 +640,20 @@ def _raise_on_error(result):

def _match_topic(subscription, topic):
"""Test if topic matches subscription."""
reg_ex_parts = []
suffix = ""
if subscription.endswith('#'):
return (subscription[:-2] == topic or
topic.startswith(subscription[:-1]))

subscription = subscription[:-2]
suffix = "(.*)"
sub_parts = subscription.split('/')
topic_parts = topic.split('/')
for sub_part in sub_parts:
if sub_part == "+":
reg_ex_parts.append(r"([^\/]+)")
else:
reg_ex_parts.append(sub_part)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not escaping the characters that do have special meaning in regular expressions, and in fact causes issue #7478. Using regular expressions in case where both subscription and topic are variables is only asking for troubles and this fix should be reverted IMO.


reg_ex = "^" + (r'\/'.join(reg_ex_parts)) + suffix + "$"

reg = re.compile(reg_ex)

return (len(sub_parts) == len(topic_parts) and
all(a == b for a, b in zip(sub_parts, topic_parts) if a != '+'))
return reg.match(topic) is not None
40 changes: 40 additions & 0 deletions tests/components/mqtt/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,46 @@ def test_subscribe_topic_subtree_wildcard_no_match(self):
self.hass.block_till_done()
self.assertEqual(0, len(self.calls))

def test_subscribe_topic_level_wildcard_and_wildcard_root_topic(self):
"""Test the subscription of wildcard topics."""
mqtt.subscribe(self.hass, '+/test-topic/#', self.record_calls)

fire_mqtt_message(self.hass, 'hi/test-topic', 'test-payload')

self.hass.block_till_done()
self.assertEqual(1, len(self.calls))
self.assertEqual('hi/test-topic', self.calls[0][0])
self.assertEqual('test-payload', self.calls[0][1])

def test_subscribe_topic_level_wildcard_and_wildcard_subtree_topic(self):
"""Test the subscription of wildcard topics."""
mqtt.subscribe(self.hass, '+/test-topic/#', self.record_calls)

fire_mqtt_message(self.hass, 'hi/test-topic/here-iam', 'test-payload')

self.hass.block_till_done()
self.assertEqual(1, len(self.calls))
self.assertEqual('hi/test-topic/here-iam', self.calls[0][0])
self.assertEqual('test-payload', self.calls[0][1])

def test_subscribe_topic_level_wildcard_and_wildcard_level_no_match(self):
"""Test the subscription of wildcard topics."""
mqtt.subscribe(self.hass, '+/test-topic/#', self.record_calls)

fire_mqtt_message(self.hass, 'hi/here-iam/test-topic', 'test-payload')

self.hass.block_till_done()
self.assertEqual(0, len(self.calls))

def test_subscribe_topic_level_wildcard_and_wildcard_no_match(self):
"""Test the subscription of wildcard topics."""
mqtt.subscribe(self.hass, '+/test-topic/#', self.record_calls)

fire_mqtt_message(self.hass, 'hi/another-test-topic', 'test-payload')

self.hass.block_till_done()
self.assertEqual(0, len(self.calls))

def test_subscribe_binary_topic(self):
"""Test the subscription to a binary topic."""
mqtt.subscribe(self.hass, 'test-topic', self.record_calls,
Expand Down