Class ForwardedHeaderResolver

Namespace: redb.Route.Http
Assembly: redb.Route.Http.Hosting.dll

Turns X-Forwarded-For and X-Forwarded-Proto into a client address and scheme,
given the socket peer and the set of proxies the host trusts. Pure: no HttpContext, no
I/O, so it is unit-testable on strings and survives a change of HTTP engine unchanged.

public static class ForwardedHeaderResolver

Inheritance

Object→ForwardedHeaderResolver

Methods

Resolve(IPAddress?, string?, string?, TrustedProxyOptions)

Resolves the client address and scheme for one request.

public static ForwardedResolution Resolve(IPAddress? socketPeer, string? forwardedFor, string? forwardedProto, TrustedProxyOptions trust)

Remarks

Every proxy in a chain appends the address it accepted the connection from, so a client's own
entries always sit to the left of the entry the first trusted proxy wrote. The chain is
therefore walked from the right: trusted entries are skipped, and the first entry that is not
a trusted proxy is the client. A header from a peer that is not itself trusted is ignored
outright, because nothing in it was written by anyone we trust.


An entry that does not parse as an address stops the walk and the socket peer is kept. It
must not be skipped: skipping would carry the walk past the last trusted hop into the part of
the header the client controls.


X-Forwarded-Proto is read in step with the address: with k trusted hops skipped,
the scheme is the entry k positions from the right, which is the scheme observed by the
proxy that talked to the client. A single value is used as is.


Not handled, deliberately: the RFC 7239 Forwarded header, and X-Forwarded-Host.
Rewriting the host changes what redirects and absolute URLs point at and needs an allow-list
of its own; it is a separate decision.