Skip to content

Commit

Permalink
fix: libwaku's invalid waku message error handling (#3301)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer authored Feb 17, 2025
1 parent 091024b commit a3876f1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 7 additions & 0 deletions library/events/json_message_event.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func fromJsonNode*(
T: type JsonMessage, jsonContent: JsonNode
): Result[JsonMessage, string] =
# Visit https://rfc.vac.dev/spec/14/ for further details

# Check if required fields exist
if not jsonContent.hasKey("payload"):
return err("Missing required field in WakuMessage: payload")
if not jsonContent.hasKey("contentTopic"):
return err("Missing required field in WakuMessage: contentTopic")

ok(
JsonMessage(
payload: Base64String(jsonContent["payload"].getStr()),
Expand Down
5 changes: 2 additions & 3 deletions library/libwaku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -291,18 +291,17 @@ proc waku_relay_publish(
checkLibwakuParams(ctx, callback, userData)

let jwm = jsonWakuMessage.alloc()
defer:
deallocShared(jwm)
var jsonMessage: JsonMessage
try:
let jsonContent = parseJson($jwm)
jsonMessage = JsonMessage.fromJsonNode(jsonContent).valueOr:
raise newException(JsonParsingError, $error)
except JsonParsingError:
deallocShared(jwm)
let msg = fmt"Error parsing json message: {getCurrentExceptionMsg()}"
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
finally:
deallocShared(jwm)

let wakuMessage = jsonMessage.toWakuMessage().valueOr:
let msg = "Problem building the WakuMessage: " & $error
Expand Down

0 comments on commit a3876f1

Please sign in to comment.