From 21b6e92023f6229c85827084a95b2015fa0adfc8 Mon Sep 17 00:00:00 2001 From: Pete Wicken Date: Sun, 16 May 2021 18:36:02 +0100 Subject: [PATCH 1/4] Fix private address checking for IPv4 mapped IPv6. --- Lib/ipaddress.py | 4 ++++ Lib/test/test_ipaddress.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index af7aedfa6e51a1..12e284ab86fc73 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.""" @@ -2005,6 +2006,9 @@ def is_private(self): iana-ipv6-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 90559ce1242c55..ff77bdb1bbc582 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') From 12ad7ef0cdab31f4fb6a4041626898a18f6a25f6 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 16 May 2021 17:48:27 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-05-16-17-48-24.bpo-33433.MyzO71.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-05-16-17-48-24.bpo-33433.MyzO71.rst 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 00000000000000..40b59b61e87260 --- /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, privacy check is deferred to the mapped IPv4 address. Solves bug where public mapped IPv4 addresses are considered private by the IPv6 check. \ No newline at end of file From a7f23a48d47870769c34f279a7e1c916753057eb Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Mon, 17 May 2021 00:20:32 -0700 Subject: [PATCH 3/4] link to the RFC and add some ReST goodness. --- .../next/Library/2021-05-16-17-48-24.bpo-33433.MyzO71.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 40b59b61e87260..703e038fac9856 100644 --- 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 @@ -1 +1 @@ -For IPv4 mapped IPv6 addresses, privacy check is deferred to the mapped IPv4 address. Solves bug where public mapped IPv4 addresses are considered private by the IPv6 check. \ No newline at end of file +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. From e24bed312588bda009ea9de01b81cbe175d5a6a0 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Mon, 17 May 2021 00:27:40 -0700 Subject: [PATCH 4/4] update docstring to clarify the behavior. --- Lib/ipaddress.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 12e284ab86fc73..4a6496a5da3ef8 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -2003,7 +2003,8 @@ 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