mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-17 01:33:03 +08:00
improve exceptions in case of corrupt packages close pipes after exception occoured to avoid lingering connections should fix #788
This commit is contained in:
@@ -145,6 +145,13 @@ namespace MQTTnet.AspNetCore
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// completing the cannels makes sure that there is no more data read after a protocol error
|
||||
_input?.Complete(e);
|
||||
_output?.Complete(e);
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
ReadingPacketCompletedCallback?.Invoke();
|
||||
|
||||
@@ -117,6 +117,10 @@ namespace MQTTnet.AspNetCore
|
||||
var span = _buffer.Span;
|
||||
var length = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(_offset));
|
||||
|
||||
if (Length < _offset + length)
|
||||
{
|
||||
throw new MqttProtocolViolationException($"Expected at least {_offset + 2 + length} bytes but there are only {Length} bytes");
|
||||
}
|
||||
var result = span.Slice(_offset + 2, length);
|
||||
_offset += 2 + length;
|
||||
return result;
|
||||
|
||||
@@ -10,6 +10,11 @@ namespace MQTTnet.AspNetCore
|
||||
{
|
||||
readonly ArrayPool<byte> _pool = ArrayPool<byte>.Create();
|
||||
|
||||
public SpanBasedMqttPacketWriter()
|
||||
{
|
||||
Reset(0);
|
||||
}
|
||||
|
||||
byte[] _buffer;
|
||||
int _position;
|
||||
|
||||
|
||||
@@ -134,7 +134,7 @@ namespace MQTTnet.Formatter
|
||||
{
|
||||
if (_length < _offset + length)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(_buffer), $"Expected at least {_offset + length} bytes but there are only {_length} bytes");
|
||||
throw new MqttProtocolViolationException($"Expected at least {_offset + length} bytes but there are only {_length} bytes");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -120,6 +120,14 @@ namespace MQTTnet.Formatter
|
||||
{
|
||||
if (receivedMqttPacket == null) throw new ArgumentNullException(nameof(receivedMqttPacket));
|
||||
|
||||
if (receivedMqttPacket.Body.Length < 7)
|
||||
{
|
||||
// 2 byte protocol name length
|
||||
// at least 4 byte protocol name
|
||||
// 1 byte protocol level
|
||||
throw new MqttProtocolViolationException("Mqtt Connect packet must have at least 7 bytes");
|
||||
}
|
||||
|
||||
var protocolName = receivedMqttPacket.Body.ReadStringWithLengthPrefix();
|
||||
var protocolLevel = receivedMqttPacket.Body.ReadByte();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user