mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-21 12:16:02 +08:00
Add validations of variable byte integers (#2214)
* Add validations and add docs * Update release notes * Fix build
This commit is contained in:
@@ -6,4 +6,15 @@ using System;
|
||||
|
||||
namespace MQTTnet.Exceptions;
|
||||
|
||||
public class MqttProtocolViolationException(string message) : Exception(message);
|
||||
public class MqttProtocolViolationException(string message) : Exception(message)
|
||||
{
|
||||
public const uint VariableByteIntegerMaxValue = 268435455;
|
||||
|
||||
public static void ThrowIfVariableByteIntegerExceedsLimit(uint value)
|
||||
{
|
||||
if (value > VariableByteIntegerMaxValue)
|
||||
{
|
||||
throw new MqttProtocolViolationException($"The value {value} is too large for a variable byte integer ({VariableByteIntegerMaxValue}).");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ namespace MQTTnet.Formatter;
|
||||
/// </summary>
|
||||
public sealed class MqttBufferWriter
|
||||
{
|
||||
const uint VariableByteIntegerMaxValue = 268435455;
|
||||
const int EncodedStringMaxLength = 65535;
|
||||
|
||||
readonly int _maxBufferSize;
|
||||
@@ -223,10 +222,7 @@ public sealed class MqttBufferWriter
|
||||
return;
|
||||
}
|
||||
|
||||
if (value > VariableByteIntegerMaxValue)
|
||||
{
|
||||
throw new MqttProtocolViolationException($"The specified value ({value}) is too large for a variable byte integer.");
|
||||
}
|
||||
MqttProtocolViolationException.ThrowIfVariableByteIntegerExceedsLimit(value);
|
||||
|
||||
var size = 0;
|
||||
var x = value;
|
||||
|
||||
@@ -21,7 +21,6 @@ public sealed class MqttApplicationMessageBuilder
|
||||
string _contentType;
|
||||
byte[] _correlationData;
|
||||
uint _messageExpiryInterval;
|
||||
|
||||
MqttPayloadFormatIndicator _payloadFormatIndicator;
|
||||
ReadOnlySequence<byte> _payload;
|
||||
MqttQualityOfServiceLevel _qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce;
|
||||
@@ -84,6 +83,8 @@ public sealed class MqttApplicationMessageBuilder
|
||||
/// </summary>
|
||||
public MqttApplicationMessageBuilder WithMessageExpiryInterval(uint messageExpiryInterval)
|
||||
{
|
||||
// No validation required because this is a 4 byte integer!
|
||||
|
||||
_messageExpiryInterval = messageExpiryInterval;
|
||||
return this;
|
||||
}
|
||||
@@ -228,9 +229,11 @@ public sealed class MqttApplicationMessageBuilder
|
||||
/// </summary>
|
||||
public MqttApplicationMessageBuilder WithSubscriptionIdentifier(uint subscriptionIdentifier)
|
||||
{
|
||||
MqttProtocolViolationException.ThrowIfVariableByteIntegerExceedsLimit(subscriptionIdentifier);
|
||||
|
||||
if (_subscriptionIdentifiers == null)
|
||||
{
|
||||
_subscriptionIdentifiers = new List<uint>();
|
||||
_subscriptionIdentifiers = [];
|
||||
}
|
||||
|
||||
_subscriptionIdentifiers.Add(subscriptionIdentifier);
|
||||
|
||||
Reference in New Issue
Block a user