Skip to content

Commit

Permalink
Fix URL encoding for pubsubTopic and contentTopics parameters
Browse files Browse the repository at this point in the history
Related to waku-org#3128

Update the API to enforce mandatory URL encoding for `pubsubTopic` and `content_topic`.

* Update `docs/api/rest-api.md` to include examples of URL-encoded `pubsubTopic` and `contentTopics` parameters.
* Modify `waku/waku_api/rest/store/handlers.nim` to validate and enforce URL encoding for `pubsubTopic` and `contentTopics` parameters.
* Add error handling for invalid or non-encoded `pubsubTopic` and `contentTopics` parameters in `waku/waku_api/rest/store/handlers.nim`.
* Update `decodeRequestBody` function in `waku/waku_api/rest/rest_serdes.nim` to validate and enforce URL encoding for `pubsubTopic` and `contentTopics` parameters.
* Add error handling for invalid or non-encoded `pubsubTopic` and `contentTopics` parameters in `waku/waku_api/rest/rest_serdes.nim`.
  • Loading branch information
vishwamartur committed Dec 6, 2024
1 parent 1b532e8 commit 6aa62ac
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/api/rest-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ A particular OpenAPI spec can be easily imported into [Postman](https://www.post
curl http://localhost:8645/debug/v1/info -s | jq
```

#### [`get_waku_v2_store_v3_messages`](https://rfc.vac.dev/spec/16/#get_waku_v2_store_v3_messages)

```bash
curl -v -X GET "http://127.0.0.1:49153/store/v3/messages?includeData=true&pubsubTopic=/waku/2/rs/3/0&pageSize=20&ascending=true"
```

or call it encoded

```bash
curl -v -X GET "http://127.0.0.1:5213/store/v3/messages?includeData=true&pubsubTopic=%2Fwaku%2F2%2Frs%2F3%2F0&pageSize=20&ascending=true"
```

In both cases, it works and retrieves the message with the correct topic name.

### Node configuration
Find details [here](https://github.com/waku-org/nwaku/tree/master/docs/operators/how-to/configure-rest-api.md)
12 changes: 12 additions & 0 deletions waku/waku_api/rest/rest_serdes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ func decodeRequestBody*[T](
)
)

# Validate and enforce URL encoding for pubsubTopic and contentTopics
if T.hasKey("pubsubTopic"):
let pubsubTopic = T["pubsubTopic"]
if pubsubTopic != encodeUrl(pubsubTopic):
return err(RestApiResponse.badRequest("Invalid or non-encoded pubsubTopic parameter"))

if T.hasKey("contentTopics"):
let contentTopics = T["contentTopics"]
for topic in contentTopics:
if topic != encodeUrl(topic):
return err(RestApiResponse.badRequest("Invalid or non-encoded content_topic parameter"))

return ok(requestResult.get())

proc decodeBytes*(
Expand Down
4 changes: 4 additions & 0 deletions waku/waku_api/rest/store/handlers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,17 @@ proc createStoreQuery(
let decodedPubsubTopic = decodeUrl(pubsubTopic.get())
if decodedPubsubTopic != "":
parsedPubsubTopic = some(decodedPubsubTopic)
else:
return err("Invalid or non-encoded pubsubTopic parameter")

# Parse the content topics
var parsedContentTopics = newSeq[ContentTopic](0)
if contentTopics.isSome():
let ctList = decodeUrl(contentTopics.get())
if ctList != "":
for ct in ctList.split(','):
if ct == "":
return err("Invalid or non-encoded content_topic parameter")
parsedContentTopics.add(ct)

# Parse start time
Expand Down

0 comments on commit 6aa62ac

Please sign in to comment.