Skip to content

Commit c71ea4b

Browse files
[3.10] gh-128840: Fix parsing long IPv6 addresses with embedded IPv4 address (GH-134836) (GH-135089)
(cherry picked from commit d83576b) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 0a1f75d commit c71ea4b

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

Lib/ipaddress.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,10 +1649,12 @@ def _ip_int_from_string(cls, ip_str):
16491649
"""
16501650
if not ip_str:
16511651
raise AddressValueError('Address cannot be empty')
1652-
if len(ip_str) > 39:
1653-
msg = ("At most 39 characters expected in "
1654-
f"{ip_str[:14]!r}({len(ip_str)-28} chars elided){ip_str[-14:]!r}")
1655-
raise AddressValueError(msg)
1652+
if len(ip_str) > 45:
1653+
shorten = ip_str
1654+
if len(shorten) > 100:
1655+
shorten = f'{ip_str[:45]}({len(ip_str)-90} chars elided){ip_str[-45:]}'
1656+
raise AddressValueError(f"At most 45 characters expected in "
1657+
f"{shorten!r}")
16561658

16571659
# We want to allow more parts than the max to be 'split'
16581660
# to preserve the correct error message when there are

Lib/test/test_ipaddress.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,16 @@ def assertBadSplit(addr):
391391

392392
def test_bad_address_split_v6_too_long(self):
393393
def assertBadSplit(addr):
394-
msg = r"At most 39 characters expected in %s"
395-
with self.assertAddressError(msg, repr(re.escape(addr[:14]))):
394+
msg = r"At most 45 characters expected in '%s"
395+
with self.assertAddressError(msg, re.escape(addr[:45])):
396396
ipaddress.IPv6Address(addr)
397397

398398
# Long IPv6 address
399399
long_addr = ("0:" * 10000) + "0"
400400
assertBadSplit(long_addr)
401401
assertBadSplit(long_addr + "%zoneid")
402+
assertBadSplit(long_addr + ":255.255.255.255")
403+
assertBadSplit(long_addr + ":ffff:255.255.255.255")
402404

403405
def test_bad_address_split_v6_too_many_parts(self):
404406
def assertBadSplit(addr):
@@ -2172,6 +2174,11 @@ def testIPv6AddressTooLarge(self):
21722174
self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1'),
21732175
ipaddress.ip_address('FFFF::c000:201'))
21742176

2177+
self.assertEqual(ipaddress.ip_address('0000:0000:0000:0000:0000:FFFF:192.168.255.255'),
2178+
ipaddress.ip_address('::ffff:c0a8:ffff'))
2179+
self.assertEqual(ipaddress.ip_address('FFFF:0000:0000:0000:0000:0000:192.168.255.255'),
2180+
ipaddress.ip_address('ffff::c0a8:ffff'))
2181+
21752182
self.assertEqual(ipaddress.ip_address('::FFFF:192.0.2.1%scope'),
21762183
ipaddress.ip_address('::FFFF:c000:201%scope'))
21772184
self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1%scope'),
@@ -2184,6 +2191,10 @@ def testIPv6AddressTooLarge(self):
21842191
ipaddress.ip_address('::FFFF:c000:201%scope'))
21852192
self.assertNotEqual(ipaddress.ip_address('FFFF::192.0.2.1'),
21862193
ipaddress.ip_address('FFFF::c000:201%scope'))
2194+
self.assertEqual(ipaddress.ip_address('0000:0000:0000:0000:0000:FFFF:192.168.255.255%scope'),
2195+
ipaddress.ip_address('::ffff:c0a8:ffff%scope'))
2196+
self.assertEqual(ipaddress.ip_address('FFFF:0000:0000:0000:0000:0000:192.168.255.255%scope'),
2197+
ipaddress.ip_address('ffff::c0a8:ffff%scope'))
21872198

21882199
def testIPVersion(self):
21892200
self.assertEqual(self.ipv4_address.version, 4)
@@ -2568,6 +2579,10 @@ def testCompressIPv6Address(self):
25682579
'::7:6:5:4:3:2:0': '0:7:6:5:4:3:2:0/128',
25692580
'7:6:5:4:3:2:1::': '7:6:5:4:3:2:1:0/128',
25702581
'0:6:5:4:3:2:1::': '0:6:5:4:3:2:1:0/128',
2582+
'0000:0000:0000:0000:0000:0000:255.255.255.255': '::ffff:ffff/128',
2583+
'0000:0000:0000:0000:0000:ffff:255.255.255.255': '::ffff:255.255.255.255/128',
2584+
'ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255':
2585+
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128',
25712586
}
25722587
for uncompressed, compressed in list(test_addresses.items()):
25732588
self.assertEqual(compressed, str(ipaddress.IPv6Interface(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix parsing long IPv6 addresses with embedded IPv4 address.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy