File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed
main/java/org/apache/hc/core5/net
test/java/org/apache/hc/core5/net Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -66,8 +66,13 @@ private InetAddressUtils() {
6666 private static final Pattern IPV4_PATTERN =
6767 Pattern .compile ("^" + IPV4_BASIC_PATTERN_STRING + "$" );
6868
69- private static final Pattern IPV4_MAPPED_IPV6_PATTERN = // TODO does not allow for redundant leading zeros
70- Pattern .compile ("^::[fF]{4}:" + IPV4_BASIC_PATTERN_STRING + "$" );
69+ // Accept IPv4-mapped IPv6, allowing redundant leading zeros in the IPv4 part.
70+ // Examples: ::ffff:1.2.3.4, ::FFFF:001.002.003.004
71+ // Still enforces each octet <= 255 and max 3 digits per octet.
72+ private static final String IPV4_MAPPED_OCTET = "(25[0-5]|2[0-4]\\ d|1\\ d\\ d|0?\\ d?\\ d)" ;
73+ private static final Pattern IPV4_MAPPED_IPV6_PATTERN = Pattern .compile ("^::[fF]{4}:((" + IPV4_MAPPED_OCTET + ")\\ .){3}" + IPV4_MAPPED_OCTET + "$" );
74+
75+
7176
7277 private static final Pattern IPV6_STD_PATTERN =
7378 Pattern .compile (
Original file line number Diff line number Diff line change @@ -198,4 +198,22 @@ void testInValidIPv4MappedIPv6Address() {
198198 Assertions .assertFalse (InetAddressUtils .isIPv4MappedIPv6 ("::ffff:1:2:3:4" ));
199199 }
200200
201+ @ Test
202+ void testValidIPv4MappedIPv6AddressWithLeadingZeros () {
203+ Assertions .assertTrue (InetAddressUtils .isIPv4MappedIPv6 ("::ffff:001.002.003.004" ));
204+ Assertions .assertTrue (InetAddressUtils .isIPv4MappedIPv6 ("::FFFF:000.000.000.255" ));
205+ Assertions .assertTrue (InetAddressUtils .isIPv4MappedIPv6 ("::ffff:010.020.030.040" ));
206+ }
207+
208+ @ Test
209+ void testInvalidIPv4MappedIPv6AddressWithBadOctets () {
210+ // >255 not allowed
211+ Assertions .assertFalse (InetAddressUtils .isIPv4MappedIPv6 ("::ffff:256.000.000.000" ));
212+ // too few octets
213+ Assertions .assertFalse (InetAddressUtils .isIPv4MappedIPv6 ("::ffff:01.02.03" ));
214+ // too many digits in an octet (4 digits)
215+ Assertions .assertFalse (InetAddressUtils .isIPv4MappedIPv6 ("::ffff:0255.000.000.000" ));
216+ }
217+
218+
201219}
You can’t perform that action at this time.
0 commit comments