Skip to content

Commit 2c6ca1a

Browse files
[3.11] gh-128840: Fix parsing long IPv6 addresses with embedded IPv4 address (GH-134836) (GH-135091)
(cherry picked from commit d83576b) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 9df171d commit 2c6ca1a

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
@@ -1664,10 +1664,12 @@ def _ip_int_from_string(cls, ip_str):
16641664
"""
16651665
if not ip_str:
16661666
raise AddressValueError('Address cannot be empty')
1667-
if len(ip_str) > 39:
1668-
msg = ("At most 39 characters expected in "
1669-
f"{ip_str[:14]!r}({len(ip_str)-28} chars elided){ip_str[-14:]!r}")
1670-
raise AddressValueError(msg)
1667+
if len(ip_str) > 45:
1668+
shorten = ip_str
1669+
if len(shorten) > 100:
1670+
shorten = f'{ip_str[:45]}({len(ip_str)-90} chars elided){ip_str[-45:]}'
1671+
raise AddressValueError(f"At most 45 characters expected in "
1672+
f"{shorten!r}")
16711673

16721674
# We want to allow more parts than the max to be 'split'
16731675
# 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):
@@ -2178,6 +2180,11 @@ def testIPv6AddressTooLarge(self):
21782180
self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1'),
21792181
ipaddress.ip_address('FFFF::c000:201'))
21802182

2183+
self.assertEqual(ipaddress.ip_address('0000:0000:0000:0000:0000:FFFF:192.168.255.255'),
2184+
ipaddress.ip_address('::ffff:c0a8:ffff'))
2185+
self.assertEqual(ipaddress.ip_address('FFFF:0000:0000:0000:0000:0000:192.168.255.255'),
2186+
ipaddress.ip_address('ffff::c0a8:ffff'))
2187+
21812188
self.assertEqual(ipaddress.ip_address('::FFFF:192.0.2.1%scope'),
21822189
ipaddress.ip_address('::FFFF:c000:201%scope'))
21832190
self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1%scope'),
@@ -2190,6 +2197,10 @@ def testIPv6AddressTooLarge(self):
21902197
ipaddress.ip_address('::FFFF:c000:201%scope'))
21912198
self.assertNotEqual(ipaddress.ip_address('FFFF::192.0.2.1'),
21922199
ipaddress.ip_address('FFFF::c000:201%scope'))
2200+
self.assertEqual(ipaddress.ip_address('0000:0000:0000:0000:0000:FFFF:192.168.255.255%scope'),
2201+
ipaddress.ip_address('::ffff:c0a8:ffff%scope'))
2202+
self.assertEqual(ipaddress.ip_address('FFFF:0000:0000:0000:0000:0000:192.168.255.255%scope'),
2203+
ipaddress.ip_address('ffff::c0a8:ffff%scope'))
21932204

21942205
def testIPVersion(self):
21952206
self.assertEqual(self.ipv4_address.version, 4)
@@ -2574,6 +2585,10 @@ def testCompressIPv6Address(self):
25742585
'::7:6:5:4:3:2:0': '0:7:6:5:4:3:2:0/128',
25752586
'7:6:5:4:3:2:1::': '7:6:5:4:3:2:1:0/128',
25762587
'0:6:5:4:3:2:1::': '0:6:5:4:3:2:1:0/128',
2588+
'0000:0000:0000:0000:0000:0000:255.255.255.255': '::ffff:ffff/128',
2589+
'0000:0000:0000:0000:0000:ffff:255.255.255.255': '::ffff:255.255.255.255/128',
2590+
'ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255':
2591+
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128',
25772592
}
25782593
for uncompressed, compressed in list(test_addresses.items()):
25792594
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