Skip to content

Commit 52bcba6

Browse files
Add AppContext switch in patch release to opt-out of breaking behavior change in ForwardedHeaders middleware (#62690)
* Add AppContext switch in patch release to opt-out of breaking behavior change in ForwardedHeaders middleware * config --------- Co-authored-by: William Godbe <wigodbe@microsoft.com>
1 parent 8bdea94 commit 52bcba6

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

eng/PatchConfig.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Later on, this will be checked using this condition:
3939
</PropertyGroup>
4040
<PropertyGroup Condition=" '$(VersionPrefix)' == '2.3.5' ">
4141
<PackagesInPatch>
42+
Microsoft.AspNetCore.HttpOverrides;
4243
</PackagesInPatch>
4344
</PropertyGroup>
4445
</Project>

src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class ForwardedHeadersMiddleware
2424
private readonly ForwardedHeadersOptions _options;
2525
private readonly RequestDelegate _next;
2626
private readonly ILogger _logger;
27+
private readonly bool _ignoreUnknownProxiesWithoutFor;
2728
private bool _allowAllHosts;
2829
private IList<StringSegment> _allowedHosts;
2930

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

94+
if (AppContext.TryGetSwitch("Microsoft.AspNetCore.HttpOverrides.IgnoreUnknownProxiesWithoutFor", out var enabled)
95+
&& enabled)
96+
{
97+
_ignoreUnknownProxiesWithoutFor = true;
98+
}
99+
100+
if (Environment.GetEnvironmentVariable("MICROSOFT_ASPNETCORE_HTTPOVERRIDES_IGNORE_UNKNOWN_PROXIES_WITHOUT_FOR") is string env
101+
&& (env.Equals("true", StringComparison.OrdinalIgnoreCase) || env.Equals("1")))
102+
{
103+
_ignoreUnknownProxiesWithoutFor = true;
104+
}
105+
93106
PreProcessHosts();
94107
}
95108

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

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

239257
if (checkFor)

0 commit comments

Comments
 (0)