From cc390863d42e86b1eebfb3ddceb7fce9db6a252d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Capparelli?= Date: Wed, 27 Mar 2024 02:48:21 -0300 Subject: [PATCH 1/6] Add tests in utils.address_in_network --- tests/test_utils.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 5e9b56ea64..d7a034e1f3 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -284,8 +284,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: From ec654e121c45ea83780c430821ef55d194a9a2b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Capparelli?= Date: Wed, 27 Mar 2024 03:01:10 -0300 Subject: [PATCH 2/6] Fix utils.address_in_network --- src/requests/utils.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/requests/utils.py b/src/requests/utils.py index ae6c42f6cb..4197157369 100644 --- a/src/requests/utils.py +++ b/src/requests/utils.py @@ -9,6 +9,7 @@ import codecs import contextlib import io +import ipaddress import os import re import socket @@ -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): From 44a9edd4f9099e7027b40c0a299ef4a08cd4cf2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Capparelli?= Date: Wed, 27 Mar 2024 03:11:42 -0300 Subject: [PATCH 3/6] Add tests in utils.is_ipv4_address --- tests/test_utils.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index d7a034e1f3..c9434d1fbf 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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) From 8049b1e2e8544965e086ed48bd18ba407ecc6166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Capparelli?= Date: Wed, 27 Mar 2024 03:37:48 -0300 Subject: [PATCH 4/6] Fix utils.is_ipv4_address --- src/requests/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/requests/utils.py b/src/requests/utils.py index 4197157369..bfbdd4cd6d 100644 --- a/src/requests/utils.py +++ b/src/requests/utils.py @@ -712,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 From 20c158c3722c242998be148a108e771e73158241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Capparelli?= Date: Wed, 27 Mar 2024 03:42:22 -0300 Subject: [PATCH 5/6] Add test in utils.is_valid_cidr --- tests/test_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index c9434d1fbf..31f7416441 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -281,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): From 08217cb1adb488cf26068e9098b2f7388e874cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Capparelli?= Date: Wed, 27 Mar 2024 04:14:58 -0300 Subject: [PATCH 6/6] Fix utils.is_valid_cidr --- src/requests/utils.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/requests/utils.py b/src/requests/utils.py index bfbdd4cd6d..5e7f80be5f 100644 --- a/src/requests/utils.py +++ b/src/requests/utils.py @@ -724,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