Skip to content

Commit

Permalink
gh-82836: fix private network check (#97733)
Browse files Browse the repository at this point in the history
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".
  • Loading branch information
JamoBox authored Nov 29, 2022
1 parent 052bc12 commit ed39109
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,15 +1077,16 @@ def is_link_local(self):

@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):
Expand Down Expand Up @@ -1122,6 +1123,15 @@ def is_loopback(self):
return (self.network_address.is_loopback and
self.broadcast_address.is_loopback)


class _BaseConstants:

_private_networks = []


_BaseNetwork._constants = _BaseConstants


class _BaseV4:

"""Base IPv4 object.
Expand Down Expand Up @@ -1561,6 +1571,7 @@ class _IPv4Constants:


IPv4Address._constants = _IPv4Constants
IPv4Network._constants = _IPv4Constants


class _BaseV6:
Expand Down Expand Up @@ -2285,3 +2296,4 @@ class _IPv6Constants:


IPv6Address._constants = _IPv6Constants
IPv6Network._constants = _IPv6Constants
33 changes: 33 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2277,6 +2277,39 @@ def testReservedIpv4(self):
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit ed39109

Please sign in to comment.