Validate pubsub subscriptions (#627)

* Check topic before subscribing
* Block subscribe to invalid topics
This commit is contained in:
Tanguy 2022-01-14 19:40:30 +01:00 committed by GitHub
parent e72d03bc78
commit 1a97d0a2f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View File

@ -452,6 +452,11 @@ proc subscribe*(p: PubSub,
## on every received message ## 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: p.topics.withValue(topic, handlers) do:
# Already subscribed, just adding another handler # Already subscribed, just adding another handler
handlers[].add(handler) handlers[].add(handler)

View File

@ -147,6 +147,9 @@ suite "GossipSub":
nodes[1].start(), nodes[1].start(),
)) ))
# We must subscribe before setting the validator
nodes[0].subscribe("foobar", handler)
var gossip = GossipSub(nodes[0]) var gossip = GossipSub(nodes[0])
let invalidDetected = newFuture[void]() let invalidDetected = newFuture[void]()
gossip.subscriptionValidator = gossip.subscriptionValidator =
@ -162,7 +165,6 @@ suite "GossipSub":
await subscribeNodes(nodes) await subscribeNodes(nodes)
nodes[0].subscribe("foobar", handler)
nodes[1].subscribe("foobar", handler) nodes[1].subscribe("foobar", handler)
await invalidDetected.wait(10.seconds) await invalidDetected.wait(10.seconds)