Skip to content

Commit 9edb469

Browse files
adityamandaleekawtgodbe
authored andcommitted
[2.1] Fix IPNetwork.Contains for prefixes which are not at start of subnet range. (#50967)
* Fix IPNetwork.Contains behavior for prefixes which are not at start of subnet range. Backport of 3d00915 to 2.1. * Update patchconfig.
1 parent 98a1033 commit 9edb469

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

eng/PatchConfig.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Later on, this will be checked using this condition:
139139
Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv;
140140
Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
141141
Microsoft.AspNetCore.SignalR.Redis;
142+
Microsoft.AspNetCore.HttpOverrides;
142143
</PackagesInPatch>
143144
</PropertyGroup>
144145
</Project>

src/Middleware/HttpOverrides/src/IPNetwork.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Net;
6+
using System.Net.Sockets;
57

68
namespace Microsoft.AspNetCore.HttpOverrides
79
{
810
public class IPNetwork
911
{
1012
public IPNetwork(IPAddress prefix, int prefixLength)
1113
{
14+
CheckPrefixLengthRange(prefix, prefixLength);
1215
Prefix = prefix;
1316
PrefixLength = prefixLength;
1417
PrefixBytes = Prefix.GetAddressBytes();
@@ -20,7 +23,7 @@ public IPNetwork(IPAddress prefix, int prefixLength)
2023
private byte[] PrefixBytes { get; }
2124

2225
/// <summary>
23-
/// The CIDR notation of the subnet mask
26+
/// The CIDR notation of the subnet mask
2427
/// </summary>
2528
public int PrefixLength { get; }
2629

@@ -36,7 +39,7 @@ public bool Contains(IPAddress address)
3639
var addressBytes = address.GetAddressBytes();
3740
for (int i = 0; i < PrefixBytes.Length && Mask[i] != 0; i++)
3841
{
39-
if (PrefixBytes[i] != (addressBytes[i] & Mask[i]))
42+
if ((PrefixBytes[i] & Mask[i]) != (addressBytes[i] & Mask[i]))
4043
{
4144
return false;
4245
}
@@ -63,5 +66,23 @@ private byte[] CreateMask()
6366

6467
return mask;
6568
}
69+
70+
private static void CheckPrefixLengthRange(IPAddress prefix, int prefixLength)
71+
{
72+
if (prefixLength < 0)
73+
{
74+
throw new ArgumentOutOfRangeException(nameof(prefixLength));
75+
}
76+
77+
if (prefix.AddressFamily == AddressFamily.InterNetwork && prefixLength > 32)
78+
{
79+
throw new ArgumentOutOfRangeException(nameof(prefixLength));
80+
}
81+
82+
if (prefix.AddressFamily == AddressFamily.InterNetworkV6 && prefixLength > 128)
83+
{
84+
throw new ArgumentOutOfRangeException(nameof(prefixLength));
85+
}
86+
}
6687
}
6788
}

src/Middleware/HttpOverrides/test/IPNetworkTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ public class IPNetworkTest
1212
[InlineData("174.0.0.0", 7, "175.1.1.10")]
1313
[InlineData("10.174.0.0", 15, "10.175.1.10")]
1414
[InlineData("10.168.0.0", 14, "10.171.1.10")]
15+
[InlineData("192.168.0.1", 31, "192.168.0.0")]
16+
[InlineData("192.168.0.1", 31, "192.168.0.1")]
17+
[InlineData("192.168.0.1", 32, "192.168.0.1")]
18+
[InlineData("192.168.1.1", 0, "0.0.0.0")]
19+
[InlineData("192.168.1.1", 0, "255.255.255.255")]
20+
[InlineData("2001:db8:3c4d::", 127, "2001:db8:3c4d::1")]
21+
[InlineData("2001:db8:3c4d::1", 128, "2001:db8:3c4d::1")]
22+
[InlineData("2001:db8:3c4d::1", 0, "::")]
23+
[InlineData("2001:db8:3c4d::1", 0, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")]
1524
public void Contains_Positive(string prefixText, int length, string addressText)
1625
{
1726
var network = new IPNetwork(IPAddress.Parse(prefixText), length);
@@ -23,6 +32,9 @@ public void Contains_Positive(string prefixText, int length, string addressText)
2332
[InlineData("174.0.0.0", 7, "173.1.1.10")]
2433
[InlineData("10.174.0.0", 15, "10.173.1.10")]
2534
[InlineData("10.168.0.0", 14, "10.172.1.10")]
35+
[InlineData("192.168.0.1", 31, "192.168.0.2")]
36+
[InlineData("192.168.0.1", 32, "192.168.0.0")]
37+
[InlineData("2001:db8:3c4d::", 127, "2001:db8:3c4d::2")]
2638
public void Contains_Negative(string prefixText, int length, string addressText)
2739
{
2840
var network = new IPNetwork(IPAddress.Parse(prefixText), length);

0 commit comments

Comments
 (0)