When choosing what authentication scheme to use, WinHttpHandler picks the most secure scheme supported by the server. When the client does not support this scheme it closes the connection. This fix updates the authentication logic to instead pick the most secure scheme offered by the server that is also supported by the client.
This change also adds a test of the relevant behavior.
Fixes: dotnet/corefx#27672
Commit migrated from https://github.com/dotnet/corefx/commit/6d031a04f3df8fadd5b82d9c23d904182eea94e4
.NET Core does not allow insecure redirects from https to http, but .NET Framework does. Developers have had some trouble diagnosing the issue when they run into this difference. This change adds logging to help developers track down what is going on.
Commit migrated from https://github.com/dotnet/corefx/commit/1fd7d79fc36ca113bdabbee4204e15ae5bcde24a
* Enable SocketsHttpHandler cancellation support
This change significantly improves the cancellation support in SocketsHttpHandler. Previously we were passing the CancellationToken around to every method, eventually bottoming out in calls to the underlying Stream which then ends up passing them down to the underlying Socket. But today Socket's support for cancellation is minimal, only doing up-front checks; if cancellation is requested during the socket operation rather than before, the request will be ignored. Since HttpClient implements features like timeouts on top of cancellation support, it's important to do better than this.
The change implements cancellation by registering with the CancellationToken to dispose of the connection. This will cause any reads/writes to wake up. We then translate resulting exceptions into cancellation exceptions. When in the main SendAsync method, we register once for the whole body of the operation until the point that we're returning the response message. For individual operations on the response content stream, we register per operation; however, when feasible we try to avoid the registration costs by only registering if operations don't complete synchronously. We also account for the case that on Unix, closing the connection may result in read operations waking up not with an exception but rather with EOF, which we also need to translate into cancellation when appropriate.
Along the way I cleaned up a few minor issues as well.
I also added a bunch of cancellation-related tests:
- Test cancellation occurring while sending request content
- Test cancellation occurring while receiving response headers
- Test cancellation occurring while receiving response body and using a buffered operation
- Test that all of the above are triggerable with CancellationTokenSource.Cancel, HttpClient.CancelPendingRequests, and HttpClient.Dispose
- Test cancellation occurring while receiving response body and using an unbuffered operation, either a ReadAsync or CopyToAsync on the response stream
- Test that a CancelPendingRequests doesn't affect unbuffered operations on the response stream
There are deficiencies here in the existing handlers, and tests have been selectively disabled accordingly (I also fixed a couple cases that naturally fell out of the changes I was making for SocketsHttpHandler). SocketsHttpHandler passes now for all of them.
* Add test that Dispose doesn't cancel response stream
Commit migrated from https://github.com/dotnet/corefx/commit/53be85c2fe473fdea8c001e3d9fd81dd478b858e
* Clear initlocals in a bunch of assemblies
Sets
```XML
<ILLinkClearInitLocals>true</ILLinkClearInitLocals>
```
for the assemblies:
- System.IO.FileSystem
- System.IO.Pipes
- System.Memory
- System.Net.Http
- System.Net.Http.WinHttpHandler
- System.Net.NameResolution
- System.Net.NetworkInformation
- System.Net.Primitives
- System.Net.Requests
- System.Net.Security
- System.Net.Sockets
- System.Net.WebSockets
- System.Net.WebSockets.Client
- System.Net.WebSockets.WebSocketsProtocol
- System.Private.Uri
- System.Runtime
- System.Runtime.Extensions
Eventually we'd ideally set the flag for the whole repo; these are just the ones I audited thus far. In a few cases I had to make code changes to adapt, where the code was expecting stackallocs to be cleared. I also looked at a few other projects (System.Collections, System.Runtime.Numerics) where there were more significant dependencies on clearing and I decided to leave those for later.
* Disable ILLinkClearInitLocals running on Unix until it works there
Commit migrated from https://github.com/dotnet/corefx/commit/f0a032e4a1c8a848574a5db3e69e8a6fa0aae91e
When a user has added an Authorization header to a request via the HttpClient DefaultRequestHeaders Authorization field, ensure that header is stripped on redirect.
This change adds a method to strip Authorization headers that were set via the HttpClient DefaultRequestHeaders Authorization field, and calls that method on redirect. In addition, it adds a test to ensure that this behavior occurs as expected.
The new method ResetAuthRequestHeaders is based on the method ResetCookieRequestHeaders, which is used to reset cookie headers on redirect.
The issue also occurs in CurlHandler, but the fix is slightly different. Rather than remove the headers that have already been set, we can instead just skip them when copying over the new headers to the redirected request.
Fixesdotnet/corefx#26426
Commit migrated from https://github.com/dotnet/corefx/commit/7e6396b2bcb4d372b199cfb7b3b13e290c441124
* Fix WinHttpHandler when building certificate chain
Unlike SslStream, WinHttpHandler was not using all the certificates
obtained from the TLS/SSL handshake when building the chain. In
addition to the end-entity server certificate, there were other certs
available in the native cert handle that could be passed along
to the chain build operation.
As part of fixing this, I refactored the Common schannel and crypto
interop code so that it can be used in both SslStream and
WinHttpHandler.
I tested this manually because this cannot be easily tested in the CI
system. Added traces in WinHttpHandler to support this.
Fixesdotnet/corefx#23827Fixesdotnet/corefx#4473
* Address PR feedback
Check for null IntPtr.
Commit migrated from https://github.com/dotnet/corefx/commit/dcc29528c9c4633e81b1937e7dd9fde7115a938c
* Fix null dereference in WinHttpResponseStream
If the response stream is disposed of while an operation like CopyToAsync is in flight, a subsequent cancellation request can trigger a null dereference in CancelPendingResponseStreamReadOperation. While this may not be desirable interaction, we should be more robust against it.
* Address PR feedback
Commit migrated from https://github.com/dotnet/corefx/commit/035b1e367c18db600c9d7158102fd2ab4e7a7ed2
* Implement client certificates for HttpClient on UAP
Added code for HttpClientHandler.ClientCertificates on UAP.
Moved code duplicated in several places into a common helper class, CertificateHelper.
Then I discovered a bug introduced into GetEligibleCertificates (from PR dotnet/corefx#21908) that
doesn't properly filter by EKU or KeyUsage if there is only one cert (this was failing
the WinHttpHandler Unit tests). So, I removed that optimization.
Used the new Azure test endpoint for client certificates. I did not use the loopback
server because there is a problem using it with HttpClient in UAP. There is a crash
in the SslStream.AuthenticateAsServer that I was not able to diagnose yet. It will
require a lot of time debugging and I wanted to get this HttpClientHandler feature
done now. Tracking that investigation with dotnet/corefx#22021.
Fixesdotnet/corefx#21628
Contributes to dotnet/corefx#14542
* Address PR feedback
* Moved method to Windows-only Common file
* Fixed some line spacing
Other potential refactoring/optimizations will be looked into in dotnet/corefx#22045
Commit migrated from https://github.com/dotnet/corefx/commit/472b8d8ec212c2afb107dea305b4624c4989a79f
* Fix HttpClientHandler ServerCertificateCustomValidationCallback for UWP
This PR implements the ServerCertificateCustomValidationCallback for UWP. While implementing this, I discovered that the behavior of
the current .NET Core code is incorrect regarding how to propagate exceptions from the user provided callback. I opened up a new
issue dotnet/corefx#21904 and revised the test as well.
Fixesdotnet/corefx#21627
* Address more PR feedback
Commit migrated from https://github.com/dotnet/corefx/commit/e30fdc38e60f6fa2ed8e7e103d1f8c44655438cd
* Using alternative Oid constructor
This alternative contructor will avoid trigging a potentially costly
lookup for the friendly name associated with the OID.
* Alternative way to lookup OID
This method avoids the AD lookup, while also not making any localization
assumptions
* fix typo
had the wrong OID for Server Auth
* Optional parameter on FromOidValue to disable AD search
New optional parameter on FromOidValue to disable lookup, update server
auth OID lookup in WinHttpCertificateHelper and SecureChannel to use
this new parameter when doing lookup
* Revert "Optional parameter on FromOidValue to disable AD search"
This reverts commit dotnet/corefx@6d0fd6df3d.
* Limit Server Auth OID lookup to local machine
Same change as what was made to SecureChannel class to avoid OID lookup
via AD
* Use alternative OID contructor
Use OID as friendly name to avoid lookup as well as localization issues
Commit migrated from https://github.com/dotnet/corefx/commit/81f80a8a0088d482afdb077b23f21a3e75a1c30c
The WinHTTP team has recommended that we always set the HTTP/2 options
explicitly when setting up the WinHTTP request handle. Future versions
of Windows might adjust the WinHTTP default value for this setting.
So, by always setting the value at the WinHTTP layer, we can ensure
that we have a stable default value at the .NET layer.
Commit migrated from https://github.com/dotnet/corefx/commit/d553cb46c0da91406e55b756160cfa87e2ee2f68
* Add nice messages for assemblies with autogenerated PNSE
* Fix failing builds
* Apply feedback
* Temporary workaround for Microsoft.Win32.Registry which needs using System statement
* Add new resource files
* Temporary workaround for System.Security.Cryptography.Cng which needs using System statement on Unix platforms
* Update BuildTools version and use resource strings for all PNSE messages
* Fix builds
* Fix typo
Commit migrated from https://github.com/dotnet/corefx/commit/00b2a853429c896eb973ffc1bbff04b186ab5d07
To help diagnose pernicious exceptions, this debug-build-only change augments exceptions created using CreateExceptionUsingError to include the calling thread's stack trace, so that when these exceptions are then moved to other threads and propagated there, we can better correlate where they came from.
Commit migrated from https://github.com/dotnet/corefx/commit/2c368e81d9d07f0d40edf36047e5b93e44db200b
WinHttpHandler was incorrectly sending two zero-length chunks (a violation of HTTP protocol)
when POST'ing empty data using chunked encoding. The second zero-length chunk was ok since it
means end of data. The first zero-length chunk was because it was the empty string being encoded.
This was causing problems by sending extra data to WinHTTP and hence to the server. Any
subsequent requests to the server might get mis-understood by the server and the server would then
send back an error. That translated to getting a WinHTTP error (12152)
"The server returned an invalid or unrecognized response".
This is hard to repro except in the HttpListener tests because of the timing of when the extra
zero-length chunk of request data gets sent to the server. There is already an HttpClient unit test
for POSTing zero-length data using chunked encoding. But this but was not triggering this error
due to timing issues. Even the HttpListener test needed to be adjusted to force a consistent repro.
Added asserts to validate the logic of making sure that we don't try to chunk encode zero length
data.
Fixesdotnet/corefx#18869.
Commit migrated from https://github.com/dotnet/corefx/commit/dad35ea3d495798dd04da90dec68230063f1331e
Fixed a test bug in PostAuthHelper which caused the wrong entity-body
semantics to get used during the test. This actually exposed a netfx
bug dealing with ambiguous semantics (Content-Length vs. chunked).
After fixing this test bug, it then exposed a product bug where we
leak an InvalidOperationException that isn't wrapped properly by an
HttpRequestException. The scenario that exposed this related to
POST'ing content that is non-rewindable and having to resubmit the
POST due to authentication challenges. This test was passing on
.NET Core because the test was written using the wrong assumptions.
It failed on .NET Framework which exposed the problem.
The InvalidOperationException comes from when a StreamContent's
SerializeToStreamAsync method is called and the content needs to be rewound.
If the content can't be rewound, then the exception is thrown with the
error message "The stream was already consumed. It cannot be read again."
Updated the WinHttpHandler and CurlHandler to wrap the InvalidOperationException
in an HttpRequestException. Updated the test accordingly.
Fixesdotnet/corefx#17621.
Commit migrated from https://github.com/dotnet/corefx/commit/e7e3c581870787d87393e674db22589ad56e944c
NotSupportedOnTargetFramework was used to explicitly make a package fail
to install (due to ref/lib guardrails check) where it would otherwise
succeed. This is no longer serving its purpose since NuGet removed the
guardrails check. It's also creating a less desireable failure mode:
FileNotFound vs PlatformNotSupported.
Commit migrated from https://github.com/dotnet/corefx/commit/e7533ab2c24728a5937bfb68609e183833359b27
Now instead of Use*Key a project can instead define the assembly key
in the project (genreally the common dir.pops for a project) using
the AssemblyKey property. The accepted values are Open, ECMA, MSFT,
Test.
This change allows for a repo to set the default key they want to use
for projects. In corefx we have switched the default to use the Open
key instead of the old BuildTools default of MSFT key.
As part of this update we are explicitly setting the AssemblyKey in
all the library projects (although it is really only necessary for
projects that aren't the default).
Also with this change we are updating all the new libraries that have
not yet shipped stable (compared to our 1.1 release) and making them
use the Open key. Which means that for prerelease dependencies there
might be some binary breaking changes to consume.
The following libraries ahven't shipped so there key is being
changed from MSFT to Open:
Microsoft.XmlSerializer.Generator
System.CodeDom
System.Configuration.ConfigurationManager
System.Data.Odbc
System.DirectoryServices
System.DirectoryServices.AccountManagement
System.DirectoryServices.Protocols
System.IO.Ports
System.Json
System.Memory
System.Net.HttpListener
System.Net.Mail
System.Net.ServicePoint
System.Net.WebClient
System.Net.WebProxy
System.Private.Xml
System.Private.Xml.Linq
System.Security.Cryptography.Xml
System.Security.Permissions
System.Transactions.Local
System.Web.HttpUtility
Commit migrated from https://github.com/dotnet/corefx/commit/ac09a797d09502ebab61f7167bb856846602e1e6
* Fix MaxResponseHeadersLengthSemantics
The new HttpClientHandler.MaxResponseHeadersLength property is defined differently from the similarly
named WebRequestHandler and HttpWebRequest properties. The units are supposed to be in Kilobytes
(1024 bytes). This problem was discovered while doing .NET Core vs. .NET Framework compat testing.
Fix the semantics to match the existing WebRequestHandler and HttpWebRequest properties. Adjust tests.
Contributes to dotnet/corefx#17691
* Address PR feedback
Commit migrated from https://github.com/dotnet/corefx/commit/170f7a5ac7f98f2076f0776c573e3188a390d565