Skip to content

Commit

Permalink
bpo-36845: validate integer network prefix when constructing IP netwo…
Browse files Browse the repository at this point in the history
…rks (GH-13298)

(cherry picked from commit 5e48e3d)

Co-authored-by: Nicolai Moore <niconorsk@gmail.com>
  • Loading branch information
2 people authored and methane committed May 14, 2019
1 parent 0a5b88e commit 30cccf0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,8 @@ def _make_netmask(cls, arg):
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
Expand Down Expand Up @@ -1622,6 +1624,8 @@ def _make_netmask(cls, arg):
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))
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,14 @@ def assertBadNetmask(addr, netmask):
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
Expand Down Expand Up @@ -579,6 +587,14 @@ def assertBadNetmask(addr, netmask):
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
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,7 @@ Bastien Montagne
Skip Montanaro
Peter Moody
Alan D. Moore
Nicolai Moore
Paul Moore
Ross Moore
Ben Morgan
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added validation of integer prefixes to the construction of IP networks and
interfaces in the ipaddress module.

0 comments on commit 30cccf0

Please sign in to comment.