From 8586762ddeeb89dffb9cffb213e2a304e0571c99 Mon Sep 17 00:00:00 2001 From: endes123321 Date: Sun, 29 Mar 2020 16:12:36 +0100 Subject: [PATCH 1/5] fixed is_valid_public_ipv4 --- lbry/dht/peer.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lbry/dht/peer.py b/lbry/dht/peer.py index 19fbb92e3..5405caac6 100644 --- a/lbry/dht/peer.py +++ b/lbry/dht/peer.py @@ -33,10 +33,10 @@ def is_valid_public_ipv4(address, allow_localhost: bool = False): parsed_ip = ipaddress.ip_address(address) if parsed_ip.is_loopback and allow_localhost: return True - return not any((parsed_ip.version != 4, parsed_ip.is_unspecified, parsed_ip.is_link_local, - parsed_ip.is_loopback, parsed_ip.is_multicast, parsed_ip.is_reserved, parsed_ip.is_private, - parsed_ip.is_reserved, - CARRIER_GRADE_NAT_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")), + if parsed_ip.version != 4 or parsed_ip.is_unspecified or parsed_ip.is_link_local or parsed_ip.is_loopback or parsed_ip.is_multicast or parsed_ip.is_reserved or parsed_ip.is_private or parsed_ip.is_reserved: + return False + else: + return not any((CARRIER_GRADE_NAT_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")), IPV4_TO_6_RELAY_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")))) except ipaddress.AddressValueError: return False From 8b6dd9f603fbd71f6a81a1b071b35cf908240b23 Mon Sep 17 00:00:00 2001 From: endes Date: Sun, 19 Apr 2020 19:51:03 +0100 Subject: [PATCH 2/5] better lint style --- lbry/dht/peer.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lbry/dht/peer.py b/lbry/dht/peer.py index 5405caac6..44c659771 100644 --- a/lbry/dht/peer.py +++ b/lbry/dht/peer.py @@ -33,11 +33,13 @@ def is_valid_public_ipv4(address, allow_localhost: bool = False): parsed_ip = ipaddress.ip_address(address) if parsed_ip.is_loopback and allow_localhost: return True - if parsed_ip.version != 4 or parsed_ip.is_unspecified or parsed_ip.is_link_local or parsed_ip.is_loopback or parsed_ip.is_multicast or parsed_ip.is_reserved or parsed_ip.is_private or parsed_ip.is_reserved: - return False + + if any(parsed_ip.version != 4, parsed_ip.is_unspecified, parsed_ip.is_link_local, parsed_ip.is_loopback, + parsed_ip.is_multicast, parsed_ip.is_reserved, parsed_ip.is_private, parsed_ip.is_reserved): + return False else: - return not any((CARRIER_GRADE_NAT_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")), - IPV4_TO_6_RELAY_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")))) + return not any((CARRIER_GRADE_NAT_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")), + IPV4_TO_6_RELAY_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")))) except ipaddress.AddressValueError: return False From 5a4b6be97459d256bea448254ecafec9d06eedb4 Mon Sep 17 00:00:00 2001 From: endes Date: Sun, 19 Apr 2020 20:14:21 +0100 Subject: [PATCH 3/5] fix --- lbry/dht/peer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lbry/dht/peer.py b/lbry/dht/peer.py index 44c659771..26f9581fd 100644 --- a/lbry/dht/peer.py +++ b/lbry/dht/peer.py @@ -34,8 +34,8 @@ def is_valid_public_ipv4(address, allow_localhost: bool = False): if parsed_ip.is_loopback and allow_localhost: return True - if any(parsed_ip.version != 4, parsed_ip.is_unspecified, parsed_ip.is_link_local, parsed_ip.is_loopback, - parsed_ip.is_multicast, parsed_ip.is_reserved, parsed_ip.is_private, parsed_ip.is_reserved): + if any((parsed_ip.version != 4, parsed_ip.is_unspecified, parsed_ip.is_link_local, parsed_ip.is_loopback, + parsed_ip.is_multicast, parsed_ip.is_reserved, parsed_ip.is_private, parsed_ip.is_reserved)): return False else: return not any((CARRIER_GRADE_NAT_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")), From 8269d2f83cb6c1e436204bb3c52d1a9f9059de35 Mon Sep 17 00:00:00 2001 From: endes Date: Sun, 19 Apr 2020 20:20:19 +0100 Subject: [PATCH 4/5] better lint style --- lbry/dht/peer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lbry/dht/peer.py b/lbry/dht/peer.py index 26f9581fd..7de9e5ef1 100644 --- a/lbry/dht/peer.py +++ b/lbry/dht/peer.py @@ -35,7 +35,7 @@ def is_valid_public_ipv4(address, allow_localhost: bool = False): return True if any((parsed_ip.version != 4, parsed_ip.is_unspecified, parsed_ip.is_link_local, parsed_ip.is_loopback, - parsed_ip.is_multicast, parsed_ip.is_reserved, parsed_ip.is_private, parsed_ip.is_reserved)): + parsed_ip.is_multicast, parsed_ip.is_reserved, parsed_ip.is_private, parsed_ip.is_reserved)): return False else: return not any((CARRIER_GRADE_NAT_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")), From f030d41dc731b7559bf78d59cfa058e31cfd87c5 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 2 Jun 2020 15:56:57 -0400 Subject: [PATCH 5/5] add test_is_valid_ipv4 --- lbry/dht/peer.py | 2 +- tests/unit/dht/test_peer.py | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lbry/dht/peer.py b/lbry/dht/peer.py index 7de9e5ef1..bf2c0deab 100644 --- a/lbry/dht/peer.py +++ b/lbry/dht/peer.py @@ -40,7 +40,7 @@ def is_valid_public_ipv4(address, allow_localhost: bool = False): else: return not any((CARRIER_GRADE_NAT_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")), IPV4_TO_6_RELAY_SUBNET.supernet_of(ipaddress.ip_network(f"{address}/32")))) - except ipaddress.AddressValueError: + except (ipaddress.AddressValueError, ValueError): return False diff --git a/tests/unit/dht/test_peer.py b/tests/unit/dht/test_peer.py index 8bf5d70a9..8a61dad14 100644 --- a/tests/unit/dht/test_peer.py +++ b/tests/unit/dht/test_peer.py @@ -1,7 +1,7 @@ import asyncio import unittest from lbry.utils import generate_id -from lbry.dht.peer import PeerManager, make_kademlia_peer +from lbry.dht.peer import PeerManager, make_kademlia_peer, is_valid_public_ipv4 from lbry.testcase import AsyncioTestCase @@ -50,6 +50,31 @@ class PeerTest(AsyncioTestCase): for i in range(32): self.assertRaises(ValueError, make_kademlia_peer, self.node_ids[1], f"{224 + i}.0.0.0", 1000) self.assertRaises(ValueError, make_kademlia_peer, self.node_ids[1], '255.255.255.255', 1000) + self.assertRaises( + ValueError, make_kademlia_peer, self.node_ids[1], 'beee:eeee:eeee:eeee:eeee:eeee:eeee:eeef', 1000 + ) + self.assertRaises( + ValueError, make_kademlia_peer, self.node_ids[1], '2001:db8::ff00:42:8329', 1000 + ) + + def test_is_valid_ipv4(self): + self.assertFalse(is_valid_public_ipv4('beee:eeee:eeee:eeee:eeee:eeee:eeee:eeef')) + self.assertFalse(is_valid_public_ipv4('beee:eeee:eeee:eeee:eeee:eeee:eeee:eeef', True)) + + self.assertFalse(is_valid_public_ipv4('2001:db8::ff00:42:8329')) + self.assertFalse(is_valid_public_ipv4('2001:db8::ff00:42:8329', True)) + + self.assertFalse(is_valid_public_ipv4('127.0.0.1')) + self.assertTrue(is_valid_public_ipv4('127.0.0.1', True)) + + self.assertFalse(is_valid_public_ipv4('172.16.0.1')) + self.assertFalse(is_valid_public_ipv4('172.16.0.1', True)) + + self.assertTrue(is_valid_public_ipv4('1.2.3.4')) + self.assertTrue(is_valid_public_ipv4('1.2.3.4', True)) + + self.assertFalse(is_valid_public_ipv4('derp')) + self.assertFalse(is_valid_public_ipv4('derp', True)) def test_boolean(self): self.assertNotEqual(self.first_contact, self.second_contact)