Browse Source

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

release/3.x.x
HansM 3 years ago
committed by GitHub
parent
commit
3cdf6d1261
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 2 deletions
  1. +1
    -1
      Build/MQTTnet.nuspec
  2. +5
    -0
      Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs
  3. +5
    -0
      Source/MQTTnet/Client/MqttClient.cs
  4. +10
    -0
      Source/MQTTnet/Protocol/MqttTopicValidator.cs
  5. +6
    -1
      Source/MQTTnet/Server/MqttServer.cs
  6. +54
    -0
      Tests/MQTTnet.Core.Tests/MqttTopicValidatorSubscribe_Tests.cs

+ 1
- 1
Build/MQTTnet.nuspec View File

@@ -13,7 +13,7 @@
<description>MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker) and supports v3.1.0, v3.1.1 and v5.0.0 of the MQTT protocol.</description>
<releaseNotes>
* [MQTTnet.Server] Moved server project to a dedicated GitHub repository.
* [MQTTnet, MQTTnet.Extensions.ManagedClient] Fixed bug that allowed invalid subscriptions (Thanks to @marcelwinh).
Git commit: $gitCommit
</releaseNotes>
<copyright>Copyright Christian Kratky 2016-2020</copyright>


+ 5
- 0
Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs View File

@@ -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
- 0
Source/MQTTnet/Client/MqttClient.cs 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();



+ 10
- 0
Source/MQTTnet/Protocol/MqttTopicValidator.cs View File

@@ -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
Source/MQTTnet/Server/MqttServer.cs 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();



+ 54
- 0
Tests/MQTTnet.Core.Tests/MqttTopicValidatorSubscribe_Tests.cs View File

@@ -0,0 +1,54 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Exceptions;
using MQTTnet.Protocol;

namespace MQTTnet.Tests
{
[TestClass]
public class MqttTopicValidatorSubscribe_Tests
{
[TestMethod]
public void Valid_Topic()
{
MqttTopicValidator.ThrowIfInvalidSubscribe("/a/b/c");
}

[TestMethod]
public void Valid_Topic_Plus_In_Between()
{
MqttTopicValidator.ThrowIfInvalidSubscribe("/a/+/c");
}

[TestMethod]
public void Valid_Topic_Plus_Last_Char()
{
MqttTopicValidator.ThrowIfInvalidSubscribe("/a/+");
}

[TestMethod]
public void Valid_Topic_Hash_Last_Char()
{
MqttTopicValidator.ThrowIfInvalidSubscribe("/a/#");
}

[TestMethod]
public void Valid_Topic_Only_Hash()
{
MqttTopicValidator.ThrowIfInvalidSubscribe("#");
}

[TestMethod]
[ExpectedException(typeof(MqttProtocolViolationException))]
public void Invalid_Topic_Hash_In_Between()
{
MqttTopicValidator.ThrowIfInvalidSubscribe("/a/#/c");
}

[TestMethod]
[ExpectedException(typeof(MqttProtocolViolationException))]
public void Invalid_Topic_Empty()
{
MqttTopicValidator.ThrowIfInvalidSubscribe(string.Empty);
}
}
}

Loading…
Cancel
Save