[Python-checkins] bpo-33433 Fix private address checking for IPv4 mapped IPv6. (GH-26172)

miss-islington webhook-mailer at python.org
Mon May 17 15:42:36 EDT 2021


https://github.com/python/cpython/commit/a44bb6ddb17538b7b2096d13eb79a1208bd97f34
commit: a44bb6ddb17538b7b2096d13eb79a1208bd97f34
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-05-17T12:42:08-07:00
summary:

bpo-33433 Fix private address checking for IPv4 mapped IPv6. (GH-26172)


For IPv4 mapped IPv6 addresses, defer privacy check to the mapped IPv4 address. Solves bug where public mapped IPv4 addresses are considered private by the IPv6 check.

Automerge-Triggered-By: GH:gpshead
(cherry picked from commit 83f0f8d62f279f846a92fede2244beaa0149b9d8)

Co-authored-by: Pete Wicken <2273100+JamoBox at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2021-05-16-17-48-24.bpo-33433.MyzO71.rst
M Lib/ipaddress.py
M Lib/test/test_ipaddress.py

diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index af7aedfa6e51a..4a6496a5da3ef 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -16,6 +16,7 @@
 IPV4LENGTH = 32
 IPV6LENGTH = 128
 
+
 class AddressValueError(ValueError):
     """A Value Error related to the address."""
 
@@ -2002,9 +2003,13 @@ def is_private(self):
 
         Returns:
             A boolean, True if the address is reserved per
-            iana-ipv6-special-registry.
+            iana-ipv6-special-registry, or is ipv4_mapped and is
+            reserved in the iana-ipv4-special-registry.
 
         """
+        ipv4_mapped = self.ipv4_mapped
+        if ipv4_mapped is not None:
+            return ipv4_mapped.is_private
         return any(self in net for net in self._constants._private_networks)
 
     @property
diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py
index 90559ce1242c5..ff77bdb1bbc58 100644
--- a/Lib/test/test_ipaddress.py
+++ b/Lib/test/test_ipaddress.py
@@ -2339,6 +2339,12 @@ def testIpv4Mapped(self):
         self.assertEqual(ipaddress.ip_address('::ffff:c0a8:101').ipv4_mapped,
                          ipaddress.ip_address('192.168.1.1'))
 
+    def testIpv4MappedPrivateCheck(self):
+        self.assertEqual(
+                True, ipaddress.ip_address('::ffff:192.168.1.1').is_private)
+        self.assertEqual(
+                False, ipaddress.ip_address('::ffff:172.32.0.0').is_private)
+
     def testAddrExclude(self):
         addr1 = ipaddress.ip_network('10.1.1.0/24')
         addr2 = ipaddress.ip_network('10.1.1.0/26')
diff --git a/Misc/NEWS.d/next/Library/2021-05-16-17-48-24.bpo-33433.MyzO71.rst b/Misc/NEWS.d/next/Library/2021-05-16-17-48-24.bpo-33433.MyzO71.rst
new file mode 100644
index 0000000000000..703e038fac985
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-05-16-17-48-24.bpo-33433.MyzO71.rst
@@ -0,0 +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.



More information about the Python-checkins mailing list