skip to content

Behind a CDN and a load balancer, how do ASP.NET Core's ForwardLimit and trust lists pick the X-Forwarded-For entry that becomes RemoteIpAddress, and how can it be spoofed?

level: seniorimportance: should knowfreq 35%

answer

  1. the header is read right to left
  2. ForwardLimit defaults to 1
  3. each hop must be a known proxy
  4. null limit plus empty lists
  5. AllowedHosts and RequireHeaderSymmetry

basics

~20 s

The middleware reads X-Forwarded-For right to left, one entry per hop, while the current sender is trusted and ForwardLimit (default 1) allows. Two proxies need ForwardLimit 2 and both trusted; empty trust lists with no limit let a forged leftmost entry win.

solid answer

~40 s

Each proxy appends the address it received from, so the rightmost `X-Forwarded-For` entry is the most trustworthy. `ForwardedHeadersMiddleware` starts at the TCP peer and walks right to left: if the current address is in `KnownProxies` or `KnownIPNetworks`, it takes the next entry as the new client, until `ForwardLimit` entries are used or an untrusted address appears. With a CDN in front of the balancer and the default `ForwardLimit = 1`, `RemoteIpAddress` becomes the CDN edge, not the skier. The fix is `ForwardLimit = 2` plus the CDN's published ranges in `KnownIPNetworks`. A client can always prepend fake entries, but they are only reached if a hop that should stop the walk is trusted: clearing both lists and setting `ForwardLimit = null` makes the leftmost, client-written value win. `AllowedHosts` guards `X-Forwarded-Host` the same way.

code

http · 4 lines
http
GET /passes/day HTTP/1.1
Host: lifts.example.com
X-Forwarded-For: 192.0.2.66, 203.0.113.42, 198.51.100.7
X-Forwarded-Proto: https, https

go deeper

for a junior

Recall that X-Forwarded-For can hold several addresses, one per hop, and that clients can write the header themselves.

for a middle

Explain the right-to-left walk, the default ForwardLimit of 1 and why the CDN edge shows up as the client with two proxies.

for a senior

Configure a multi-proxy chain safely, show how cleared lists plus a null limit let a forged entry win, and guard X-Forwarded-Host with AllowedHosts.

for a principal

Own the edge contract: which hop is authoritative for client identity, how CDN ranges are kept current, and how changes to the chain are rolled out.

## How X-Forwarded-For grows Every proxy that follows the convention **appends** the address of the peer it received the request from. Take the lift-pass shop behind a CDN and a load balancer: 1. A skier at `203.0.113.42` connects to a CDN edge at `198.51.100.7`. 2. The edge appends the skier: `X-Forwarded-For: 203.0.113.42`. 3. The balancer at `10.0.0.10` appends the edge: `X-Forwarded-For: 203.0.113.42, 198.51.100.7`. 4. Kestrel's TCP peer is `10.0.0.10`. The **rightmost** entry was written by the proxy closest to you; the **leftmost** may have been written by anybody, because a client can send its own `X-Forwarded-For` and every proxy simply appends to it. ## How the middleware walks the chain `ForwardedHeadersMiddleware` processes entries **right to left**: - It starts with `Connection.RemoteIpAddress` as the current address. - For each step it checks the current address against `KnownProxies` and `KnownIPNetworks` (skipped only if both are empty). An unknown address stops the walk. - A trusted address lets it consume the next entry from the right, which becomes the new current address. - `ForwardLimit` (default **1**) caps how many entries are consumed. `null` removes the cap. - An entry that does not parse as an IP endpoint also stops the walk. Values consumed are removed from the header; whatever stays in `X-Forwarded-For` was not trusted. ## The two-proxy case | Settings | Result for `203.0.113.42, 198.51.100.7` from `10.0.0.10` | |---|---| | Balancer trusted, `ForwardLimit = 1` (default) | `RemoteIpAddress = 198.51.100.7`, the CDN edge | | Balancer trusted, `ForwardLimit = 2`, CDN not trusted | still the CDN edge: the walk stops at the unknown edge | | Balancer and CDN ranges trusted, `ForwardLimit = 2` | `203.0.113.42`, the skier | The limit and the trust list work together: the limit says how many hops you have, the lists say which addresses are hops. ## How spoofing happens A client sends `X-Forwarded-For: 192.0.2.66`. After the CDN and the balancer append, the app receives `192.0.2.66, 203.0.113.42, 198.51.100.7`. - With the correct settings above, the walk stops after two entries and the skier's real `203.0.113.42` wins; the forged value is left in the header. - With **both lists cleared and `ForwardLimit = null`**, every address counts as a proxy and there is no cap, so the walk reaches the leftmost entry and `RemoteIpAddress` becomes `192.0.2.66`, chosen by the attacker. - With the lists cleared and the app **reachable directly**, any client can send a single forged entry and the default limit of 1 applies it. The source documents the rule: set `ForwardLimit` to `null` only when `KnownProxies` or `KnownIPNetworks` are configured. ## The other forwarded values - **`X-Forwarded-Host`** rewrites `Request.Host`, which feeds absolute links. `ForwardedHeadersOptions.AllowedHosts` restricts the accepted values (an empty list allows all; `*.example.com` matches subdomains only). If a trusted proxy passes a client's own `X-Forwarded-Host` through, leaving it open lets that client steer password-reset links. - **`RequireHeaderSymmetry`** (default `false`) makes the middleware give up, with a warning, when the headers it processes carry different numbers of entries, which catches a proxy that appends to one header but not another. ## A configuration for the chain ```csharp builder.Services.Configure<ForwardedHeadersOptions>(o => { o.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; o.ForwardLimit = 2; o.KnownProxies.Add(IPAddress.Parse("10.0.0.10")); o.KnownIPNetworks.Add(System.Net.IPNetwork.Parse("198.51.100.0/24")); // CDN egress range }); ``` Keep the CDN ranges current; a stale list silently drops back to logging the edge as the client.

  • Why is ForwardLimit = null safe when the trust lists are configured?
    Because the walk still stops at the first address that is not a known proxy. A client's own address is normally not in `KnownProxies` or `KnownIPNetworks`, so anything the client wrote further left is never consumed. The limit is a backstop for misconfigured or side-channel traffic, not the primary defence; the trust lists are.
  • What does RequireHeaderSymmetry change, and when would you turn it on?
    With `RequireHeaderSymmetry = true`, the middleware compares the entry counts of the headers it processes, such as `X-Forwarded-For` and `X-Forwarded-Proto`; if they differ it logs a warning and applies nothing. Turn it on when every proxy in the chain appends to all of them, so a mismatch signals tampering or a misconfigured hop.
  • Why does an open AllowedHosts matter if X-Forwarded-Host is enabled?
    `X-Forwarded-Host` replaces `Request.Host`, which link generation uses for absolute URLs. If a trusted proxy passes a client-supplied value through, an attacker can make the app email a reset link pointing at their own domain. `ForwardedHeadersOptions.AllowedHosts` limits the values that may be applied.

  • The leftmost X-Forwarded-For entry is always the real client.
  • ForwardLimit = null is safe even with KnownProxies and KnownIPNetworks cleared.
  • Raising ForwardLimit alone is enough to see past a CDN.
  • RequireHeaderSymmetry is on by default in ASP.NET Core 10.
  • AllowedHosts on ForwardedHeadersOptions decides which proxies are trusted.