Skip to content

[3.12] gh-128840: Limit the number of parts in IPv6 address parsing (GH-128841) #134612

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

Open
wants to merge 1 commit into
base: 3.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 9 additions & 2 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -1664,8 +1664,16 @@ def _ip_int_from_string(cls, ip_str):
"""
if not ip_str:
raise AddressValueError('Address cannot be empty')
if len(ip_str) > 39:
msg = ("At most 39 characters expected in "
f"{ip_str[:14]!r}({len(ip_str)-28} chars elided){ip_str[-14:]!r}")
raise AddressValueError(msg)

parts = ip_str.split(':')
# We want to allow more parts than the max to be 'split'
# to preserve the correct error message when there are
# too many parts combined with '::'
_max_parts = cls._HEXTET_COUNT + 1
parts = ip_str.split(':', maxsplit=_max_parts)

# An IPv6 address needs at least 2 colons (3 parts).
_min_parts = 3
Expand All @@ -1685,7 +1693,6 @@ def _ip_int_from_string(cls, ip_str):
# An IPv6 address can't have more than 8 colons (9 parts).
# The extra colon comes from using the "::" notation for a single
# leading or trailing zero part.
_max_parts = cls._HEXTET_COUNT + 1
if len(parts) > _max_parts:
msg = "At most %d colons permitted in %r" % (_max_parts-1, ip_str)
raise AddressValueError(msg)
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,17 @@ def assertBadSplit(addr):
# A trailing IPv4 address is two parts
assertBadSplit("10:9:8:7:6:5:4:3:42.42.42.42%scope")

def test_bad_address_split_v6_too_long(self):
def assertBadSplit(addr):
msg = r"At most 39 characters expected in %s"
with self.assertAddressError(msg, repr(re.escape(addr[:14]))):
ipaddress.IPv6Address(addr)

# Long IPv6 address
long_addr = ("0:" * 10000) + "0"
assertBadSplit(long_addr)
assertBadSplit(long_addr + "%zoneid")

def test_bad_address_split_v6_too_many_parts(self):
def assertBadSplit(addr):
msg = "Exactly 8 parts expected without '::' in %r"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Short-circuit the processing of long IPv6 addresses early in :mod:`ipaddress` to prevent excessive
memory consumption and a minor denial-of-service.
Loading