Skip to content

Commit 0581163

Browse files
committed
exit even if not enough data
1 parent 1fcffb8 commit 0581163

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal async Task OnTlsClientHelloAsync(ConnectionContext connection)
3434
{
3535
// If the buffer length is less than 6 bytes (handshake + version + length + client-hello byte)
3636
// and no more data is coming, we can't block in a loop here because we will not get more data
37-
if (buffer.Length < 6 && result.IsCompleted)
37+
if (result.IsCompleted && buffer.Length < 6)
3838
{
3939
break;
4040
}
@@ -43,6 +43,13 @@ internal async Task OnTlsClientHelloAsync(ConnectionContext connection)
4343

4444
if (parseState == ClientHelloParseState.NotEnoughData)
4545
{
46+
// if no data will be added, and we still lack enough bytes
47+
// we can't block in a loop, so just exit
48+
if (result.IsCompleted)
49+
{
50+
break;
51+
}
52+
4653
continue;
4754
}
4855

src/Servers/Kestrel/test/InMemory.FunctionalTests/TlsListenerMiddlewareTests.Units.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,21 @@ public static IEnumerable<object[]> InvalidClientHelloData_Segmented()
480480
// not enough data - so incorrect
481481
};
482482

483+
private static byte[] invalid_9BytesMessage =
484+
{
485+
// 0x16 = Handshake
486+
0x16,
487+
// 0x0301 = TLS 1.0
488+
0x03, 0x01,
489+
// length = 0x0020 (32 bytes)
490+
0x00, 0x20,
491+
// Handshake.msg_type (client hello)
492+
0x01,
493+
// should have 31 bytes (zeros for simplicity)
494+
0, 0, 0
495+
// no other data here - incorrect
496+
};
497+
483498
private static byte[] invalid_UnknownProtocolVersion1 =
484499
{
485500
// Handshake
@@ -529,6 +544,7 @@ public static IEnumerable<object[]> InvalidClientHelloData_Segmented()
529544

530545
private static List<byte[]> invalid_collection = new List<byte[]>()
531546
{
532-
invalid_TlsClientHelloHeader, invalid_3BytesMessage, invalid_UnknownProtocolVersion1, invalid_UnknownProtocolVersion2, invalid_IncorrectHandshakeMessageType
547+
invalid_TlsClientHelloHeader, invalid_3BytesMessage, invalid_9BytesMessage,
548+
invalid_UnknownProtocolVersion1, invalid_UnknownProtocolVersion2, invalid_IncorrectHandshakeMessageType
533549
};
534550
}

0 commit comments

Comments
 (0)