Skip to content

Commit fcf3ea0

Browse files
miss-islingtonserhiy-storchakaambv
authored
[3.12] gh-128840: Fix parsing long IPv6 addresses with embedded IPv4 address (GH-134836) (GH-134847)
(cherry picked from commit d83576b) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
1 parent 3217780 commit fcf3ea0

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):
@@ -2181,6 +2183,11 @@ def testIPv6AddressTooLarge(self):
21812183
self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1'),
21822184
ipaddress.ip_address('FFFF::c000:201'))
21832185

2186+
self.assertEqual(ipaddress.ip_address('0000:0000:0000:0000:0000:FFFF:192.168.255.255'),
2187+
ipaddress.ip_address('::ffff:c0a8:ffff'))
2188+
self.assertEqual(ipaddress.ip_address('FFFF:0000:0000:0000:0000:0000:192.168.255.255'),
2189+
ipaddress.ip_address('ffff::c0a8:ffff'))
2190+
21842191
self.assertEqual(ipaddress.ip_address('::FFFF:192.0.2.1%scope'),
21852192
ipaddress.ip_address('::FFFF:c000:201%scope'))
21862193
self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1%scope'),
@@ -2193,6 +2200,10 @@ def testIPv6AddressTooLarge(self):
21932200
ipaddress.ip_address('::FFFF:c000:201%scope'))
21942201
self.assertNotEqual(ipaddress.ip_address('FFFF::192.0.2.1'),
21952202
ipaddress.ip_address('FFFF::c000:201%scope'))
2203+
self.assertEqual(ipaddress.ip_address('0000:0000:0000:0000:0000:FFFF:192.168.255.255%scope'),
2204+
ipaddress.ip_address('::ffff:c0a8:ffff%scope'))
2205+
self.assertEqual(ipaddress.ip_address('FFFF:0000:0000:0000:0000:0000:192.168.255.255%scope'),
2206+
ipaddress.ip_address('ffff::c0a8:ffff%scope'))
21962207

21972208
def testIPVersion(self):
21982209
self.assertEqual(self.ipv4_address.version, 4)
@@ -2596,6 +2607,10 @@ def testCompressIPv6Address(self):
25962607
'::7:6:5:4:3:2:0': '0:7:6:5:4:3:2:0/128',
25972608
'7:6:5:4:3:2:1::': '7:6:5:4:3:2:1:0/128',
25982609
'0:6:5:4:3:2:1::': '0:6:5:4:3:2:1:0/128',
2610+
'0000:0000:0000:0000:0000:0000:255.255.255.255': '::ffff:ffff/128',
2611+
'0000:0000:0000:0000:0000:ffff:255.255.255.255': '::ffff:255.255.255.255/128',
2612+
'ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255':
2613+
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128',
25992614
}
26002615
for uncompressed, compressed in list(test_addresses.items()):
26012616
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