Skip to content

bpo-33433 Fix private address checking for IPv4 mapped IPv6. #26172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
IPV4LENGTH = 32
IPV6LENGTH = 128


class AddressValueError(ValueError):
"""A Value Error related to the address."""

Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -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.