gh-82836: fix private network check (GH-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".
(cherry picked from commit ed391090cc)

Co-authored-by: Pete Wicken <2273100+JamoBox@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2022-11-29 14:12:57 -08:00 committed by GitHub
parent d7c2e0a537
commit 8dafefcace
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