Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: URL encoding for pubsubTopic and contentTopics parameters #3200

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
vishwamartur marked this conversation as resolved.
Show resolved Hide resolved
```

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-URL-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-URL-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
Loading