From 2220d106c1681866510a9f0883a2a165cdb1e418 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Wed, 11 Mar 2015 20:28:36 +0000 Subject: [PATCH] HTTPCLIENT-1631: IPv4 addresses parser accepts addresses that starts with zero Contributed by Maxim Kirilov git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1665995 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/java/org/apache/http/conn/util/InetAddressUtils.java | 3 ++- .../java/org/apache/http/conn/util/TestInetAddressUtils.java | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/httpclient/src/main/java/org/apache/http/conn/util/InetAddressUtils.java b/httpclient/src/main/java/org/apache/http/conn/util/InetAddressUtils.java index df46788b5..acee8afa2 100644 --- a/httpclient/src/main/java/org/apache/http/conn/util/InetAddressUtils.java +++ b/httpclient/src/main/java/org/apache/http/conn/util/InetAddressUtils.java @@ -43,7 +43,8 @@ public class InetAddressUtils { } private static final String IPV4_BASIC_PATTERN_STRING = - "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}" + // initial 3 fields, 0-255 followed by . + "(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){1}" + // initial first field, 1-255 + "(([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 . "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"; // final field, 0-255 private static final Pattern IPV4_PATTERN = diff --git a/httpclient/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java b/httpclient/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java index b8ce74add..0d6f73aab 100644 --- a/httpclient/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java +++ b/httpclient/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java @@ -48,6 +48,7 @@ public class TestInetAddressUtils { Assert.assertFalse(InetAddressUtils.isIPv4Address("g.ar.ba.ge")); Assert.assertFalse(InetAddressUtils.isIPv4Address("192.168.0")); Assert.assertFalse(InetAddressUtils.isIPv4Address("256.255.255.255")); + Assert.assertFalse(InetAddressUtils.isIPv4Address("0.168.0.0")); //IP address that starts with zero not allowed } @Test