#14 - Validate reserved bits.
This commit is contained in:
parent
207767a9b0
commit
7d11034790
|
|
@ -221,7 +221,12 @@ namespace Microsoft.AspNet.WebSockets.Protocol
|
||||||
{
|
{
|
||||||
if (!_firstDataOpCode.HasValue)
|
if (!_firstDataOpCode.HasValue)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException("A continuation can't be the first frame");
|
if (State == WebSocketState.Open)
|
||||||
|
{
|
||||||
|
await CloseOutputAsync(WebSocketCloseStatus.ProtocolError, "Invalid continuation frame", cancellationToken);
|
||||||
|
Abort();
|
||||||
|
}
|
||||||
|
throw new InvalidOperationException("A continuation can't be the first frame"); // TODO: WebSocketException
|
||||||
}
|
}
|
||||||
opCode = _firstDataOpCode.Value;
|
opCode = _firstDataOpCode.Value;
|
||||||
}
|
}
|
||||||
|
|
@ -256,8 +261,12 @@ namespace Microsoft.AspNet.WebSockets.Protocol
|
||||||
if (messageType == WebSocketMessageType.Text
|
if (messageType == WebSocketMessageType.Text
|
||||||
&& !Utilities.TryValidateUtf8(new ArraySegment<byte>(buffer.Array, buffer.Offset, bytesToCopy), _frameInProgress.Fin, _incomingUtf8MessageState))
|
&& !Utilities.TryValidateUtf8(new ArraySegment<byte>(buffer.Array, buffer.Offset, bytesToCopy), _frameInProgress.Fin, _incomingUtf8MessageState))
|
||||||
{
|
{
|
||||||
await CloseOutputAsync(WebSocketCloseStatus.InvalidPayloadData, string.Empty, cancellationToken);
|
if (State == WebSocketState.Open)
|
||||||
throw new InvalidOperationException("An invalid UTF-8 payload was received.");
|
{
|
||||||
|
await CloseOutputAsync(WebSocketCloseStatus.InvalidPayloadData, "Invalid UTF-8", cancellationToken);
|
||||||
|
Abort();
|
||||||
|
}
|
||||||
|
throw new InvalidOperationException("An invalid UTF-8 payload was received."); // TODO: WebSocketException
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bytesToCopy == _frameBytesRemaining)
|
if (bytesToCopy == _frameBytesRemaining)
|
||||||
|
|
@ -292,9 +301,24 @@ namespace Microsoft.AspNet.WebSockets.Protocol
|
||||||
_receiveBufferBytes -= frameHeaderSize;
|
_receiveBufferBytes -= frameHeaderSize;
|
||||||
_frameBytesRemaining = _frameInProgress.DataLength;
|
_frameBytesRemaining = _frameInProgress.DataLength;
|
||||||
|
|
||||||
|
if (_frameInProgress.AreReservedSet())
|
||||||
|
{
|
||||||
|
if (State == WebSocketState.Open)
|
||||||
|
{
|
||||||
|
await CloseOutputAsync(WebSocketCloseStatus.ProtocolError, "Unexpected reserved bits set", cancellationToken);
|
||||||
|
Abort();
|
||||||
|
}
|
||||||
|
throw new InvalidOperationException("Unexpected reserved bits are set."); // TODO: WebSocketException
|
||||||
|
}
|
||||||
|
|
||||||
if (_unmaskInput != _frameInProgress.Masked)
|
if (_unmaskInput != _frameInProgress.Masked)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException("Unmasking settings out of sync with data.");
|
if (State == WebSocketState.Open)
|
||||||
|
{
|
||||||
|
await CloseOutputAsync(WebSocketCloseStatus.ProtocolError, "Incorrect masking", cancellationToken);
|
||||||
|
Abort();
|
||||||
|
}
|
||||||
|
throw new InvalidOperationException("Unmasking settings out of sync with data."); // TODO: WebSocketException
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_frameInProgress.OpCode == Constants.OpCodes.PingFrame || _frameInProgress.OpCode == Constants.OpCodes.PongFrame)
|
if (_frameInProgress.OpCode == Constants.OpCodes.PingFrame || _frameInProgress.OpCode == Constants.OpCodes.PongFrame)
|
||||||
|
|
|
||||||
|
|
@ -209,6 +209,12 @@ namespace Microsoft.AspNet.WebSockets.Protocol
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bits 1-3.
|
||||||
|
internal bool AreReservedSet()
|
||||||
|
{
|
||||||
|
return (_header[0] & 0x70) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Given the second bytes of a frame, calculate how long the whole frame header should be.
|
// Given the second bytes of a frame, calculate how long the whole frame header should be.
|
||||||
// Range 2-12 bytes
|
// Range 2-12 bytes
|
||||||
public static int CalculateFrameHeaderSize(byte b2)
|
public static int CalculateFrameHeaderSize(byte b2)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue