Check if the received frame is correctly masked or not

This commit is contained in:
sta 2014-09-16 14:53:26 +09:00
parent dd25d1d0a4
commit 130b0a08d0
2 changed files with 39 additions and 9 deletions

View File

@ -648,7 +648,19 @@ namespace WebSocketSharp
private bool concatenateFragmentsInto (Stream destination)
{
while (true) {
var frame = WebSocketFrame.Read (_stream, true);
var frame = WebSocketFrame.Read (_stream, false);
var masked = frame.IsMasked;
if (_client && masked)
return processUnsupportedFrame (
frame, CloseStatusCode.ProtocolError, "A frame from the server is masked.");
if (!_client && !masked)
return processUnsupportedFrame (
frame, CloseStatusCode.ProtocolError, "A frame from a client isn't masked.");
if (masked)
frame.Unmask ();
if (frame.IsFinal) {
/* FINAL */
@ -1002,6 +1014,18 @@ namespace WebSocketSharp
private bool processWebSocketFrame (WebSocketFrame frame)
{
var masked = frame.IsMasked;
if (_client && masked)
return processUnsupportedFrame (
frame, CloseStatusCode.ProtocolError, "A frame from the server is masked.");
if (!_client && !masked)
return processUnsupportedFrame (
frame, CloseStatusCode.ProtocolError, "A frame from a client isn't masked.");
if (masked)
frame.Unmask ();
return frame.IsCompressed && _compression == CompressionMethod.None
? processUnsupportedFrame (
frame,
@ -1285,7 +1309,7 @@ namespace WebSocketSharp
Action receive = null;
receive = () => WebSocketFrame.ReadAsync (
_stream,
true,
false,
frame => {
if (processWebSocketFrame (frame) && _readyState != WebSocketState.Closed) {
receive ();

View File

@ -516,14 +516,10 @@ Extended Payload Length: {7}
data = new byte[0];
}
var payload = new PayloadData (data, masked);
if (masked && unmask) {
payload.Mask (maskingKey);
frame._mask = Mask.Unmask;
frame._maskingKey = new byte[0];
}
frame._payloadData = new PayloadData (data, masked);
if (unmask && masked)
frame.Unmask ();
frame._payloadData = payload;
return frame;
}
@ -606,6 +602,16 @@ Extended Payload Length: {7}
error);
}
internal void Unmask ()
{
if (_mask == Mask.Unmask)
return;
_payloadData.Mask (_maskingKey);
_maskingKey = new byte[0];
_mask = Mask.Unmask;
}
#endregion
#region Public Methods