Skip to content

Harden KnownProxies/KnownNetworks for non-IP transports - #67817

Merged
cincuranet merged 7 commits into
dotnet:mainfrom
cincuranet:fix-sock-knownprox
Sep 3, 2026
Merged

cincuranet merged 7 commits into
dotnet:mainfrom
cincuranet:fix-sock-knownprox

Conversation

@cincuranet

Copy link
Copy Markdown
Contributor

Fixes #67816

@cincuranet
cincuranet marked this pull request as ready for review July 17, 2026 08:16
Copilot AI review requested due to automatic review settings July 17, 2026 08:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens ForwardedHeadersMiddleware so that when KnownProxies/KnownNetworks (trusted-proxy enforcement) is configured, forwarded headers are not applied if the request arrives over a transport that does not provide a peer IP (e.g., Unix domain sockets / named pipes). This closes a security gap where enforcement could be bypassed when RemoteIpAddress is unavailable.

Changes:

  • Update ForwardedHeadersMiddleware.ApplyForwarders to fail closed (stop processing) when trusted-proxy enforcement is enabled but the immediate peer IP is not available.
  • Add new tests covering fail-closed behavior for KnownProxies and KnownIPNetworks when RemoteIpAddress is null.
  • Add a test intended to preserve behavior when enforcement is explicitly disabled (but it currently needs a small fix to actually disable the defaults).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs Fail-closed behavior when enforcement is enabled but no remote peer IP is available.
src/Middleware/HttpOverrides/test/ForwardedHeadersMiddlewareTest.cs New coverage for non-IP transports; one new test needs to explicitly clear default KnownProxies to match its intent.

@cincuranet
cincuranet marked this pull request as draft July 31, 2026 12:28
@cincuranet
cincuranet marked this pull request as ready for review July 31, 2026 17:36
@javiercn
javiercn requested a review from halter73 August 7, 2026 22:36
@javiercn

javiercn commented Aug 7, 2026

Copy link
Copy Markdown
Member

@halter73 and @BrennanConroy please take a look. This needs to go through API review.

@BrennanConroy BrennanConroy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

General idea looks good, should this be true by default? Also, needs to go through API review.

@cincuranet

Copy link
Copy Markdown
Contributor Author

It could be true by default. But then it's a behavioral breaking change. Apps where the server doesn't surface a peer IP - Unix domain sockets, named pipes, and any custom/in-memory server - stop having X-Forwarded-* applied by default. Reverse-proxy metadata silently stops flowing.

And bunch of our tests would have to set Connection.RemoteIpAddress, but that's easy to fix, i.e. IPAddress.IPv6Loopback would be enough (matches default KnownProxies).

@javiercn

Copy link
Copy Markdown
Member

@BrennanConroy @halter73 I sent the API review request for this thing. Take a look and if you are ok, we can get this merged

@halter73 halter73 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The implementation looks good, but this is a behavioral breaking change for non-IP transports using the default options. Given the timing, I think we should target .NET 12 so it gets a full preview cycle rather than land it late in .NET 11. We should also add a breaking-change announcement in dotnet/AspNetCore.Docs, covering the default behavior, clearing both allowlists for trusted non-IP-only deployments, and branching the middleware for mixed transports. If we believe the security impact warrants shipping sooner or backporting, that should be an explicit decision with the same announcement in place.

@cincuranet

Copy link
Copy Markdown
Contributor Author

@halter73 Definitely 12.

@Youssef1313 Youssef1313 added the area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares label Sep 2, 2026
@cincuranet

Copy link
Copy Markdown
Contributor Author

/ba-g Failures are unrelated.

@cincuranet
cincuranet merged commit 6097c38 into dotnet:main Sep 3, 2026
34 of 36 checks passed
@cincuranet
cincuranet deleted the fix-sock-knownprox branch September 3, 2026 11:40
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 4, 2026
@cincuranet

Copy link
Copy Markdown
Contributor Author

Documented in dotnet/AspNetCore.Docs#38164.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harden KnownProxies/KnownNetworks for non-IP transports

6 participants