Skip to content
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

Fix utils functions with invalid ip/cidr inputs #6675

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
35 changes: 13 additions & 22 deletions src/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import codecs
import contextlib
import io
import ipaddress
import os
import re
import socket
Expand Down Expand Up @@ -687,11 +688,12 @@ def address_in_network(ip, net):

:rtype: bool
"""
ipaddr = struct.unpack("=L", socket.inet_aton(ip))[0]
netaddr, bits = net.split("/")
netmask = struct.unpack("=L", socket.inet_aton(dotted_netmask(int(bits))))[0]
network = struct.unpack("=L", socket.inet_aton(netaddr))[0] & netmask
return (ipaddr & netmask) == (network & netmask)
try:
ip_address = ipaddress.ip_address(ip)
network = ipaddress.ip_network(net)
return ip_address in network
except (ipaddress.AddressValueError, ValueError):
return False


def dotted_netmask(mask):
Expand All @@ -710,8 +712,8 @@ def is_ipv4_address(string_ip):
:rtype: bool
"""
try:
socket.inet_aton(string_ip)
except OSError:
ipaddress.IPv4Address(string_ip)
except ipaddress.AddressValueError:
return False
return True

Expand All @@ -722,22 +724,11 @@ def is_valid_cidr(string_network):

:rtype: bool
"""
if string_network.count("/") == 1:
try:
mask = int(string_network.split("/")[1])
except ValueError:
return False

if mask < 1 or mask > 32:
return False

try:
socket.inet_aton(string_network.split("/")[0])
except OSError:
return False
else:
try:
interface = ipaddress.ip_interface(string_network)
except (ipaddress.AddressValueError, ValueError):
return False
return True
return string_network in (interface.compressed, interface.exploded)


@contextlib.contextmanager
Expand Down
23 changes: 20 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,14 @@ class TestIsIPv4Address:
def test_valid(self):
assert is_ipv4_address("8.8.8.8")

@pytest.mark.parametrize("value", ("8.8.8.8.8", "localhost.localdomain"))
@pytest.mark.parametrize(
"value",
(
"8.8.8.8.8",
"1.1.1.1 someone was here...",
"localhost.localdomain",
),
)
def test_invalid(self, value):
assert not is_ipv4_address(value)

This comment was marked as spam.


Expand All @@ -274,6 +281,7 @@ def test_valid(self):
"192.168.1.0/128",
"192.168.1.0/-1",
"192.168.1.999/24",
"1.1.1.1 something/24",
),
)
def test_invalid(self, value):
Expand All @@ -284,8 +292,17 @@ class TestAddressInNetwork:
def test_valid(self):
assert address_in_network("192.168.1.1", "192.168.1.0/24")

def test_invalid(self):
assert not address_in_network("172.16.0.1", "192.168.1.0/24")
@pytest.mark.parametrize(
"ip, net",
(
("172.16.0.1", "192.168.1.0/24"),
("1.1.1.1", "1.1.1.1/24"),
("1.1.1.1wtf", "1.1.1.1/24"),
("1.1.1.1 wtf", "1.1.1.1/24"),
),
)
def test_invalid(self, ip, net):
assert not address_in_network(ip, net)


class TestGuessFilename:
Expand Down
Loading