Skip to content

Commit

Permalink
Validate pubsub subscriptions (#627)
Browse files Browse the repository at this point in the history
* Check topic before subscribing
* Block subscribe to invalid topics
  • Loading branch information
Menduist authored Jan 14, 2022
1 parent e72d03b commit 1a97d0a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions libp2p/protocols/pubsub/pubsub.nim
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ proc subscribe*(p: PubSub,
## on every received message
##

# Check that this is an allowed topic
if p.subscriptionValidator != nil and p.subscriptionValidator(topic) == false:
warn "Trying to subscribe to a topic not passing validation!", topic
return

p.topics.withValue(topic, handlers) do:
# Already subscribed, just adding another handler
handlers[].add(handler)
Expand Down
4 changes: 3 additions & 1 deletion tests/pubsub/testgossipsub2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ suite "GossipSub":
nodes[1].start(),
))

# We must subscribe before setting the validator
nodes[0].subscribe("foobar", handler)

var gossip = GossipSub(nodes[0])
let invalidDetected = newFuture[void]()
gossip.subscriptionValidator =
Expand All @@ -162,7 +165,6 @@ suite "GossipSub":

await subscribeNodes(nodes)

nodes[0].subscribe("foobar", handler)
nodes[1].subscribe("foobar", handler)

await invalidDetected.wait(10.seconds)
Expand Down

0 comments on commit 1a97d0a

Please sign in to comment.