-
Notifications
You must be signed in to change notification settings - Fork 838
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
E2E tests for different header encodings #760
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f3a11b5
Latin1 and UTF8 header value e2e tests
ManickaP 0c977e2
Response test
ManickaP 3d1760e
Addressed styling issues.
ManickaP 1530881
Configuration for request header encoding.
ManickaP 816f107
Response headers error handling and added tests for the new option.
ManickaP 256f711
Docs.
ManickaP 7cb6e49
Merge branch 'main' into mapichov/154_utf8_headers
ManickaP 219b9f8
Updated docs.
ManickaP 9916bf9
Addressed PR feedback.
ManickaP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,8 @@ HTTP client configuration is based on [ProxyHttpClientOptions](xref:Microsoft.Re | |
"Store": "<string>", | ||
"Location": "<string>", | ||
"AllowInvalid": "<bool>" | ||
} | ||
}, | ||
"RequestHeaderEncoding": "<encoding-name>" | ||
} | ||
``` | ||
|
||
|
@@ -75,6 +76,32 @@ Configuration settings: | |
} | ||
|
||
``` | ||
- RequestHeaderEncoding - enables other than ASCII encoding for outgoing request headers. Setting this value will leverage [`SocketsHttpHandler.RequestHeaderEncodingSelector`](https://docs.microsoft.com/en-us/dotnet/api/system.net.http.socketshttphandler.requestheaderencodingselector?view=net-5.0) and use the selected encoding for all headers. If you need more granular approach, please use custom `IProxyHttpClientFactory`. The value is then parsed by [`Encoding.GetEncoding`](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding.getencoding?view=net-5.0#System_Text_Encoding_GetEncoding_System_String_), use values like: "utf-8", "iso-8859-1", etc. **This setting is only available for .NET 5.0.** | ||
```JSON | ||
"RequestHeaderEncoding": "utf-8" | ||
``` | ||
If you're using an encoding other than "ascii" (or "utf-8" for Kestrel) you also need to set your server to accept requests with such headers. For example, use [`KestrelServerOptions.RequestHeaderEncodingSelector`](https://docs.microsoft.com/en-us/dotnet/api/Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions.RequestHeaderEncodingSelector?view=aspnetcore-5.0) to set up Kestrel to accept Latin1 ("iso-8859-1") headers: | ||
```C# | ||
private static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}) | ||
.ConfigureWebHost(webBuilder => webBuilder.UseKestrel(kestrel => | ||
{ | ||
kestrel.RequestHeaderEncodingSelector = _ => Encoding.Latin1; | ||
})); | ||
``` | ||
|
||
For .NET Core 3.1, Latin1 ("iso-8859-1") is the only non-ASCII header encoding that can be accepted and only via application wide switch: | ||
```C# | ||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.AllowLatin1Headers", true); | ||
Tratcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AppContext.SetSwitch("Microsoft.AspNetCore.Server.Kestrel.Latin1RequestHeaders", true); | ||
``` | ||
|
||
At the moment, there is no solution for changing encoding for response headers, only ASCII is accepted. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... in Kestrel. Link to dotnet/aspnetcore#26334. |
||
|
||
|
||
### HttpRequest | ||
HTTP request configuration is based on [ProxyHttpRequestOptions](xref:Microsoft.ReverseProxy.Abstractions.ProxyHttpRequestOptions) and represented by the following configuration schema. | ||
|
@@ -256,6 +283,12 @@ public class CustomProxyHttpClientFactory : IProxyHttpClientFactory | |
{ | ||
handler.SslOptions.RemoteCertificateValidationCallback = (sender, cert, chain, errors) => cert.Subject == "dev.mydomain"; | ||
} | ||
#if NET | ||
if (newClientOptions.RequestHeaderEncoding != null) | ||
{ | ||
handler.RequestHeaderEncodingSelector = (_, _) => newClientOptions.RequestHeaderEncoding; | ||
} | ||
#endif | ||
|
||
return new HttpMessageInvoker(handler, disposeHandler: true); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,9 +172,20 @@ public async Task ProxyAsync( | |
throw new InvalidOperationException("Proxying the Client request body to the Destination server hasn't started. This is a coding defect."); | ||
} | ||
|
||
// :: Step 5: Copy response status line Client ◄-- Proxy ◄-- Destination | ||
// :: Step 6: Copy response headers Client ◄-- Proxy ◄-- Destination | ||
await CopyResponseStatusAndHeadersAsync(destinationResponse, context, transformer); | ||
try | ||
{ | ||
// :: Step 5: Copy response status line Client ◄-- Proxy ◄-- Destination | ||
// :: Step 6: Copy response headers Client ◄-- Proxy ◄-- Destination | ||
await CopyResponseStatusAndHeadersAsync(destinationResponse, context, transformer); | ||
} | ||
catch (Exception ex) | ||
{ | ||
ReportProxyError(context, ProxyError.ResponseHeaders, ex); | ||
// Clear the response since status code, reason and some headers might have already been copied and we want clean 502 response. | ||
context.Response.Clear(); | ||
context.Response.StatusCode = StatusCodes.Status502BadGateway; | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure to dispose the destinationResponse to avoid a leak. |
||
} | ||
|
||
// :: Step 7-A: Check for a 101 upgrade response, this takes care of WebSockets as well as any other upgradeable protocol. | ||
if (destinationResponse.StatusCode == HttpStatusCode.SwitchingProtocols) | ||
|
@@ -285,7 +296,7 @@ public async Task ProxyAsync( | |
// Allow someone to custom build the request uri, otherwise provide a default for them. | ||
var request = context.Request; | ||
destinationRequest.RequestUri ??= RequestUtilities.MakeDestinationAddress(destinationPrefix, request.Path, request.QueryString); | ||
|
||
Log.Proxying(_logger, destinationRequest.RequestUri); | ||
|
||
// TODO: What if they replace the HttpContent object? That would mess with our tracking and error handling. | ||
|
@@ -689,6 +700,7 @@ private static string GetMessage(ProxyError error) | |
ProxyError.ResponseBodyCanceled => "Copying the response body was canceled.", | ||
ProxyError.ResponseBodyClient => "The client reported an error when copying the response body.", | ||
ProxyError.ResponseBodyDestination => "The destination reported an error when copying the response body.", | ||
ProxyError.ResponseHeaders => "The destination returned a response that cannot be proxied back to the client.", | ||
ProxyError.UpgradeRequestCanceled => "Copying the upgraded request body was canceled.", | ||
ProxyError.UpgradeRequestClient => "The client reported an error when copying the upgraded request body.", | ||
ProxyError.UpgradeRequestDestination => "The destination reported an error when copying the upgraded request body.", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
ConfigureWebHost and ConfigureWebHostDefaults are redundant. Also call ConfigureKestrel rather than UseKestrel here, UseKestrel was already called by ConfigureWebHostDefaults.