Skip to content

Use handshake timeout for Tls listener callback #62177

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

DeagleGross
Copy link
Member

Problem

HttpsConnectionMiddleware carefully considers a handshake timeout.

New TlsListenerMiddleware does not use it, and in case of a configured timeout request will not be cancelled.

Solution

I renamed TlsListenerMiddleware to TlsListener to be used as part of HttpsConnectionMiddleware. It will use the same cancellation token as the latter. We can move the code directly into HttpsConnectionMiddleware but IMO TlsListener has a bit different purpose and can be living in a separate instance. TlsListener now does not invoke next middleware delegate.

Also changed the parsing code to remember the TLS client hello record length, and if that is known re-parsing of TLS client hello "header" will not be happening on each iteration.

Closes #62172

@DeagleGross DeagleGross self-assigned this May 30, 2025
@github-actions github-actions bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label May 30, 2025
Copy link
Member

@halter73 halter73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@DeagleGross
Copy link
Member Author

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@@ -21,7 +21,7 @@

namespace InMemory.FunctionalTests;

public class TlsListenerMiddlewareTests : TestApplicationErrorLoggerLoggedTest
public class TlsListenerTests : TestApplicationErrorLoggerLoggedTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably add a cancellation test here as well. Set the handshake timeout to something small like 1 millisecond and check that the request was canceled.

@@ -50,6 +50,57 @@ public Task OnTlsClientHelloAsync_ValidData_MultipleSegments(int id, List<byte[]
public Task OnTlsClientHelloAsync_InvalidData_MultipleSegments(int id, List<byte[]> packets)
=> RunTlsClientHelloCallbackTest_WithMultipleSegments(id, packets, tlsClientHelloCallbackExpected: false);

[Fact]
public async Task RunTlsClientHelloCallbackTest_WithPreCancelledToken()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public async Task RunTlsClientHelloCallbackTest_WithPreCancelledToken()
public async Task RunTlsClientHelloCallbackTest_WithPreCanceledToken()

{
clientHelloBytes = default;

// in case bad actor will be sending a TLS client hello one byte at a time
// and we know the expected length of TLS client hello,
// we can check and fail fastly here instead of re-parsing the TLS client hello "header" on each iteration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// we can check and fail fastly here instead of re-parsing the TLS client hello "header" on each iteration
// we can check and fail quickly here instead of re-parsing the TLS client hello "header" on each iteration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TlsListenerMiddleware does not use handshake timeout
3 participants