Skip to content

[3.7] bpo-36845: validate integer network prefix when constructing IP networks (GH-13298) #13309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.