Skip to content

Commit 1a6e1fb

Browse files
committed
[Modify] Add an Opcode check
1 parent 842b5b3 commit 1a6e1fb

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

websocket-sharp/WebSocketFrame.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,33 @@ private static string dump (WebSocketFrame frame)
377377
return output.ToString ();
378378
}
379379

380+
private static bool isControl (byte opcode)
381+
{
382+
return opcode == (byte) Opcode.Close ||
383+
opcode == (byte) Opcode.Ping ||
384+
opcode == (byte) Opcode.Pong;
385+
}
386+
380387
private static bool isControl (Opcode opcode)
381388
{
382389
return opcode == Opcode.Close || opcode == Opcode.Ping || opcode == Opcode.Pong;
383390
}
384391

392+
private static bool isData (byte opcode)
393+
{
394+
return opcode == (byte) Opcode.Text || opcode == (byte) Opcode.Binary;
395+
}
396+
385397
private static bool isData (Opcode opcode)
386398
{
387399
return opcode == Opcode.Text || opcode == Opcode.Binary;
388400
}
389401

402+
private static bool isSupported (byte opcode)
403+
{
404+
return Enum.IsDefined (typeof (Opcode), opcode);
405+
}
406+
390407
private static string print (WebSocketFrame frame)
391408
{
392409
// Payload Length
@@ -452,7 +469,7 @@ private static WebSocketFrame processHeader (byte[] header)
452469
var rsv3 = (header[0] & 0x10) == 0x10 ? Rsv.On : Rsv.Off;
453470

454471
// Opcode
455-
var opcode = (Opcode) (header[0] & 0x0f);
472+
var opcode = (byte) (header[0] & 0x0f);
456473

457474
// MASK
458475
var mask = (header[1] & 0x80) == 0x80 ? Mask.On : Mask.Off;
@@ -461,13 +478,15 @@ private static WebSocketFrame processHeader (byte[] header)
461478
var payloadLen = (byte) (header[1] & 0x7f);
462479

463480
// Check if valid header.
464-
var err = isControl (opcode) && payloadLen > 125
465-
? "A control frame has payload data which is greater than the allowable max length."
481+
var err = !isSupported (opcode)
482+
? "An unsupported opcode."
466483
: isControl (opcode) && fin == Fin.More
467484
? "A control frame is fragmented."
468-
: !isData (opcode) && rsv1 == Rsv.On
469-
? "A non data frame is compressed."
470-
: null;
485+
: isControl (opcode) && payloadLen > 125
486+
? "A control frame has a long payload length than the allowable max length."
487+
: !isData (opcode) && rsv1 == Rsv.On
488+
? "A non data frame is compressed."
489+
: null;
471490

472491
if (err != null)
473492
throw new WebSocketException (CloseStatusCode.ProtocolError, err);
@@ -477,7 +496,7 @@ private static WebSocketFrame processHeader (byte[] header)
477496
frame._rsv1 = rsv1;
478497
frame._rsv2 = rsv2;
479498
frame._rsv3 = rsv3;
480-
frame._opcode = opcode;
499+
frame._opcode = (Opcode) opcode;
481500
frame._mask = mask;
482501
frame._payloadLength = payloadLen;
483502

0 commit comments

Comments
 (0)