From 05a28ea346a8455e57cf132c8a73f5d4872183e0 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Tue, 1 Oct 2019 18:22:19 -0300 Subject: [PATCH] handle attempts to add peers with no node id and issue a warning --- lbry/lbry/dht/protocol/protocol.py | 3 +++ lbry/tests/unit/dht/protocol/test_routing_table.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/lbry/lbry/dht/protocol/protocol.py b/lbry/lbry/dht/protocol/protocol.py index 2eecc5893..838afb2db 100644 --- a/lbry/lbry/dht/protocol/protocol.py +++ b/lbry/lbry/dht/protocol/protocol.py @@ -323,6 +323,9 @@ class KademliaProtocol(DatagramProtocol): return args, {} async def _add_peer(self, peer: 'KademliaPeer'): + if not peer.node_id: + log.warning("Tried adding a peer with no node id!") + return False for p in self.routing_table.get_peers(): if (p.address, p.udp_port) == (peer.address, peer.udp_port) and p.node_id != peer.node_id: self.routing_table.remove_peer(p) diff --git a/lbry/tests/unit/dht/protocol/test_routing_table.py b/lbry/tests/unit/dht/protocol/test_routing_table.py index 467423ac9..c2d017302 100644 --- a/lbry/tests/unit/dht/protocol/test_routing_table.py +++ b/lbry/tests/unit/dht/protocol/test_routing_table.py @@ -28,6 +28,7 @@ expected_ranges = [ ) ] + class TestRouting(AsyncioTestCase): async def test_fill_one_bucket(self): loop = asyncio.get_event_loop() @@ -65,6 +66,16 @@ class TestRouting(AsyncioTestCase): for node in nodes.values(): node.protocol.stop() + async def test_cant_add_peer_without_a_node_id_gracefully(self): + loop = asyncio.get_event_loop() + node = Node(loop, PeerManager(loop), constants.generate_id(), 4444, 4444, 3333, '1.2.3.4') + bad_peer = make_kademlia_peer(None, '1.2.3.4', 5555) + with self.assertLogs(level='WARNING') as logged: + self.assertFalse(await node.protocol._add_peer(bad_peer)) + self.assertEqual(1, len(logged.output)) + self.assertTrue(logged.output[0].endswith('Tried adding a peer with no node id!')) + + async def test_split_buckets(self): loop = asyncio.get_event_loop() peer_addresses = [