From 32c8e43ef1e859fa55af2f470a2a1120f51afabe Mon Sep 17 00:00:00 2001 From: Amine Yaiche Date: Tue, 22 Mar 2016 19:39:09 +0100 Subject: [PATCH] Fixed #26378 -- Allowed a left byte of zero in mixed IPv4/IPv6 validation. --- django/utils/ipv6.py | 3 ++- tests/utils_tests/test_ipv6.py | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/django/utils/ipv6.py b/django/utils/ipv6.py index 4f433d198b..819b6a299a 100644 --- a/django/utils/ipv6.py +++ b/django/utils/ipv6.py @@ -54,7 +54,8 @@ def clean_ipv6_address(ip_str, unpack_ipv4=False, for index in range(len(hextets)): # Remove leading zeroes - hextets[index] = hextets[index].lstrip('0') + if '.' not in hextets[index]: + hextets[index] = hextets[index].lstrip('0') if not hextets[index]: hextets[index] = '0' diff --git a/tests/utils_tests/test_ipv6.py b/tests/utils_tests/test_ipv6.py index 57bf1d826b..b280940c3c 100644 --- a/tests/utils_tests/test_ipv6.py +++ b/tests/utils_tests/test_ipv6.py @@ -50,6 +50,8 @@ class TestUtilsIPv6(unittest.TestCase): self.assertEqual(clean_ipv6_address('::ffff:0a0a:0a0a'), '::ffff:10.10.10.10') self.assertEqual(clean_ipv6_address('::ffff:1234:1234'), '::ffff:18.52.18.52') self.assertEqual(clean_ipv6_address('::ffff:18.52.18.52'), '::ffff:18.52.18.52') + self.assertEqual(clean_ipv6_address('::ffff:0.52.18.52'), '::ffff:0.52.18.52') + self.assertEqual(clean_ipv6_address('::ffff:0.0.0.0'), '::ffff:0.0.0.0') def test_unpacks_ipv4(self): self.assertEqual(clean_ipv6_address('::ffff:0a0a:0a0a', unpack_ipv4=True), '10.10.10.10')