8000 GH-113171: Fix "private" (non-global) IP address ranges by jstasiak · Pull Request #113179 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-113171: Fix "private" (non-global) IP address ranges #113179

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 9 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Actually fix the shared address space too
  • Loading branch information
jstasiak committed Mar 18, 2024
commit d3ee80d34bbac907e96b37ed64ae9252c71f36a0
19 changes: 9 additions & 10 deletions Doc/library/ipaddress.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,15 @@ write code that handles both IP versions correctly. Address objects are

``True`` if the address is defined as not globally reachable by
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
(for IPv6) with the following exceptions:
(for IPv6) with the following exception:

* ``is_private`` is ``False`` for the shared address space (``100.64.0.0/10``)
* For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
semantics of the underlying IPv4 addresses and the following condition holds
(see :attr:`IPv6Address.ipv4_mapped`)::
For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
semantics of the underlying IPv4 addresses and the following condition holds
(see :attr:`IPv6Address.ipv4_mapped`)::

address.is_private == address.ipv4_mapped.is_private
address.is_private == address.ipv4_mapped.is_private

``is_private`` has value opposite to :attr:`is_global`, except for the shared address space
(``100.64.0.0/10`` range) where they are both ``False``.
``is_private`` has value opposite to :attr:`is_global`.

.. versionchanged:: 3.13

Expand All @@ -203,6 +201,8 @@ write code that handles both IP versions correctly. Address objects are
* There are exceptions within ``2001::/23`` (otherwise considered private): ``2001:1::1/128``,
``2001:1::2/128``, ``2001:3::/32``, ``2001:4:112::/48``, ``2001:20::/28``, ``2001:30::/28``.
The exceptions are not considered private.
* The shared address space (``100.64.0.0/10``) is not treated in any special way anymore,
previously it was considered both not private and not global. It is considered private now.

.. attribute:: is_global

Expand All @@ -216,8 +216,7 @@ write code that handles both IP versions correctly. Address objects are

address.is_global == address.ipv4_mapped.is_global

``is_global`` has value opposite to :attr:`is_private`, except for the shared address space
(``100.64.0.0/10`` range) where they are both ``False``.
``is_global`` has value opposite to :attr:`is_private`.

.. versionadded:: 3.4

Expand Down
43 changes: 17 additions & 26 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,17 +1339,15 @@ def is_reserved(self):
def is_private(self):
"""``True`` if the address is defined as not globally reachable by
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
(for IPv6) with the following exceptions:
(for IPv6) with the following exception:

* ``is_private`` is ``False`` for ``100.64.0.0/10``
* For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
semantics of the underlying IPv4 addresses and the following condition holds
(see :attr:`IPv6Address.ipv4_mapped`)::
For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
semantics of the underlying IPv4 addresses and the following condition holds
(see :attr:`IPv6Address.ipv4_mapped`)::

address.is_private == address.ipv4_mapped.is_private
address.is_private == address.ipv4_mapped.is_private

``is_private`` has value opposite to :attr:`is_global`, except for the ``100.64.0.0/10``
IPv4 range where they are both ``False``.
``is_private`` has value opposite to :attr:`is_global`.
"""
return (
any(self in net for net in self._constants._private_networks)
Expand All @@ -1369,10 +1367,9 @@ def is_global(self):

address.is_global == address.ipv4_mapped.is_global

``is_global`` has value opposite to :attr:`is_private`, except for the ``100.64.0.0/10``
IPv4 range where they are both ``False``.
``is_global`` has value opposite to :attr:`is_private`.
"""
return self not in self._constants._public_network and not self.is_private
return not self.is_private

@property
def is_multicast(self):
Expand Down Expand Up @@ -1571,9 +1568,7 @@ def is_global(self):
iana-ipv4-special-registry.

"""
return (not (self.network_address in IPv4Network('100.64.0.0/10') and
self.broadcast_address in IPv4Network('100.64.0.0/10')) and
not self.is_private)
return not self.is_private


class _IPv4Constants:
Expand All @@ -1583,13 +1578,12 @@ class _IPv4Constants:

_multicast_network = IPv4Network('224.0.0.0/4')

_public_network = IPv4Network('100.64.0.0/10')

# Not globally reachable address blocks listed on
# https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
_private_networks = [
IPv4Network('0.0.0.0/8'),
IPv4Network('10.0.0.0/8'),
IPv4Network('100.64.0.0/10'),
IPv4Network('127.0.0.0/8'),
IPv4Network('169.254.0.0/16'),
IPv4Network('172.16.0.0/12'),
Expand Down Expand Up @@ -2085,17 +2079,15 @@ def is_site_local(self):
def is_private(self):
"""``True`` if the address is defined as not globally reachable by
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
(for IPv6) with the following exceptions:
(for IPv6) with the following exception:

* ``is_private`` is ``False`` for ``100.64.0.0/10``
* For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
semantics of the underlying IPv4 addresses and the following condition holds
(see :attr:`IPv6Address.ipv4_mapped`)::
For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
semantics of the underlying IPv4 addresses and the following condition holds
(see :attr:`IPv6Address.ipv4_mapped`)::

address.is_private == address.ipv4_mapped.is_private
address.is_private == address.ipv4_mapped.is_private

``is_private`` has value opposite to :attr:`is_global`, except for the ``100.64.0.0/10``
IPv4 range where they are both ``False``.
``is_private`` has value opposite to :attr:`is_global`.
"""
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is not None:
Expand All @@ -2117,8 +2109,7 @@ def is_global(self):

address.is_global == address.ipv4_mapped.is_global

``is_global`` has value opposite to :attr:`is_private`, except for the ``100.64.0.0/10``
IPv4 range where they are both ``False``.
``is_global`` has value opposite to :attr:`is_private`.
"""
return not self.is_private

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,7 @@ def testReservedIpv4(self):
self.assertEqual(True, ipaddress.ip_network(
'127.42.0.0/16').is_loopback)
self.assertEqual(False, ipaddress.ip_network('128.0.0.0').is_loopback)
self.assertEqual(False,
self.assertEqual(True,
ipaddress.ip_network('100.64.0.0/10').is_private)
self.assertEqual(False, ipaddress.ip_network('100.64.0.0/10').is_global)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ The new behavior:
* There are exceptions within ``2001::/23`` (otherwise considered private): ``2001:1::1/128``,
``2001:1::2/128``, ``2001:3::/32``, ``2001:4:112::/48``, ``2001:20::/28``, ``2001:30::/28``.
The exceptions are not considered private.
* The shared address space (``100.64.0.0/10``) is not treated in any special way anymore,
previously it was considered both not private and not global. It is considered private now.
0