Skip to content

Obsolete Microsoft.AspNetCore.HttpOverrides.IPNetwork #62490

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 28 commits into from
Jul 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8258d90
Update IMicrosoft.AspNetCore.HttpOverrides.PNetwork implementation
WeihanLi Jun 27, 2025
8bbc434
Update IPNetwork constructor for implicit convert
WeihanLi Jun 27, 2025
7372e0b
Obsolete IPNetwork.cs
WeihanLi Jun 27, 2025
1e44ff7
Update invalid comment tag
WeihanLi Jun 27, 2025
7e5db2a
fix IPNetwork.cs error
WeihanLi Jun 27, 2025
f71426d
use System.Net.IPNetwork for ForwardedHeadersOptions
WeihanLi Jun 27, 2025
7993c1f
update publicapi
WeihanLi Jun 27, 2025
996592b
Update IPNetwork.cs comment inherit error
WeihanLi Jun 27, 2025
2d015aa
include implicit convert into public api
WeihanLi Jun 27, 2025
6346519
fix IPNetwork.TryParse comment
WeihanLi Jun 27, 2025
21547f6
Merge branch 'dotnet:main' into patch-1
WeihanLi Jun 27, 2025
74be29f
fix comment and publish api error
WeihanLi Jun 28, 2025
61c9246
fix KnownNetwork PublicAPI
WeihanLi Jun 28, 2025
dfc068c
update PublicAPI
WeihanLi Jun 28, 2025
a0f13a0
remove IPNetwork implicit from PublicAPI.Shipped.txt
WeihanLi Jun 28, 2025
4f63a58
test: fix test cases
WeihanLi Jun 28, 2025
1c2b21f
refactor: remove the implicit convert for obsolete IPNetwork
WeihanLi Jul 11, 2025
b74969e
remove missed implicit convert
WeihanLi Jul 11, 2025
f637aec
revert unnecessay changes
WeihanLi Jul 11, 2025
d8c7cb3
test: update test cases
WeihanLi Jul 14, 2025
51dba56
Merge branch 'patch-1' of https://github.com/WeihanLi/aspnetcore into…
WeihanLi Jul 18, 2025
8795915
feat: add KnownIPNetworks
WeihanLi Jul 18, 2025
22748db
feat: update PublicAPI.Unshipped.txt
WeihanLi Jul 18, 2025
5f59285
feat: add back Clear for legacy IPNetwork
WeihanLi Jul 18, 2025
720d723
feat: revert Microsoft.AspNetCore.HttpOverrides.IPNetwork
WeihanLi Jul 18, 2025
f3a8519
mark Microsoft.AspNetCore.HttpOverrides.IPNetwork obsolete
WeihanLi Jul 18, 2025
3d4f91a
update obsolete message and diagnosticId
WeihanLi Jul 19, 2025
90840a1
update obsolete message for HttpOverride.IPNetwork
WeihanLi Jul 19, 2025
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
3 changes: 3 additions & 0 deletions src/DefaultBuilder/src/ForwardedHeadersOptionsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public void Configure(ForwardedHeadersOptions options)
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
// Only loopback proxies are allowed by default. Clear that restriction because forwarders are
// being enabled by explicit configuration.
#pragma warning disable ASPDEPR005 // KnownNetworks is obsolete
options.KnownNetworks.Clear();
#pragma warning restore ASPDEPR005 // KnownNetworks is obsolete
options.KnownIPNetworks.Clear();
options.KnownProxies.Clear();
}
}
15 changes: 14 additions & 1 deletion src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ public void ApplyForwarders(HttpContext context)
// Host and Scheme initial values are never inspected, no need to set them here.
};

var checkKnownIps = _options.KnownNetworks.Count > 0 || _options.KnownProxies.Count > 0;
var checkKnownIps = _options.KnownIPNetworks.Count > 0
#pragma warning disable ASPDEPR005 // KnownNetworks is obsolete
|| _options.KnownNetworks.Count > 0
#pragma warning restore ASPDEPR005 // KnownNetworks is obsolete
|| _options.KnownProxies.Count > 0;
bool applyChanges = false;
int entriesConsumed = 0;

Expand Down Expand Up @@ -399,13 +403,22 @@ private bool CheckKnownAddress(IPAddress address)
{
return true;
}
foreach (var network in _options.KnownIPNetworks)
{
if (network.Contains(address))
{
return true;
}
}
#pragma warning disable ASPDEPR005 // KnownNetworks is obsolete
foreach (var network in _options.KnownNetworks)
{
if (network.Contains(address))
{
return true;
}
}
#pragma warning restore ASPDEPR005 // KnownNetworks is obsolete
return false;
}

Expand Down
11 changes: 10 additions & 1 deletion src/Middleware/HttpOverrides/src/ForwardedHeadersOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.HttpOverrides;
using AspNetIPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
using IPAddress = System.Net.IPAddress;
using IPNetwork = System.Net.IPNetwork;

namespace Microsoft.AspNetCore.Builder;

Expand Down Expand Up @@ -82,8 +84,15 @@ public class ForwardedHeadersOptions

/// <summary>
/// Address ranges of known proxies to accept forwarded headers from.
/// Obsolete, please use <see cref="KnownIPNetworks"/> instead
/// </summary>
public IList<IPNetwork> KnownNetworks { get; } = new List<IPNetwork>() { new IPNetwork(IPAddress.Loopback, 8) };
[Obsolete("Please use KnownIPNetworks instead. For more information, visit https://aka.ms/aspnet/deprecate/005.", DiagnosticId = "ASPDEPR005")]
public IList<AspNetIPNetwork> KnownNetworks { get; } = new List<AspNetIPNetwork>() { new(IPAddress.Loopback, 8) };

/// <summary>
/// Address ranges of known proxies to accept forwarded headers from.
/// </summary>
public IList<IPNetwork> KnownIPNetworks { get; } = new List<IPNetwork>() { new(IPAddress.Loopback, 8) };

/// <summary>
/// The allowed values from x-forwarded-host. If the list is empty then all hosts are allowed.
Expand Down
2 changes: 2 additions & 0 deletions src/Middleware/HttpOverrides/src/IPNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace Microsoft.AspNetCore.HttpOverrides;

/// <summary>
/// A representation of an IP network based on CIDR notation.
/// Please use <see cref="System.Net.IPNetwork"/> instead
/// </summary>
[Obsolete("Please use System.Net.IPNetwork instead. For more information, visit https://aka.ms/aspnet/deprecate/005.", DiagnosticId = "ASPDEPR005")]
public class IPNetwork
{
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Middleware/HttpOverrides/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
Microsoft.AspNetCore.Builder.ForwardedHeadersOptions.KnownIPNetworks.get -> System.Collections.Generic.IList<System.Net.IPNetwork>!
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ public async Task XForwardedForForwardLimit(int limit, string header, string exp
ForwardLimit = limit,
};
options.KnownProxies.Clear();
#pragma warning disable ASPDEPR005 // KnownNetworks is obsolete
options.KnownNetworks.Clear();
#pragma warning restore ASPDEPR005 // KnownNetworks is obsolete
options.KnownIPNetworks.Clear();
app.UseForwardedHeaders(options);
});
}).Build();
Expand Down Expand Up @@ -861,7 +864,10 @@ public async Task XForwardedProtoOverrideLimitedByLoopback(string protoHeader, s
};
if (!loopback)
{
#pragma warning disable ASPDEPR005 // KnownNetworks is obsolete
options.KnownNetworks.Clear();
#pragma warning restore ASPDEPR005 // KnownNetworks is obsolete
options.KnownIPNetworks.Clear();
options.KnownProxies.Clear();
}
app.UseForwardedHeaders(options);
Expand All @@ -888,7 +894,7 @@ public void AllForwardsDisabledByDefault()
var options = new ForwardedHeadersOptions();
Assert.True(options.ForwardedHeaders == ForwardedHeaders.None);
Assert.Equal(1, options.ForwardLimit);
Assert.Single(options.KnownNetworks);
Assert.Single(options.KnownIPNetworks);
Assert.Single(options.KnownProxies);
}

Expand Down Expand Up @@ -1092,7 +1098,7 @@ public async Task XForwardForIPv4ToIPv6Mapping(string forHeader, string knownPro
var knownNetworkParts = knownNetwork.Split('/');
var networkIp = IPAddress.Parse(knownNetworkParts[0]);
var prefixLength = int.Parse(knownNetworkParts[1], CultureInfo.InvariantCulture);
options.KnownNetworks.Add(new IPNetwork(networkIp, prefixLength));
options.KnownIPNetworks.Add(new System.Net.IPNetwork(networkIp, prefixLength));
}

using var host = new HostBuilder()
Expand Down Expand Up @@ -1134,7 +1140,10 @@ public async Task ForwardersWithDIOptionsRunsOnce(int limit, string header, stri
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedProto;
options.KnownProxies.Clear();
#pragma warning disable ASPDEPR005 // KnownNetworks is obsolete
options.KnownNetworks.Clear();
#pragma warning restore ASPDEPR005 // KnownNetworks is obsolete
options.KnownIPNetworks.Clear();
options.ForwardLimit = limit;
});
})
Expand Down Expand Up @@ -1176,7 +1185,10 @@ public async Task ForwardersWithDirectOptionsRunsTwice(int limit, string header,
ForwardLimit = limit,
};
options.KnownProxies.Clear();
#pragma warning disable ASPDEPR005 // KnownNetworks is obsolete
options.KnownNetworks.Clear();
#pragma warning restore ASPDEPR005 // KnownNetworks is obsolete
options.KnownIPNetworks.Clear();
app.UseForwardedHeaders(options);
app.UseForwardedHeaders(options);
});
Expand Down
1 change: 1 addition & 0 deletions src/Middleware/HttpOverrides/test/IPNetworkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Microsoft.AspNetCore.HttpOverrides;

[Obsolete("Microsoft.AspNetCore.HttpOverrides.IPNetwork is obsolete. For more information, visit https://aka.ms/aspnet/deprecate/005.", DiagnosticId = "ASPDEPR005")]
public class IPNetworkTest
{
[Theory]
Expand Down
Loading