Parcourir la source

Add validation of packet control type.

release/3.x.x
Christian Kratky il y a 6 ans
Parent
révision
b5a43c416a
1 fichiers modifiés avec 11 ajouts et 5 suppressions
  1. +11
    -5
      Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketReader.cs

+ 11
- 5
Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketReader.cs Voir le fichier

@@ -12,7 +12,7 @@ namespace MQTTnet.Serializer
{
public static class MqttPacketReader
{
public static async Task<MqttPacketHeader> ReadHeaderAsync(IMqttChannel stream, CancellationToken cancellationToken)
public static async Task<MqttPacketHeader> ReadHeaderAsync(IMqttChannel channel, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
@@ -23,20 +23,26 @@ namespace MQTTnet.Serializer
// some large delay and thus the thread should be put back to the pool (await). So ReadByte()
// is not an option here.
var buffer = new byte[1];
var readCount = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
var readCount = await channel.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
if (readCount <= 0)
{
return null;
}

var fixedHeader = buffer[0];
var controlPacketType = (MqttControlPacketType)(fixedHeader >> 4);
var bodyLength = await ReadBodyLengthAsync(stream, cancellationToken).ConfigureAwait(false);
var controlPacketType = fixedHeader >> 4;

if (controlPacketType < 1 || controlPacketType > 14)
{
throw new MqttProtocolViolationException($"The packet type is invalid ({controlPacketType}).");
}

var bodyLength = await ReadBodyLengthAsync(channel, cancellationToken).ConfigureAwait(false);

return new MqttPacketHeader
{
FixedHeader = fixedHeader,
ControlPacketType = controlPacketType,
ControlPacketType = (MqttControlPacketType)controlPacketType,
BodyLength = bodyLength
};
}


Chargement…
Annuler
Enregistrer