Skip to content

Commit a36601a

Browse files
fix: Do not allow invalid pubsub topic subscription via relay REST api (#3559)
* Check input pubsub topics for REST /relay/v1/subscriptions endpoint
1 parent 82926f9 commit a36601a

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

tests/wakunode_rest/test_rest_relay.nim

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ suite "Waku v2 Rest API - Relay":
4444
assert false, "Failed to mount relay"
4545

4646
var restPort = Port(0)
47+
4748
let restAddress = parseIpAddress("0.0.0.0")
4849
let restServer = WakuRestServerRef.init(restAddress, restPort).tryGet()
4950

@@ -61,8 +62,23 @@ suite "Waku v2 Rest API - Relay":
6162

6263
let shards = @[$shard0, $shard1, $shard2]
6364

64-
# When
65+
let invalidTopic = "/test/2/this/is/a/content/topic/1"
66+
67+
var containsIncorrect = shards
68+
containsIncorrect.add(invalidTopic)
69+
70+
# When contains incorrect pubsub topics, subscribe shall fail
6571
let client = newRestHttpClient(initTAddress(restAddress, restPort))
72+
let errorResponse = await client.relayPostSubscriptionsV1(containsIncorrect)
73+
74+
# Then
75+
check:
76+
errorResponse.status == 400
77+
$errorResponse.contentType == $MIMETYPE_TEXT
78+
errorResponse.data ==
79+
"Invalid pubsub topic(s): @[\"/test/2/this/is/a/content/topic/1\"]"
80+
81+
# when all pubsub topics are correct, subscribe shall succeed
6682
let response = await client.relayPostSubscriptionsV1(shards)
6783

6884
# Then

waku/node/waku_node.nim

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ proc getShardsGetter(node: WakuNode): GetShards =
129129
# fetch pubsubTopics subscribed to relay and convert them to shards
130130
if node.wakuRelay.isNil():
131131
return @[]
132-
let subTopics = node.wakuRelay.subscribedTopics()
133-
let relayShards = topicsToRelayShards(subTopics).valueOr:
134-
error "could not convert relay topics to shards", error = $error
132+
let subscribedTopics = node.wakuRelay.subscribedTopics()
133+
let relayShards = topicsToRelayShards(subscribedTopics).valueOr:
134+
error "could not convert relay topics to shards",
135+
error = $error, topics = subscribedTopics
135136
return @[]
136137
if relayShards.isSome():
137138
let shards = relayShards.get().shardIds

waku/waku_api/rest/relay/handlers.nim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ const ROUTE_RELAY_AUTO_SUBSCRIPTIONSV1* = "/relay/v1/auto/subscriptions"
4141
const ROUTE_RELAY_AUTO_MESSAGESV1* = "/relay/v1/auto/messages/{contentTopic}"
4242
const ROUTE_RELAY_AUTO_MESSAGESV1_NO_TOPIC* = "/relay/v1/auto/messages"
4343

44+
proc validatePubSubTopics(topics: seq[PubsubTopic]): Result[void, RestApiResponse] =
45+
let badPubSubTopics = topics.filterIt(RelayShard.parseStaticSharding(it).isErr())
46+
if badPubSubTopics.len > 0:
47+
error "Invalid pubsub topic(s)", PubSubTopics = $badPubSubTopics
48+
return
49+
err(RestApiResponse.badRequest("Invalid pubsub topic(s): " & $badPubSubTopics))
50+
51+
return ok()
52+
4453
proc installRelayApiHandlers*(
4554
router: var RestRouter, node: WakuNode, cache: MessageCache
4655
) =
@@ -61,6 +70,9 @@ proc installRelayApiHandlers*(
6170
let req: seq[PubsubTopic] = decodeRequestBody[seq[PubsubTopic]](contentBody).valueOr:
6271
return error
6372

73+
validatePubSubTopics(req).isOkOr:
74+
return error
75+
6476
# Only subscribe to topics for which we have no subscribed topic handlers yet
6577
let newTopics = req.filterIt(not cache.isPubsubSubscribed(it))
6678

@@ -87,6 +99,9 @@ proc installRelayApiHandlers*(
8799
let req: seq[PubsubTopic] = decodeRequestBody[seq[PubsubTopic]](contentBody).valueOr:
88100
return error
89101

102+
validatePubSubTopics(req).isOkOr:
103+
return error
104+
90105
# Unsubscribe all handlers from requested topics
91106
for pubsubTopic in req:
92107
cache.pubsubUnsubscribe(pubsubTopic)

0 commit comments

Comments
 (0)