[MQTTnet, MQTTnet.Extensions.ManagedClient] Fixed bug that allowed invalid subscriptions (Thanks to @marcelwinh). (#1226)

This commit is contained in:
HansM
2021-09-05 11:42:42 +02:00
committed by GitHub
parent d1c883ebae
commit 3cdf6d1261
6 changed files with 81 additions and 2 deletions
@@ -215,6 +215,11 @@ namespace MQTTnet.Extensions.ManagedClient
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
foreach (var topicFilter in topicFilters)
{
MqttTopicValidator.ThrowIfInvalidSubscribe(topicFilter.Topic);
}
lock (_subscriptions)
{
foreach (var topicFilter in topicFilters)
+5
View File
@@ -194,6 +194,11 @@ namespace MQTTnet.Client
{
if (options == null) throw new ArgumentNullException(nameof(options));
foreach (var topicFilter in options.TopicFilters)
{
MqttTopicValidator.ThrowIfInvalidSubscribe(topicFilter.Topic);
}
ThrowIfDisposed();
ThrowIfNotConnected();
@@ -42,5 +42,15 @@ namespace MQTTnet.Protocol
}
}
}
public static void ThrowIfInvalidSubscribe(string topic)
{
if (string.IsNullOrEmpty(topic))
{
throw new MqttProtocolViolationException("Topic should not be empty.");
}
if (topic.IndexOf("#") != -1 && topic.IndexOf("#") != topic.Length - 1) throw new MqttProtocolViolationException("The character '#' is only allowed as last character");
}
}
}
+6 -1
View File
@@ -113,7 +113,12 @@ namespace MQTTnet.Server
{
if (clientId == null) throw new ArgumentNullException(nameof(clientId));
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
foreach (var topicFilter in topicFilters)
{
MqttTopicValidator.ThrowIfInvalidSubscribe(topicFilter.Topic);
}
ThrowIfDisposed();
ThrowIfNotStarted();