| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 65f2e24 commit 78394a6
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1648,8 +1648,16 @@ def _ip_int_from_string(cls, ip_str): | |||
| 1648 | 1648 | """ | |
| 1649 | 1649 | if not ip_str: | |
| 1650 | 1650 | raise AddressValueError('Address cannot be empty') | |
| 1651 | + if len(ip_str) > 39: | ||
| 1652 | + msg = ("At most 39 characters expected in " | ||
| 1653 | + f"{ip_str[:14]!r}({len(ip_str)-28} chars elided){ip_str[-14:]!r}") | ||
| 1654 | + raise AddressValueError(msg) | ||
| 1651 | 1655 | ||
| 1652 | - parts = ip_str.split(':') | ||
| 1656 | + # We want to allow more parts than the max to be 'split' | ||
| 1657 | + # to preserve the correct error message when there are | ||
| 1658 | + # too many parts combined with '::' | ||
| 1659 | + _max_parts = cls._HEXTET_COUNT + 1 | ||
| 1660 | + parts = ip_str.split(':', maxsplit=_max_parts) | ||
| 1653 | 1661 | ||
| 1654 | 1662 | # An IPv6 address needs at least 2 colons (3 parts). | |
| 1655 | 1663 | _min_parts = 3 | |
@@ -1669,7 +1677,6 @@ def _ip_int_from_string(cls, ip_str): | |||
| 1669 | 1677 | # An IPv6 address can't have more than 8 colons (9 parts). | |
| 1670 | 1678 | # The extra colon comes from using the "::" notation for a single | |
| 1671 | 1679 | # leading or trailing zero part. | |
| 1672 | - _max_parts = cls._HEXTET_COUNT + 1 | ||
| 1673 | 1680 | if len(parts) > _max_parts: | |
| 1674 | 1681 | msg = "At most %d colons permitted in %r" % (_max_parts-1, ip_str) | |
| 1675 | 1682 | raise AddressValueError(msg) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -389,6 +389,17 @@ def assertBadSplit(addr): | |||
| 389 | 389 | # A trailing IPv4 address is two parts | |
| 390 | 390 | assertBadSplit("10:9:8:7:6:5:4:3:42.42.42.42%scope") | |
| 391 | 391 | ||
| 392 | + def test_bad_address_split_v6_too_long(self): | ||
| 393 | + def assertBadSplit(addr): | ||
| 394 | + msg = r"At most 39 characters expected in %s" | ||
| 395 | + with self.assertAddressError(msg, repr(re.escape(addr[:14]))): | ||
| 396 | + ipaddress.IPv6Address(addr) | ||
| 397 | + | ||
| 398 | + # Long IPv6 address | ||
| 399 | + long_addr = ("0:" * 10000) + "0" | ||
| 400 | + assertBadSplit(long_addr) | ||
| 401 | + assertBadSplit(long_addr + "%zoneid") | ||
| 402 | + | ||
| 392 | 403 | def test_bad_address_split_v6_too_many_parts(self): | |
| 393 | 404 | def assertBadSplit(addr): | |
| 394 | 405 | msg = "Exactly 8 parts expected without '::' in %r" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Short-circuit the processing of long IPv6 addresses early in :mod:`ipaddress` to prevent excessive | ||
| 2 | + memory consumption and a minor denial-of-service. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments