bpo-33433 Fix private address checking for IPv4 mapped IPv6. (GH-26172) · python/cpython@83f0f8d (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
16 16 IPV4LENGTH = 32
17 17 IPV6LENGTH = 128
18 18
19 +
19 20 class AddressValueError(ValueError):
20 21 """A Value Error related to the address."""
21 22
@@ -2002,9 +2003,13 @@ def is_private(self):
2002 2003
2003 2004 Returns:
2004 2005 A boolean, True if the address is reserved per
2005 - iana-ipv6-special-registry.
2006 + iana-ipv6-special-registry, or is ipv4_mapped and is
2007 + reserved in the iana-ipv4-special-registry.
2006 2008
2007 2009 """
2010 +ipv4_mapped = self.ipv4_mapped
2011 +if ipv4_mapped is not None:
2012 +return ipv4_mapped.is_private
2008 2013 return any(self in net for net in self._constants._private_networks)
2009 2014
2010 2015 @property
Original file line number Diff line number Diff line change
@@ -2339,6 +2339,12 @@ def testIpv4Mapped(self):
2339 2339 self.assertEqual(ipaddress.ip_address('::ffff:c0a8:101').ipv4_mapped,
2340 2340 ipaddress.ip_address('192.168.1.1'))
2341 2341
2342 +def testIpv4MappedPrivateCheck(self):
2343 +self.assertEqual(
2344 +True, ipaddress.ip_address('::ffff:192.168.1.1').is_private)
2345 +self.assertEqual(
2346 +False, ipaddress.ip_address('::ffff:172.32.0.0').is_private)
2347 +
2342 2348 def testAddrExclude(self):
2343 2349 addr1 = ipaddress.ip_network('10.1.1.0/24')
2344 2350 addr2 = ipaddress.ip_network('10.1.1.0/26')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 +For IPv4 mapped IPv6 addresses (:rfc:`4291` Section 2.5.5.2), the :mod:`ipaddress.IPv6Address.is_private` check is deferred to the mapped IPv4 address. This solves a bug where public mapped IPv4 addresses were considered private by the IPv6 check.