FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.13] gh-128840: Fix parsing long IPv6 addresses with embedded IPv4 address (GH-134836) by miss-islington · Pull Request #134846 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 6 additions & 4 deletions Lib/ipaddress.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1674,10 +1674,12 @@ 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)
if len(ip_str) > 45:
shorten = ip_str
if len(shorten) > 100:
shorten = f'{ip_str[:45]}({len(ip_str)-90} chars elided){ip_str[-45:]}'
raise AddressValueError(f"At most 45 characters expected in "
f"{shorten!r}")

# We want to allow more parts than the max to be 'split'
# to preserve the correct error message when there are
Expand Down
19 changes: 17 additions & 2 deletions Lib/test/test_ipaddress.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,16 @@ def assertBadSplit(addr):

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]))):
msg = r"At most 45 characters expected in '%s"
with self.assertAddressError(msg, re.escape(addr[:45])):
ipaddress.IPv6Address(addr)

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

def test_bad_address_split_v6_too_many_parts(self):
def assertBadSplit(addr):
Expand Down Expand Up @@ -2189,6 +2191,11 @@ def testIPv6AddressTooLarge(self):
self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1'),
ipaddress.ip_address('FFFF::c000:201'))

self.assertEqual(ipaddress.ip_address('0000:0000:0000:0000:0000:FFFF:192.168.255.255'),
ipaddress.ip_address('::ffff:c0a8:ffff'))
self.assertEqual(ipaddress.ip_address('FFFF:0000:0000:0000:0000:0000:192.168.255.255'),
ipaddress.ip_address('ffff::c0a8:ffff'))

self.assertEqual(ipaddress.ip_address('::FFFF:192.0.2.1%scope'),
ipaddress.ip_address('::FFFF:c000:201%scope'))
self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1%scope'),
Expand All @@ -2201,6 +2208,10 @@ def testIPv6AddressTooLarge(self):
ipaddress.ip_address('::FFFF:c000:201%scope'))
self.assertNotEqual(ipaddress.ip_address('FFFF::192.0.2.1'),
ipaddress.ip_address('FFFF::c000:201%scope'))
self.assertEqual(ipaddress.ip_address('0000:0000:0000:0000:0000:FFFF:192.168.255.255%scope'),
ipaddress.ip_address('::ffff:c0a8:ffff%scope'))
self.assertEqual(ipaddress.ip_address('FFFF:0000:0000:0000:0000:0000:192.168.255.255%scope'),
ipaddress.ip_address('ffff::c0a8:ffff%scope'))

def testIPVersion(self):
self.assertEqual(self.ipv4_address.version, 4)
Expand Down Expand Up @@ -2604,6 +2615,10 @@ def testCompressIPv6Address(self):
'::7:6:5:4:3:2:0': '0:7:6:5:4:3:2:0/128',
'7:6:5:4:3:2:1::': '7:6:5:4:3:2:1:0/128',
'0:6:5:4:3:2:1::': '0:6:5:4:3:2:1:0/128',
'0000:0000:0000:0000:0000:0000:255.255.255.255': '::ffff:ffff/128',
'0000:0000:0000:0000:0000:ffff:255.255.255.255': '::ffff:255.255.255.255/128',
'ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255':
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128',
}
for uncompressed, compressed in list(test_addresses.items()):
self.assertEqual(compressed, str(ipaddress.IPv6Interface(
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix parsing long IPv6 addresses with embedded IPv4 address.

Back | FazBrowse Home | New Git URL