// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Shared; using AspNetIPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork; using IPAddress = System.Net.IPAddress; using IPNetwork = System.Net.IPNetwork; namespace Microsoft.AspNetCore.Builder; /// /// Options for /// public class ForwardedHeadersOptions { // Backing dual list that keeps the obsolete and new types in sync. // Once the obsolete IList property is removed this can be changed to a simple List. private readonly DualIPNetworkList _knownNetworks = new(); /// /// Gets or sets the header used to retrieve the originating client IP. Defaults to the value specified by /// . /// public string ForwardedForHeaderName { get; set; } = ForwardedHeadersDefaults.XForwardedForHeaderName; /// /// Gets or sets the header used to retrieve the original value of the Host header field. /// Defaults to the value specified by /// public string ForwardedHostHeaderName { get; set; } = ForwardedHeadersDefaults.XForwardedHostHeaderName; /// /// Gets or sets the header used to retrieve the value for the originating scheme (HTTP/HTTPS). /// Defaults to the value specified by /// public string ForwardedProtoHeaderName { get; set; } = ForwardedHeadersDefaults.XForwardedProtoHeaderName; /// /// Gets or sets the header used to retrieve the value for the path base. /// Defaults to the value specified by /// public string ForwardedPrefixHeaderName { get; set; } = ForwardedHeadersDefaults.XForwardedPrefixHeaderName; /// /// Gets or sets the header used to store the original value of client IP before applying forwarded headers. /// Defaults to the value specified by /// /// public string OriginalForHeaderName { get; set; } = ForwardedHeadersDefaults.XOriginalForHeaderName; /// /// Gets or sets the header used to store the original value of the Host header field before applying forwarded headers. /// Defaults to the value specified by /// /// public string OriginalHostHeaderName { get; set; } = ForwardedHeadersDefaults.XOriginalHostHeaderName; /// /// Gets or sets the header used to store the original scheme (HTTP/HTTPS) before applying forwarded headers. /// Defaults to the value specified by /// /// public string OriginalProtoHeaderName { get; set; } = ForwardedHeadersDefaults.XOriginalProtoHeaderName; /// /// Gets or sets the header used to store the original path base before applying forwarded headers. /// Defaults to the value specified by /// /// public string OriginalPrefixHeaderName { get; set; } = ForwardedHeadersDefaults.XOriginalPrefixHeaderName; /// /// Identifies which forwarders should be processed. /// public ForwardedHeaders ForwardedHeaders { get; set; } /// /// Limits the number of entries in the headers that will be processed. The default value is 1. /// Set to null to disable the limit, but this should only be done if /// KnownProxies or KnownNetworks are configured. /// public int? ForwardLimit { get; set; } = 1; /// /// Addresses of known proxies to accept forwarded headers from. /// public IList KnownProxies { get; } = new List() { IPAddress.IPv6Loopback }; /// /// Address ranges of known proxies to accept forwarded headers from. /// Obsolete, please use instead /// [Obsolete("Please use KnownIPNetworks instead. For more information, visit https://aka.ms/aspnet/deprecate/005.", DiagnosticId = "ASPDEPR005", UrlFormat = Obsoletions.AspNetCoreDeprecate005Url)] public IList KnownNetworks => _knownNetworks; /// /// Address ranges of known proxies to accept forwarded headers from. /// public IList KnownIPNetworks => _knownNetworks; /// /// The allowed values from x-forwarded-host. If the list is empty then all hosts are allowed. /// Failing to restrict this these values may allow an attacker to spoof links generated by your service. /// /// /// /// Port numbers must be excluded. /// A top level wildcard "*" allows all non-empty hosts. /// Subdomain wildcards are permitted. E.g. "*.example.com" matches subdomains like foo.example.com, /// but not the parent domain example.com. /// Unicode host names are allowed but will be converted to punycode for matching. /// IPv6 addresses must include their bounding brackets and be in their normalized form. /// /// public IList AllowedHosts { get; set; } = new List(); /// /// Require the number of header values to be in sync between the different headers being processed. /// The default is 'false'. /// public bool RequireHeaderSymmetry { get; set; } }