diff options
author | Nicolai Moore <niconorsk@gmail.com> | 2019-05-14 10:32:59 (GMT) |
---|---|---|
committer | Inada Naoki <songofacandy@gmail.com> | 2019-05-14 10:32:59 (GMT) |
commit | 5e48e3db6f5a937023e99d89cef8884d22bd8533 (patch) | |
tree | a3d3ca95253f60968046c87393d29a86b3ab7ca6 /Lib | |
parent | f0be4bbb9b3cee876249c23f2ae6f38f43fa7495 (diff) | |
download | cpython-5e48e3db6f5a937023e99d89cef8884d22bd8533.zip cpython-5e48e3db6f5a937023e99d89cef8884d22bd8533.tar.gz cpython-5e48e3db6f5a937023e99d89cef8884d22bd8533.tar.bz2 |
bpo-36845: validate integer network prefix when constructing IP networks (GH-13298)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/ipaddress.py | 4 | ||||
-rw-r--r-- | Lib/test/test_ipaddress.py | 16 |
2 files changed, 20 insertions, 0 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 662d737..873c764 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1108,6 +1108,8 @@ class _BaseV4: if arg not in cls._netmask_cache: if isinstance(arg, int): prefixlen = arg + if not (0 <= prefixlen <= cls._max_prefixlen): + cls._report_invalid_netmask(prefixlen) else: try: # Check for a netmask in prefix length form @@ -1538,6 +1540,8 @@ class _BaseV6: if arg not in cls._netmask_cache: if isinstance(arg, int): prefixlen = arg + if not (0 <= prefixlen <= cls._max_prefixlen): + cls._report_invalid_netmask(prefixlen) else: prefixlen = cls._prefix_from_prefix_string(arg) netmask = IPv6Address(cls._ip_int_from_prefix(prefixlen)) diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 20316f1..9e17ea0 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -466,6 +466,14 @@ class NetmaskTestMixin_v4(CommonTestMixin_v4): assertBadNetmask("1.1.1.1", "pudding") assertBadNetmask("1.1.1.1", "::") + def test_netmask_in_tuple_errors(self): + def assertBadNetmask(addr, netmask): + msg = "%r is not a valid netmask" % netmask + with self.assertNetmaskError(re.escape(msg)): + self.factory((addr, netmask)) + assertBadNetmask("1.1.1.1", -1) + assertBadNetmask("1.1.1.1", 33) + def test_pickle(self): self.pickle_test('192.0.2.0/27') self.pickle_test('192.0.2.0/31') # IPV4LENGTH - 1 @@ -588,6 +596,14 @@ class NetmaskTestMixin_v6(CommonTestMixin_v6): assertBadNetmask("::1", "pudding") assertBadNetmask("::", "::") + def test_netmask_in_tuple_errors(self): + def assertBadNetmask(addr, netmask): + msg = "%r is not a valid netmask" % netmask + with self.assertNetmaskError(re.escape(msg)): + self.factory((addr, netmask)) + assertBadNetmask("::1", -1) + assertBadNetmask("::1", 129) + def test_pickle(self): self.pickle_test('2001:db8::1000/124') self.pickle_test('2001:db8::1000/127') # IPV6LENGTH - 1 |