summaryrefslogtreecommitdiffstats
path: root/Lib/ipaddress.py
diff options
context:
space:
mode:
authorPete Wicken <2273100+JamoBox@users.noreply.github.com>2022-11-29 21:32:18 (GMT)
committerGitHub <noreply@github.com>2022-11-29 21:32:18 (GMT)
commited391090cc8332406e6225d40877db6ff44a7104 (patch)
tree1cbb1d2dd7f660e949e924a149907d4152b5a89c /Lib/ipaddress.py
parent052bc128ac0bcafd36a0cbee5f4c2a10d74468dc (diff)
downloadcpython-ed391090cc8332406e6225d40877db6ff44a7104.zip
cpython-ed391090cc8332406e6225d40877db6ff44a7104.tar.gz
cpython-ed391090cc8332406e6225d40877db6ff44a7104.tar.bz2
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".
Diffstat (limited to 'Lib/ipaddress.py')
-rw-r--r--Lib/ipaddress.py20
1 files changed, 16 insertions, 4 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index 3f15601..1cb71d8 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -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