In ASP.NET Core 10, which senders does the forwarded headers middleware trust by default, and how do you trust a load balancer subnet without KnownNetworks?
answer
- trust is checked before each hop
- KnownProxies starts as ::1
- KnownIPNetworks starts as 127.0.0.0/8
- 8.0.17 and 9.0.6 hardening
- ASPDEPR005 and System.Net.IPNetwork
basics
~20 sOnly loopback senders: KnownProxies holds ::1 and KnownIPNetworks holds 127.0.0.0/8, so headers from a balancer elsewhere are ignored. Add its address to KnownProxies or its subnet, as a System.Net.IPNetwork, to KnownIPNetworks; KnownNetworks is obsolete in 10 (ASPDEPR005).
solid answer
~40 sBefore the middleware applies any `X-Forwarded-*` value it checks the address the request came from against `ForwardedHeadersOptions.KnownProxies` (exact `IPAddress` values, default `::1`) and `KnownIPNetworks` (CIDR ranges, default `127.0.0.0/8`). If the TCP peer is unknown, processing stops before anything is applied, and a `Debug` line logs `Unknown proxy`. Since 8.0.17 and 9.0.6 that check covers every forwarded header; earlier, an app that did not enable `X-Forwarded-For` accepted `X-Forwarded-Proto`, `-Host` and `-Prefix` from anyone. For a balancer pool or a container network, add the subnet: `options.KnownIPNetworks.Add(System.Net.IPNetwork.Parse("10.0.0.0/16"))`. In ASP.NET Core 10 the older `KnownNetworks` property and `Microsoft.AspNetCore.HttpOverrides.IPNetwork` type are obsolete (warning `ASPDEPR005`); both properties share one list. Clearing both lists trusts every sender, which is only safe when nothing but the proxy can reach the app.
code
csharp · 9 lines// Before (ASP.NET Core 8/9 style) - compiles in 10 with warning ASPDEPR005:
// options.KnownNetworks.Add(new Microsoft.AspNetCore.HttpOverrides.IPNetwork(IPAddress.Parse("10.0.0.0"), 16));
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownIPNetworks.Add(System.Net.IPNetwork.Parse("10.0.0.0/16"));
options.KnownProxies.Add(IPAddress.Parse("192.168.50.4")); // a single fixed proxy
});go deeper
Remember that forwarded headers are only believed from a known proxy, and that the defaults trust loopback only.
Explain KnownProxies versus KnownIPNetworks, their loopback defaults, what an unknown sender causes, and the switch from KnownNetworks to KnownIPNetworks in 10.
Use the Unknown proxy debug log to find the real peer address, choose a subnet for pools and containers, and recognise the 8.0.17 hardening behind a sudden redirect loop.
Decide where the trust boundary sits in each environment and whether a proxy-only network makes clearing the lists acceptable, and document it with the topology.
## Why the middleware needs a trust list `X-Forwarded-For`, `X-Forwarded-Proto`, `X-Forwarded-Host` and `X-Forwarded-Prefix` are ordinary request headers. Any client can type them into `curl`. If the forwarded headers middleware believed them from anyone, a client could choose its own IP address (defeating per-client limits and audit logs), claim `https` on a plain request, or inject a host name into generated links. So ASP.NET Core only applies them when the **hop that delivered them** is a proxy you declared. ## The two lists and their defaults (ASP.NET Core 10) | Property | Holds | Default contents | |---|---|---| | `KnownProxies` | exact `System.Net.IPAddress` values | `IPAddress.IPv6Loopback` (`::1`) | | `KnownIPNetworks` | `System.Net.IPNetwork` CIDR ranges | `127.0.0.0/8` | Both defaults are **loopback**: a proxy running on the same machine, such as Nginx on the same VM, is trusted out of the box, and nothing else is. A cloud load balancer, a Kubernetes ingress pod or a Docker bridge gateway has a non-loopback address, so its headers are ignored until you add it. ## How the check runs 1. The middleware starts from `Connection.RemoteIpAddress`, the real TCP peer. 2. If either list has entries and that address matches neither, it **stops**: no forwarded value is applied, and it logs `Unknown proxy:` with the address at `Debug` level. 3. Otherwise it applies the rightmost forwarded entry, which becomes the new current address, and repeats for the next hop while `ForwardLimit` allows. 4. In 10.0.12 an IPv4-mapped IPv6 peer (`::ffff:10.0.0.10`, common with dual-mode sockets) is also compared as plain IPv4, so adding `10.0.0.10` is enough. If **both lists are empty**, the check is skipped and every sender is trusted. ## What changed in 8.0.17 / 9.0.6 Those servicing releases hardened the middleware: headers from unknown proxies are ignored for **every** forwarded header. Previously, when `XForwardedFor` was not among the enabled flags, `X-Forwarded-Proto`, `-Host` and `-Prefix` were applied from any source. Apps that had relied on that (for example by enabling only `XForwardedProto` behind a cloud balancer) started seeing `http` again after patching, with symptoms such as HTTPS redirect loops or failing sign-in. The fix is to declare the balancer, not to roll back. ## What changed in ASP.NET Core 10 - `ForwardedHeadersOptions.KnownNetworks` and the `Microsoft.AspNetCore.HttpOverrides.IPNetwork` type are marked `[Obsolete]` with diagnostic **`ASPDEPR005`**. - The replacement is **`KnownIPNetworks`**, typed `IList<System.Net.IPNetwork>`, the BCL type that has had `Parse` and `TryParse` for CIDR strings since .NET 8. - Both properties are views over one list in 10, so old code still works while you migrate; clearing one clears the other. - With `using Microsoft.AspNetCore.HttpOverrides;` and `using System.Net;` both in scope, the name `IPNetwork` is ambiguous, so write `System.Net.IPNetwork` in full. ```csharp builder.Services.Configure<ForwardedHeadersOptions>(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; options.KnownIPNetworks.Add(System.Net.IPNetwork.Parse("10.0.0.0/16")); // balancer subnet }); ``` ## Choosing what to trust - **One fixed proxy**: put its address in `KnownProxies`. - **A pool or a container network**: put the subnet in `KnownIPNetworks`; individual addresses change on every redeploy. - **Proxy-only network** (the app has no other route in): clearing both lists is acceptable, and it is exactly what the `ASPNETCORE_FORWARDEDHEADERS_ENABLED` preset does. - **Reachable directly as well**: never clear the lists; anyone could forge the headers. When nothing seems to apply, the `Unknown proxy` debug line names the exact address and port to add. Checking `RemoteIpAddress` once without the middleware also shows how the proxy really appears to Kestrel.
- After patching to 8.0.17, an app that enabled only XForwardedProto behind a cloud balancer started redirecting in a loop. Why?Before that patch, with `XForwardedFor` not enabled, `X-Forwarded-Proto` was applied from any sender. The hardening applies the `KnownProxies` / `KnownIPNetworks` check to every forwarded header, and the balancer was never declared, so the scheme stayed `http` and HTTPS redirection fired on every request. Add the balancer's address or subnet.
- What happens if you clear both KnownProxies and KnownIPNetworks?The middleware skips the trust check and applies forwarded headers from any sender, up to `ForwardLimit` entries. That is acceptable only when the app is reachable solely through the proxy, for example on a private network. If clients can reach Kestrel directly, they can set their own `X-Forwarded-For` and `X-Forwarded-Proto`.
- By default the middleware trusts any sender on a private address range.
- KnownProxies contains 127.0.0.1 and ::1 by default.
- KnownNetworks with HttpOverrides.IPNetwork is still the current API in ASP.NET Core 10.
- Only X-Forwarded-For is checked against KnownProxies; Proto and Host are always applied.
- AllowedHosts is where you list the proxies allowed to send forwarded headers.
Assumes ASP.NET Core 10: KnownNetworks and HttpOverrides.IPNetwork are obsolete (ASPDEPR005) in favour of KnownIPNetworks and System.Net.IPNetwork; the unknown-proxy hardening shipped in 8.0.17 and 9.0.6.