Skip to content

Forwarded Headers Middleware: Ignore XForwardedHeaders from Unknown Proxy #61530

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

Merged
merged 2 commits into from
Apr 22, 2025

Conversation

yannic-hamann-abb
Copy link
Contributor

@yannic-hamann-abb yannic-hamann-abb commented Apr 17, 2025

Forwarded Headers Middleware: Ignore XForwardedHeaders from Unknown Proxy

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Fixes a bug where, under some conditions, XForwardedPrefix , XForwardedProto and XForwardedHost headers could be tampered with.

Description

This PR makes sure that XForwarded-Headers are only interpreted when they come from a known proxy. As suggested by the documentation..

If the ForwardedHeaders.XForwardedFor flag in ForwardedHeadersOptions isn't set. The ForwardedHeadersMiddleware doesn't check if the request comes from a known proxy.

This means that with the following ForwardedHeadersOptions (or any other combination where ForwardedHeaders.XForwardedFor is missing):

var options = new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedHost | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedPrefix };
_application.UseForwardedHeaders(options);

the respective X-Forwarded-headers will be always processed by the middleware which have some (security related?) side effects:

  • XForwardedPrefix sets context.Request.PathBase
  • XForwardedProto sets context.Request.Scheme
  • XForwardedHost sets context.Request.Host

With ForwardedHeadersOptions set to ForwardedHeaders.All no side effects would have been executed.

Fixes #61449

This observation has been reported by me via the MSRC-Portal but was classified as a product bug. The following information may be related: aspnet/Announcements#295

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares label Apr 17, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Apr 17, 2025
@yannic-hamann-abb
Copy link
Contributor Author

@dotnet-policy-service agree

@BrennanConroy BrennanConroy merged commit 1cb6199 into dotnet:main Apr 22, 2025
26 checks passed
@BrennanConroy
Copy link
Member

/backport to release/9.0

@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview4 milestone Apr 22, 2025
Copy link
Contributor

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

@BrennanConroy
Copy link
Member

/backport to release/8.0

Copy link
Contributor

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

@BrennanConroy
Copy link
Member

/backport to release/2.3

Copy link
Contributor

Started backporting to release/2.3: https://github.com/dotnet/aspnetcore/actions/runs/14605871270

Copy link
Contributor

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

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

Applying: Header Spoofing Proof for XForwardedProto, XForwardedHost and XForwardedPrefix
Using index info to reconstruct a base tree...
M	src/Middleware/HttpOverrides/test/ForwardedHeadersMiddlewareTest.cs
Falling back to patching base and 3-way merge...
Auto-merging src/Middleware/HttpOverrides/test/ForwardedHeadersMiddlewareTest.cs
CONFLICT (content): Merge conflict in src/Middleware/HttpOverrides/test/ForwardedHeadersMiddlewareTest.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 set advice.mergeConflict false"
Patch failed at 0001 Header Spoofing Proof for XForwardedProto, XForwardedHost and XForwardedPrefix
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

@BrennanConroy
Copy link
Member

Thanks for the find and fix @yannic-hamann-abb !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Forwarded Headers Middleware: X-Forwarded-Prefix Applied when it comes from an Unknown Proxy
2 participants