8000 bpo-33433 Fix private address checking for IPv4 mapped IPv6. (GH-26172) · python/cpython@83f0f8d · GitHub
[go: up one dir, main page]

Skip to content

Commit 83f0f8d

Browse files
authored
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
1 parent c10392e commit 83f0f8d

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Lib/ipaddress.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
IPV4LENGTH = 32
1717
IPV6LENGTH = 128
1818

19+
1920
class AddressValueError(ValueError):
2021
"""A Value Error related to the address."""
2122

@@ -2002,9 +2003,13 @@ def is_private(self):
20022003
20032004
Returns:
20042005
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.
20062008
20072009
"""
2010+
ipv4_mapped = self.ipv4_mapped
2011+
if ipv4_mapped is not None:
2012+
return ipv4_mapped.is_private
20082013
return any(self in net for net in self._constants._private_networks)
20092014

20102015
@property

Lib/test/test_ipaddress.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,12 @@ def testIpv4Mapped(self):
23392339
self.assertEqual(ipaddress.ip_address('::ffff:c0a8:101').ipv4_mapped,
23402340
ipaddress.ip_address('192.168.1.1'))
23412341

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+
23422348
def testAddrExclude(self):
23432349
addr1 = ipaddress.ip_network('10.1.1.0/24')
23442350
addr2 = ipaddress.ip_network('10.1.1.0/26')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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.

0 commit comments

Comments
 (0)
0