@@ -24,6 +24,7 @@ public class ForwardedHeadersMiddleware
24
24
private readonly ForwardedHeadersOptions _options ;
25
25
private readonly RequestDelegate _next ;
26
26
private readonly ILogger _logger ;
27
+ private readonly bool _ignoreUnknownProxiesWithoutFor ;
27
28
private bool _allowAllHosts ;
28
29
private IList < StringSegment > _allowedHosts ;
29
30
@@ -90,6 +91,18 @@ public ForwardedHeadersMiddleware(RequestDelegate next, ILoggerFactory loggerFac
90
91
_logger = loggerFactory . CreateLogger < ForwardedHeadersMiddleware > ( ) ;
91
92
_next = next ;
92
93
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
+
93
106
PreProcessHosts ( ) ;
94
107
}
95
108
@@ -228,12 +241,17 @@ public void ApplyForwarders(HttpContext context)
228
241
{
229
242
var set = sets [ entriesConsumed ] ;
230
243
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 )
233
247
{
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
+ }
237
255
}
238
256
239
257
if ( checkFor )
0 commit comments