Skip to content

Commit

Permalink
never allow unmasked client websocket package
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Aug 8, 2024
1 parent 91433fc commit 14ced0f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/SuperSocket.WebSocket/WebSocketDataPipelineFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ namespace SuperSocket.WebSocket
{
public class WebSocketDataPipelineFilter : PackagePartsPipelineFilter<WebSocketPackage>
{
private HttpHeader _httpHeader;
private readonly HttpHeader _httpHeader;

private readonly bool _requireMask = true;

/// <summary>
/// -1: default value
Expand All @@ -16,9 +18,10 @@ public class WebSocketDataPipelineFilter : PackagePartsPipelineFilter<WebSocketP
/// </summary>
private long _consumed = -1;

public WebSocketDataPipelineFilter(HttpHeader httpHeader)
public WebSocketDataPipelineFilter(HttpHeader httpHeader, bool requireMask = true)
{
_httpHeader = httpHeader;
_requireMask = requireMask;
}

protected override WebSocketPackage CreatePackage()
Expand Down Expand Up @@ -72,6 +75,11 @@ protected override void OnPartReaderSwitched(IPackagePartReader<WebSocketPackage
{
if (currentPartReader is FixPartReader)
{
if (_requireMask && !CurrentPackage.HasMask)
{
throw new ProtocolException("Mask is required for this websocket package.");
}

// not final fragment or is the last fragment of multiple fragments message
// _consumed = 0 means we are ready to preserve the bytes
if (!CurrentPackage.FIN || CurrentPackage.Head != null)
Expand Down
15 changes: 13 additions & 2 deletions src/SuperSocket.WebSocket/WebSocketPipelineFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ public class WebSocketPipelineFilter : IPipelineFilter<WebSocketPackage>
private static readonly char _COLON = ':';

private static readonly ReadOnlyMemory<byte> _headerTerminator = new byte[] { (byte)'\r', (byte)'\n', (byte)'\r', (byte)'\n' };


private readonly bool _requireMask = true;

public WebSocketPipelineFilter()
{
}

public WebSocketPipelineFilter(bool requireMask)
{
_requireMask = requireMask;
}

public IPackageDecoder<WebSocketPackage> Decoder { get; set; }

public IPipelineFilter<WebSocketPackage> NextFilter { get; internal set; }
Expand All @@ -33,7 +44,7 @@ public WebSocketPackage Filter(ref SequenceReader<byte> reader)

var package = ParseHandshake(ref pack);

NextFilter = new WebSocketDataPipelineFilter(package.HttpHeader);
NextFilter = new WebSocketDataPipelineFilter(package.HttpHeader, _requireMask);

return package;
}
Expand Down

0 comments on commit 14ced0f

Please sign in to comment.