Repository navigation
Description
Description
When using the new ConnectCallback feature, returning an SslStream results in errors related to TLS negotiation. HttpConnectionPool expects to be in charge of negotiating SSL over the returned stream, but design eliminates lots of possible transport-level opportunities for people who want or need to control or manage transport processing.
Here is an example implementation of ConnectCallback that will fail as a result of this issue.
var retVal = new HttpMessageInvoker(
new SocketsHttpHandler()
{
ConnectCallback = async (ctx, cancellationToken) =>
{
var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
await socket.ConnectAsync(ctx.DnsEndPoint);
var networkStream = new NetworkStream(socket);
var sslStream = new SslStream(networkStream);
await sslStream.AuthenticateAsClientAsync(ctx.RequestMessage.RequestUri.Host);
return sslStream;
}
}
);Ideally, HttpConnectionPool would manage negotiating TLS for me if I returned a Stream implementation, but if I returned an SslStream, it ought to defer negotiation to me.
Configuration
Latest bits from release/5.0-rc2, not arch or platform specific.
Regression?
Not a regression.
Other information
Being able to return an SslStream (or other stream types) is important because it enables the ConnectCallback to provide a facility for transport-level optimizations like stream multiplexing (imagine a stream implementation that's built atop a series of TCP sockets for compatibility reasons, but actually parallelizes writes over multiple underlying TCP connections), TLS pre-negotiation (TLS negotiation for small payloads is expensive, perhaps the implementor of the ConnectCallback would like to maintain a pool of pre-negotiated TLS sessions for specific destinations) and telemetry (streams returned by ConnectCallback can't effectively have their contents intercepted by other code running in the stack).
This seems like something as simple as a type check on the returned stream would be sufficient:
if (!(is SslStream)) {
/// negotiate TLS in the usual, existing way
}I'm happy to propose a PR with a fix for this issue along with corresponding tests.
Activity
[-]Unable to return SslStream from SocketsHttpHandler.ConnectCallback[/-][+]Enable SocketsHttpHandler.ConnectCallback to negotiate TLS[/+]brporter commentedon Sep 18, 2020
I've proposed a PR with a fix, along with a test. (#42473)
geoffkizer commentedon Sep 18, 2020
ConnectCallback isn't intended to support this. It just lets you control how the connection is established. SocketsHttpHandler still owns the TLS negotiation.
I do think it's potentially interesting to allow you to control TLS negotiation as well, but I think we'd expose this as a separate hook.
Note that you can return an SslStream from ConnectCallback; you just can't replace the TLS negotiation logic. So you could specify an HTTP url and then actually establish a TLS connection under the covers if you want to.
scalablecory commentedon Sep 18, 2020
Initial prototyping for System.Net.Connections exposed a HTTPS middleware concept that allowed exactly this, but we weren't able to find a good real-world need for it so we dropped it.
mjsabby commentedon Sep 18, 2020
@geoffkizer this is our use-case as well, so I'm missing what is not possible.
brporter commentedon Sep 18, 2020
@geoffkizer understanding that the intent of ConnectCallback was just for socket creation, the basic interface lends itself to all sorts of potential optimizations. Given that the existing implementation of ConnectCallback expects you to return a stream and not a socket, enabling a caller to perform their own TLS negotiation is natural. I think it's actually more surprising to users that I can't return any old stream, and that the stream I provide has to be in a certain state.
This fixes that by removing any preconditions on the returned stream (for TLS anyway, although I don't see others in the code).
19 remaining items
wfurt commentedon Feb 24, 2022
There still may be time in 7.0 for some improvements mentioned in #63851. It would be great if interested folks can get daily build (or new preview) and provide feedback.