Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix frame boundary detection in SslStream #104606

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ private ProtocolToken ProcessTlsFrame(int frameSize)
{
TlsFrameHeader nextHeader = default;

if (!TlsFrameHelper.TryGetFrameHeader(_buffer.EncryptedReadOnlySpan.Slice(chunkSize), ref nextHeader))
if (!TlsFrameHelper.TryGetFrameHeader(availableData.Slice(chunkSize), ref nextHeader))
{
break;
}
Expand All @@ -478,7 +478,7 @@ private ProtocolToken ProcessTlsFrame(int frameSize)

// Can process more handshake frames in single step or during TLS1.3 post-handshake auth, but we should
// avoid processing too much so as to preserve API boundary between handshake and I/O.
if ((nextHeader.Type != TlsContentType.Handshake && nextHeader.Type != TlsContentType.ChangeCipherSpec) && !_isRenego || frameSize > _buffer.EncryptedLength)
if ((nextHeader.Type != TlsContentType.Handshake && nextHeader.Type != TlsContentType.ChangeCipherSpec) && !_isRenego || frameSize > availableData.Length - chunkSize)
{
// We don't have full frame left or we already have app data which needs to be processed by decrypt.
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public enum FramingType
// 1 byte reads
ByteByByte,

// coalesce reads to biggest chunks possible
// Receive data at chunks, not necessarily respecting frame boundaries
Chunked,

// Coalesce reads to biggest chunks possible
Coalescing
}

Expand Down Expand Up @@ -129,7 +132,6 @@ public async Task Handshake_Success(FramingType framingType, SslProtocols sslPro
Assert.True(serverStream.ReadCalled, "Mocked read method was not used");

await TestHelper.PingPong(client, server);

}

internal class ConfigurableReadStream : Stream
Expand Down Expand Up @@ -168,6 +170,7 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
{
case FramingType.ByteByByte:
return await _stream.ReadAsync(buffer.Length > 0 ? buffer.Slice(0, 1) : buffer, cancellationToken);

case FramingType.Coalescing:
{
if (buffer.Length > 0)
Expand All @@ -178,6 +181,24 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
}
return await _stream.ReadAsync(buffer, cancellationToken);
}
case FramingType.Chunked:
{
if (buffer.Length > 0)
{
// wait 10ms, this should be enough for the other side to write as much data
// as it will ever write before receiving something back.
await Task.Delay(10);

const int maxRead = 1519; // arbitrarily chosen chunk size

if (buffer.Length > maxRead)
{
buffer = buffer.Slice(0, maxRead);
}
}
return await _stream.ReadAsync(buffer, cancellationToken);
}

default:
throw new NotImplementedException();
}
Expand Down
Loading