Skip to content

Commit d99b67e

Browse files
committed
now ignoring pubsub messages upon receving invalid-signed-records
1 parent cdfb083 commit d99b67e

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

libp2p/pubsub/gossipsub.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ async def handle_rpc(self, rpc: rpc_pb2.RPC, sender_peer_id: ID) -> None:
229229
"""
230230
# Process the senderRecord if sent
231231
if isinstance(self.pubsub, Pubsub):
232-
_ = maybe_consume_signed_record(rpc, self.pubsub.host)
232+
if not maybe_consume_signed_record(rpc, self.pubsub.host):
233+
logger.error("Received an invalid-signed-record, ignoring the message")
234+
return
233235

234236
control_message = rpc.control
235237

libp2p/pubsub/pubsub.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,11 @@ async def continuously_read_stream(self, stream: INetStream) -> None:
270270
rpc_incoming.ParseFromString(incoming)
271271

272272
# Process the sender's signed-record if sent
273-
_ = maybe_consume_signed_record(rpc_incoming, self.host)
273+
if not maybe_consume_signed_record(rpc_incoming, self.host):
274+
logger.error(
275+
"Received an invalid-signed-record, ignoring the incoming msg"
276+
)
277+
continue
274278

275279
if rpc_incoming.publish:
276280
# deal with RPC.publish

tests/core/pubsub/test_pubsub.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import multiaddr
1212
import trio
1313

14+
from libp2p.crypto.rsa import create_new_key_pair
1415
from libp2p.custom_types import AsyncValidatorFn
1516
from libp2p.exceptions import (
1617
ValidationError,
@@ -162,6 +163,27 @@ async def test_peers_subscribe():
162163
assert envelope_b_sub.record().seq == envelope_b_unsub.record().seq
163164

164165

166+
@pytest.mark.trio
167+
async def test_peer_subscribe_fail_upon_invald_record_transfer():
168+
async with PubsubFactory.create_batch_with_floodsub(2) as pubsubs_fsub:
169+
await connect(pubsubs_fsub[0].host, pubsubs_fsub[1].host)
170+
171+
# Corrupt host_a's local peer record
172+
envelope = pubsubs_fsub[0].host.get_peerstore().get_local_record()
173+
key_pair = create_new_key_pair()
174+
175+
if envelope is not None:
176+
envelope.public_key = key_pair.public_key
177+
pubsubs_fsub[0].host.get_peerstore().set_local_record(envelope)
178+
179+
await pubsubs_fsub[0].subscribe(TESTING_TOPIC)
180+
# Yeild to let 0 notify 1
181+
await trio.sleep(1)
182+
assert pubsubs_fsub[0].my_id not in pubsubs_fsub[1].peer_topics.get(
183+
TESTING_TOPIC, set()
184+
)
185+
186+
165187
@pytest.mark.trio
166188
async def test_get_hello_packet():
167189
async with PubsubFactory.create_batch_with_floodsub(1) as pubsubs_fsub:

0 commit comments

Comments
 (0)