Skip to content
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

Fix Kestrel host header mismatch handling when port in Url #59352

Merged
merged 2 commits into from
Dec 9, 2024

Conversation

BrennanConroy
Copy link
Member

When using the KestrelServerOptions.AllowHostHeaderOverride option, if a port was included in the request URL and the Host header didn't match, the request would still fail with a 400 Bad Request due to us double including the port in the computed Host string.

Fails with Invalid Host header: 'www.foo.com:1234:1234'

GET https://localhost:1234 HTTP/1.1
Host: localhost

Worked

GET https://localhost HTTP/1.1
Host: localhost:1234

Since Uri.Authority doesn't include the default port (80 for http, 443 for https), we should only append the port in the computed host header if it's not the default port for the scheme.

@BrennanConroy BrennanConroy added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Dec 6, 2024
@@ -649,11 +648,16 @@ private void ValidateNonOriginHostHeader(string hostText)
if (hostText != _absoluteRequestTarget!.Authority)
{
if (!_absoluteRequestTarget.IsDefaultPort
|| hostText != _absoluteRequestTarget.Authority + ":" + _absoluteRequestTarget.Port.ToString(CultureInfo.InvariantCulture))
|| hostText != $"{_absoluteRequestTarget.Authority}:{_absoluteRequestTarget.Port}")
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to the bug, but this temporary string allocation seems like it could be optimized away.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

We could do it ourselves. e.g. check that content before : matches _absoluteRequestTarget.Authority and content after : parses to an int and matches _absoluteRequestTarget.Port.

But if this path isn't that hot then probably not worth the complexity.

Copy link
Member

Choose a reason for hiding this comment

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

Does it run per request ?

Copy link
Member Author

@BrennanConroy BrennanConroy Dec 6, 2024

Choose a reason for hiding this comment

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

If the host doesn't match the authority.

@adityamandaleeka
Copy link
Member

LGTM. I assume this will go to 9 and 8?

@BrennanConroy
Copy link
Member Author

/backport to release/9.0

Copy link
Contributor

github-actions bot commented Dec 7, 2024

Started backporting to release/9.0: https://github.com/dotnet/aspnetcore/actions/runs/12210325288

@BrennanConroy
Copy link
Member Author

/backport to release/8.0

Copy link
Contributor

github-actions bot commented Dec 7, 2024

Started backporting to release/8.0: https://github.com/dotnet/aspnetcore/actions/runs/12210328859

Copy link
Contributor

github-actions bot commented Dec 7, 2024

@BrennanConroy backporting to release/8.0 failed, the patch most likely resulted in conflicts:

$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix Kestrel host header mismatch handling when port in Url
Using index info to reconstruct a base tree...
M	src/Servers/Kestrel/Core/src/Internal/Http/Http1Connection.cs
M	src/Servers/Kestrel/shared/test/HttpParsingData.cs
M	src/Servers/Kestrel/test/InMemory.FunctionalTests/BadHttpRequestTests.cs
Falling back to patching base and 3-way merge...
Auto-merging src/Servers/Kestrel/test/InMemory.FunctionalTests/BadHttpRequestTests.cs
CONFLICT (content): Merge conflict in src/Servers/Kestrel/test/InMemory.FunctionalTests/BadHttpRequestTests.cs
Auto-merging src/Servers/Kestrel/shared/test/HttpParsingData.cs
Auto-merging src/Servers/Kestrel/Core/src/Internal/Http/Http1Connection.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config advice.mergeConflict false"
Patch failed at 0001 Fix Kestrel host header mismatch handling when port in Url
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

@BrennanConroy BrennanConroy merged commit 4655dc9 into main Dec 9, 2024
27 checks passed
@BrennanConroy BrennanConroy deleted the brecon/hostmismatch branch December 9, 2024 21:44
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview1 milestone Dec 9, 2024
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.

5 participants