Path: redb-route/redb.Route.Http.Hosting/TrustedProxyOptions.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Http.Hosting/TrustedProxyOptions.cs
----
using System.Net;
namespace redb.Route.Http;
///
/// The set of reverse proxies the shared Kestrel host trusts to speak for the client.
///
///
///
/// A proxy chain is a property of the process, not of a module or a route: two routes on one
/// port never sit behind different proxies. So the list lives on the host, next to TLS and the
/// protocol set, and every HTTP-based transport on that host (Http, Soap, As2, Grpc, the Tsak
/// management API) sees the resolved client address without doing anything itself.
///
///
/// Secure by default: with no entries the host ignores X-Forwarded-For and
/// X-Forwarded-Proto entirely and the socket peer is the client. Add exactly the addresses
/// or networks of the proxies you operate. A list that is too wide (a whole cloud range where an
/// attacker can rent a machine) hands that attacker the right to choose its own client address.
///
///
public sealed class TrustedProxyOptions
{
/// Individual addresses of trusted reverse proxies.
public List KnownProxies { get; } = new();
/// CIDR networks of trusted reverse proxies.
public List KnownNetworks { get; } = new();
///
/// Rewrite the request scheme from X-Forwarded-Proto when the peer is trusted. On by
/// default: behind a TLS-terminating proxy the URL a route sees (redbHttp.Url) must be
/// the one the client used, or anything that compares against it (DPoP htu, SCIM
/// Location, redirect URIs) compares against the wrong origin.
///
public bool ForwardScheme { get; set; } = true;
/// True when at least one proxy or network is listed.
public bool IsEnabled => KnownProxies.Count > 0 || KnownNetworks.Count > 0;
///
/// Adds a trusted proxy given as a bare address (10.0.0.5, ::1) or a CIDR
/// network (10.0.0.0/8). The form is decided by the presence of a slash.
///
/// The value is neither an address nor a CIDR network.
public TrustedProxyOptions Add(string addressOrNetwork)
{
ArgumentException.ThrowIfNullOrWhiteSpace(addressOrNetwork);
var value = addressOrNetwork.Trim();
if (value.Contains('/'))
{
if (!IPNetwork.TryParse(value, out var network))
throw new FormatException($"'{value}' is not a valid CIDR network.");
KnownNetworks.Add(network);
return this;
}
if (!IPAddress.TryParse(value, out var address))
throw new FormatException($"'{value}' is not a valid IP address.");
KnownProxies.Add(Normalize(address));
return this;
}
///
/// Whether belongs to a trusted proxy. IPv4-mapped IPv6 addresses
/// (::ffff:10.0.0.5, which is how a dual-stack socket reports an IPv4 peer) compare as
/// their IPv4 form, so a list written in IPv4 matches either way.
///
public bool IsTrusted(IPAddress address)
{
ArgumentNullException.ThrowIfNull(address);
var ip = Normalize(address);
foreach (var known in KnownProxies)
{
if (known.Equals(ip)) return true;
}
foreach (var network in KnownNetworks)
{
if (network.Contains(ip)) return true;
}
return false;
}
///
/// Folds an IPv4-mapped IPv6 address to its IPv4 form; any other address is returned as is.
/// Public so a caller building the list from values, rather than
/// through , can store them in the same form compares.
///
public static IPAddress Normalize(IPAddress address)
=> address.IsIPv4MappedToIPv6 ? address.MapToIPv4() : address;
}