-
Notifications
You must be signed in to change notification settings - Fork 10.3k
feat: support tls client hello bytes callback in Kestrel #61631
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
base: main
Are you sure you want to change the base?
Conversation
src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.cs
Outdated
Show resolved
Hide resolved
src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.cs
Outdated
Show resolved
Hide resolved
src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.cs
Outdated
Show resolved
Hide resolved
src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.cs
Outdated
Show resolved
Hide resolved
src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM -- just a small comment.
src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.cs
Outdated
Show resolved
Hide resolved
src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.cs
Outdated
Show resolved
Hide resolved
src/Servers/Kestrel/test/InMemory.FunctionalTests/TlsListenerMiddlewareTests.cs
Show resolved
Hide resolved
src/Servers/Kestrel/test/InMemory.FunctionalTests/TlsListenerMiddlewareTests.Units.cs
Outdated
Show resolved
Hide resolved
src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.cs
Outdated
Show resolved
Hide resolved
src/Servers/Kestrel/Core/src/Middleware/TlsListenerMiddleware.cs
Outdated
Show resolved
Hide resolved
src/Servers/Kestrel/test/InMemory.FunctionalTests/TlsListenerMiddlewareTests.Units.cs
Outdated
Show resolved
Hide resolved
src/Servers/Kestrel/test/InMemory.FunctionalTests/TlsListenerMiddlewareTests.Units.cs
Outdated
Show resolved
Hide resolved
… duplicate registration
add tests for SSL 2.0 and SSL 3.0 |
done |
/// <summary> | ||
/// A callback to be invoked to get the TLS client hello bytes. | ||
/// Null by default. | ||
/// </summary> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to store the bytes from the ReadOnlySequence<byte>, copy them into a buffer that you control rather than keeping a reference to the ReadOnlySequence or ReadOnlyMemory instances.
@@ -91,7 +91,7 @@ public override async ValueTask DisposeAsync() | |||
// This piece of code allows us to wait until the PipeReader has been awaited on. | |||
// We need to wrap lots of layers (including the ValueTask) to gain visiblity into when | |||
// the machinery for the await happens | |||
private class ObservableDuplexPipe : IDuplexPipe | |||
internal class ObservableDuplexPipe : IDuplexPipe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
undo changes to this file
{ | ||
[Theory] | ||
[MemberData(nameof(ValidClientHelloData))] | ||
public Task OnTlsClientHelloAsync_ValidData(int id, byte[] packetBytes, bool nextMiddlewareInvoked) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nextMiddlewareInvoked
is always true?
); | ||
|
||
await writer.WriteAsync(new byte[1] { 0x16 }); | ||
var middlewareTask = Task.Run(() => middleware.OnTlsClientHelloAsync(transportConnection)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to use Task.Run
?
Assert.False(tlsClientHelloCallbackInvoked); | ||
|
||
// ensuring that we have read limited number of times | ||
Assert.True(reader.ReadAsyncCounter is >= 2 && reader.ReadAsyncCounter is <= 5, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be 2-3?
{ | ||
var pipe = new Pipe(); | ||
var writer = pipe.Writer; | ||
var reader = new ObservablePipeReader(pipe.Reader); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: not needed?
var middlewareTask = Task.Run(() => middleware.OnTlsClientHelloAsync(transportConnection)); | ||
|
||
var random = new Random(); | ||
await Task.Delay(millisecondsDelay: random.Next(25, 75)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delays should not be needed. If you need to deterministically do something then you might want to make use of TaskCompletionSource.
e.g.
TaskCompletionSource Tcs => _tcs;
async ValueTask<ReadResult> ReadAsync(CancellationToken ...)
{
await _tcs.Task;
_tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAscynchronously);
var res = await _inner.ReadAsync();
return res;
}
{ | ||
var pipe = new Pipe(); | ||
var writer = pipe.Writer; | ||
var reader = new ObservablePipeReader(pipe.Reader); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unneeded?
} | ||
} | ||
|
||
private static byte[] valid_clientHelloHeader = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually use PascalCase for properties/fields in this repo
=> RunTlsClientHelloCallbackTest_WithMultipleSegments(id, packets, nextMiddlewareInvoked, tlsClientHelloCallbackExpected: false); | ||
|
||
[Fact] | ||
public async Task RunTlsClientHelloCallbackTest_DeterministinglyReads() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like you're trying to do the same thing as RunTlsClientHelloCallbackTest_WithMultipleSegments
in this test, any reason this one should exist?
Supporting TLS Client Hello callback in Kestrel
HTTP.SYS contribution was done in this PR
Description
Adding a new property to
HttpsConnectionAdapterOptions
-TlsClientHelloBytesCallback
(added to public API).It allows to subscribe to the TLS client hello message parsed from the
ConnectionContext.Transport.Input
:If property
HttpsConnectionAdapterOptions.TlsClientHelloBytesCallback
is set (not null), then new middleware is added beforeHttpsConnectionMiddleware
.The implementation is doing the following:
Fixes #60805