gh-82836: fix private network check (#97733)

Fixes private checks for network objects. The previous method would incorrectly return True for a private check in cases such as "0.0.0.0/0".
This commit is contained in:
Pete Wicken 2022-11-29 21:32:18 +00:00 committed by GitHub
parent 052bc128ac
commit ed391090cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 4 deletions

View File

@ -1077,15 +1077,16 @@ class _BaseNetwork(_IPAddressBase):
@property
def is_private(self):
"""Test if this address is allocated for private networks.
"""Test if this network belongs to a private range.
Returns:
A boolean, True if the address is reserved per
A boolean, True if the network is reserved per
iana-ipv4-special-registry or iana-ipv6-special-registry.
"""
return (self.network_address.is_private and
self.broadcast_address.is_private)
return any(self.network_address in priv_network and
self.broadcast_address in priv_network
for priv_network in self._constants._private_networks)
@property
def is_global(self):
@ -1122,6 +1123,15 @@ class _BaseNetwork(_IPAddressBase):
return (self.network_address.is_loopback and
self.broadcast_address.is_loopback)
class _BaseConstants:
_private_networks = []
_BaseNetwork._constants = _BaseConstants
class _BaseV4:
"""Base IPv4 object.
@ -1561,6 +1571,7 @@ class _IPv4Constants:
IPv4Address._constants = _IPv4Constants
IPv4Network._constants = _IPv4Constants
class _BaseV6:
@ -2285,3 +2296,4 @@ class _IPv6Constants:
IPv6Address._constants = _IPv6Constants
IPv6Network._constants = _IPv6Constants

View File

@ -2277,6 +2277,39 @@ class IpaddrUnitTest(unittest.TestCase):
self.assertEqual(False, ipaddress.ip_address('128.0.0.0').is_loopback)
self.assertEqual(True, ipaddress.ip_network('0.0.0.0').is_unspecified)
def testPrivateNetworks(self):
self.assertEqual(False, ipaddress.ip_network("0.0.0.0/0").is_private)
self.assertEqual(False, ipaddress.ip_network("1.0.0.0/8").is_private)
self.assertEqual(True, ipaddress.ip_network("0.0.0.0/8").is_private)
self.assertEqual(True, ipaddress.ip_network("10.0.0.0/8").is_private)
self.assertEqual(True, ipaddress.ip_network("127.0.0.0/8").is_private)
self.assertEqual(True, ipaddress.ip_network("169.254.0.0/16").is_private)
self.assertEqual(True, ipaddress.ip_network("172.16.0.0/12").is_private)
self.assertEqual(True, ipaddress.ip_network("192.0.0.0/29").is_private)
self.assertEqual(True, ipaddress.ip_network("192.0.0.170/31").is_private)
self.assertEqual(True, ipaddress.ip_network("192.0.2.0/24").is_private)
self.assertEqual(True, ipaddress.ip_network("192.168.0.0/16").is_private)
self.assertEqual(True, ipaddress.ip_network("198.18.0.0/15").is_private)
self.assertEqual(True, ipaddress.ip_network("198.51.100.0/24").is_private)
self.assertEqual(True, ipaddress.ip_network("203.0.113.0/24").is_private)
self.assertEqual(True, ipaddress.ip_network("240.0.0.0/4").is_private)
self.assertEqual(True, ipaddress.ip_network("255.255.255.255/32").is_private)
self.assertEqual(False, ipaddress.ip_network("::/0").is_private)
self.assertEqual(False, ipaddress.ip_network("::ff/128").is_private)
self.assertEqual(True, ipaddress.ip_network("::1/128").is_private)
self.assertEqual(True, ipaddress.ip_network("::/128").is_private)
self.assertEqual(True, ipaddress.ip_network("::ffff:0:0/96").is_private)
self.assertEqual(True, ipaddress.ip_network("100::/64").is_private)
self.assertEqual(True, ipaddress.ip_network("2001::/23").is_private)
self.assertEqual(True, ipaddress.ip_network("2001:2::/48").is_private)
self.assertEqual(True, ipaddress.ip_network("2001:db8::/32").is_private)
self.assertEqual(True, ipaddress.ip_network("2001:10::/28").is_private)
self.assertEqual(True, ipaddress.ip_network("fc00::/7").is_private)
self.assertEqual(True, ipaddress.ip_network("fe80::/10").is_private)
def testReservedIpv6(self):
self.assertEqual(True, ipaddress.ip_network('ffff::').is_multicast)

View File

@ -0,0 +1 @@
Fix :attr:`~ipaddress.IPv4Address.is_private` properties in the :mod:`ipaddress` module. Previously non-private networks (0.0.0.0/0) would return True from this method; now they correctly return False.