Skip to content

Add AppContext switch in patch release to opt-out of breaking behavior change in ForwardedHeaders middleware #62690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eng/PatchConfig.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Later on, this will be checked using this condition:
</PropertyGroup>
<PropertyGroup Condition=" '$(VersionPrefix)' == '2.3.5' ">
<PackagesInPatch>
Microsoft.AspNetCore.HttpOverrides;
</PackagesInPatch>
</PropertyGroup>
</Project>
28 changes: 23 additions & 5 deletions src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ForwardedHeadersMiddleware
private readonly ForwardedHeadersOptions _options;
private readonly RequestDelegate _next;
private readonly ILogger _logger;
private readonly bool _ignoreUnknownProxiesWithoutFor;
private bool _allowAllHosts;
private IList<StringSegment> _allowedHosts;

Expand Down Expand Up @@ -90,6 +91,18 @@ public ForwardedHeadersMiddleware(RequestDelegate next, ILoggerFactory loggerFac
_logger = loggerFactory.CreateLogger<ForwardedHeadersMiddleware>();
_next = next;

if (AppContext.TryGetSwitch("Microsoft.AspNetCore.HttpOverrides.IgnoreUnknownProxiesWithoutFor", out var enabled)
&& enabled)
{
_ignoreUnknownProxiesWithoutFor = true;
}

if (Environment.GetEnvironmentVariable("MICROSOFT_ASPNETCORE_HTTPOVERRIDES_IGNORE_UNKNOWN_PROXIES_WITHOUT_FOR") is string env
&& (env.Equals("true", StringComparison.OrdinalIgnoreCase) || env.Equals("1")))
{
_ignoreUnknownProxiesWithoutFor = true;
}

PreProcessHosts();
}

Expand Down Expand Up @@ -228,12 +241,17 @@ public void ApplyForwarders(HttpContext context)
{
var set = sets[entriesConsumed];

// For the first instance, allow remoteIp to be null for servers that don't support it natively.
if (currentValues.RemoteIpAndPort != null && checkKnownIps && !CheckKnownAddress(currentValues.RemoteIpAndPort.Address))
// Opt-out of breaking change behavior where we now always check KnownProxies and KnownNetworks
// It used to be guarded by the ForwardedHeaders.XForwardedFor flag, but now we always check it.
if (!_ignoreUnknownProxiesWithoutFor || checkFor)
{
// Stop at the first unknown remote IP, but still apply changes processed so far.
_logger.LogDebug(1, $"Unknown proxy: {currentValues.RemoteIpAndPort}");
break;
// For the first instance, allow remoteIp to be null for servers that don't support it natively.
if (currentValues.RemoteIpAndPort != null && checkKnownIps && !CheckKnownAddress(currentValues.RemoteIpAndPort.Address))
{
// Stop at the first unknown remote IP, but still apply changes processed so far.
_logger.LogWarning(1, $"Unknown proxy: {currentValues.RemoteIpAndPort}");
break;
}
}

if (checkFor)
Expand Down
Loading