From 0e9510b505cbd7ac1b5760de842de5a10d879734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 13 Feb 2019 21:44:37 +0100 Subject: [PATCH 1/3] bpo-35989: Forbid a netmask > 32 for ipaddress.IPv4Network --- Lib/ipaddress.py | 8 +++++++- Lib/test/test_ipaddress.py | 2 ++ .../next/Library/2019-02-13-21-43-31.bpo-35989.8L9aBw.rst | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-02-13-21-43-31.bpo-35989.8L9aBw.rst diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 15507d61dec8f9..da91fc86702dbc 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1519,7 +1519,13 @@ def __init__(self, address, strict=True): # Constructing from a tuple (addr, [mask]) elif isinstance(address, tuple): addr = address[0] - mask = address[1] if len(address) > 1 else self._max_prefixlen + if len(address) > 1: + if isinstance(address[1], int) and 0 < address[1] > 32: + raise NetmaskValueError("The netmask should be isn't valid") + mask = address[1] + else: + mask = self._max_prefixlen + #mask = address[1] if len(address) > 1 else self._max_prefixlen # Assume input argument to be string or any object representation # which converts into a formatted IP prefix string. else: diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 0e0753f34c4905..373eb5e828aedf 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -839,6 +839,8 @@ def testIPv4Tuple(self): # strict=True and host bits set with self.assertRaises(ValueError): ipaddress.IPv4Network(('192.0.2.1', 24)) + with self.assertRaises(ValueError): + ipaddress.IPv4Network(('192.0.2.1', 33)) with self.assertRaises(ValueError): ipaddress.IPv4Network((ip, 24)) with self.assertRaises(ValueError): diff --git a/Misc/NEWS.d/next/Library/2019-02-13-21-43-31.bpo-35989.8L9aBw.rst b/Misc/NEWS.d/next/Library/2019-02-13-21-43-31.bpo-35989.8L9aBw.rst new file mode 100644 index 00000000000000..810a4828e12e08 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-02-13-21-43-31.bpo-35989.8L9aBw.rst @@ -0,0 +1,2 @@ +IPv4Network raises a ``NetmaskValueError`` exception if the netmask is not +between 0 and 32. Contributed by Stéphane Wirtel. From 6b6478f7066fecf54cde95b2592946d198636ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Thu, 14 Feb 2019 09:25:22 +0100 Subject: [PATCH 2/3] Fix the message when the Netmask is not valid --- Lib/ipaddress.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index da91fc86702dbc..2f0eacbf07fdb9 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1521,7 +1521,7 @@ def __init__(self, address, strict=True): addr = address[0] if len(address) > 1: if isinstance(address[1], int) and 0 < address[1] > 32: - raise NetmaskValueError("The netmask should be isn't valid") + raise NetmaskValueError(f"The netmask is not valid {address[1]}") mask = address[1] else: mask = self._max_prefixlen From 0d24edfa0c4b30cac36a5c282b7576bf2dced6c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Sat, 23 Mar 2019 01:20:56 +0100 Subject: [PATCH 3/3] =?UTF-8?q?Update=20with=20the=20recommendations=20of?= =?UTF-8?q?=20R=C3=A9mi=20Lapeyre?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/ipaddress.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 2f0eacbf07fdb9..169bf9ba7432fe 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1520,7 +1520,7 @@ def __init__(self, address, strict=True): elif isinstance(address, tuple): addr = address[0] if len(address) > 1: - if isinstance(address[1], int) and 0 < address[1] > 32: + if isinstance(address[1], int) and address[1] > 32: raise NetmaskValueError(f"The netmask is not valid {address[1]}") mask = address[1] else: