fix: libwaku's invalid waku message error handling (#3301)

This commit is contained in:
gabrielmer 2025-02-17 18:37:43 +02:00 committed by GitHub
parent 9bb567eb0e
commit 8275d70f35
2 changed files with 9 additions and 3 deletions

View File

@ -20,6 +20,13 @@ func fromJsonNode*(
T: type JsonMessage, jsonContent: JsonNode T: type JsonMessage, jsonContent: JsonNode
): Result[JsonMessage, string] = ): Result[JsonMessage, string] =
# Visit https://rfc.vac.dev/spec/14/ for further details # 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( ok(
JsonMessage( JsonMessage(
payload: Base64String(jsonContent["payload"].getStr()), payload: Base64String(jsonContent["payload"].getStr()),

View File

@ -291,18 +291,17 @@ proc waku_relay_publish(
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let jwm = jsonWakuMessage.alloc() let jwm = jsonWakuMessage.alloc()
defer:
deallocShared(jwm)
var jsonMessage: JsonMessage var jsonMessage: JsonMessage
try: try:
let jsonContent = parseJson($jwm) let jsonContent = parseJson($jwm)
jsonMessage = JsonMessage.fromJsonNode(jsonContent).valueOr: jsonMessage = JsonMessage.fromJsonNode(jsonContent).valueOr:
raise newException(JsonParsingError, $error) raise newException(JsonParsingError, $error)
except JsonParsingError: except JsonParsingError:
deallocShared(jwm)
let msg = fmt"Error parsing json message: {getCurrentExceptionMsg()}" let msg = fmt"Error parsing json message: {getCurrentExceptionMsg()}"
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData) callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR return RET_ERR
finally:
deallocShared(jwm)
let wakuMessage = jsonMessage.toWakuMessage().valueOr: let wakuMessage = jsonMessage.toWakuMessage().valueOr:
let msg = "Problem building the WakuMessage: " & $error let msg = "Problem building the WakuMessage: " & $error