Skip to content

Commit 69a7663

Browse files
rschmittok2c
authored andcommitted
Fix validation of 0/8 IPv4 addresses
0.0.0.0/8 IPv4 addresses are not illegal, just "reserved." This change brings `InetAddressUtils` in line with the JDK's `InetAddress` implementation, which accepts these addresses without issue, e.g.: ```java InetAddress.getByAddress(new byte[]{ 0, 10, 25, 68 }); InetAddress.getByName("0.10.25.68"); ```
1 parent a70a62c commit 69a7663

File tree

2 files changed

+2
-3
lines changed

2 files changed

+2
-3
lines changed

httpcore5/src/main/java/org/apache/hc/core5/net/InetAddressUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ private InetAddressUtils() {
5959
}
6060

6161
private static final String IPV4_BASIC_PATTERN_STRING =
62-
"(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){1}" + // initial first field, 1-255
63-
"(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){2}" + // following 2 fields, 0-255 followed by .
62+
"(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}" + // First 3 fields, 0-255, followed by .
6463
"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"; // final field, 0-255
6564

6665
private static final Pattern IPV4_PATTERN =

httpcore5/src/test/java/org/apache/hc/core5/net/TestInetAddressUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void testValidIPv4Address() {
4040
Assertions.assertTrue(InetAddressUtils.isIPv4("127.0.0.1"));
4141
Assertions.assertTrue(InetAddressUtils.isIPv4("192.168.0.0"));
4242
Assertions.assertTrue(InetAddressUtils.isIPv4("255.255.255.255"));
43+
Assertions.assertTrue(InetAddressUtils.isIPv4("0.168.0.0"));
4344
}
4445

4546
@Test
@@ -48,7 +49,6 @@ void testInvalidIPv4Address() {
4849
Assertions.assertFalse(InetAddressUtils.isIPv4("g.ar.ba.ge"));
4950
Assertions.assertFalse(InetAddressUtils.isIPv4("192.168.0"));
5051
Assertions.assertFalse(InetAddressUtils.isIPv4("256.255.255.255"));
51-
Assertions.assertFalse(InetAddressUtils.isIPv4("0.168.0.0")); //IP address that starts with zero not allowed
5252
}
5353

5454
@Test

0 commit comments

Comments
 (0)