Skip to content

Commit a3876f1

Browse files
authored
fix: libwaku's invalid waku message error handling (#3301)
1 parent 091024b commit a3876f1

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

library/events/json_message_event.nim

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ func fromJsonNode*(
2020
T: type JsonMessage, jsonContent: JsonNode
2121
): Result[JsonMessage, string] =
2222
# Visit https://rfc.vac.dev/spec/14/ for further details
23+
24+
# Check if required fields exist
25+
if not jsonContent.hasKey("payload"):
26+
return err("Missing required field in WakuMessage: payload")
27+
if not jsonContent.hasKey("contentTopic"):
28+
return err("Missing required field in WakuMessage: contentTopic")
29+
2330
ok(
2431
JsonMessage(
2532
payload: Base64String(jsonContent["payload"].getStr()),

library/libwaku.nim

+2-3
Original file line numberDiff line numberDiff line change
@@ -291,18 +291,17 @@ proc waku_relay_publish(
291291
checkLibwakuParams(ctx, callback, userData)
292292

293293
let jwm = jsonWakuMessage.alloc()
294+
defer:
295+
deallocShared(jwm)
294296
var jsonMessage: JsonMessage
295297
try:
296298
let jsonContent = parseJson($jwm)
297299
jsonMessage = JsonMessage.fromJsonNode(jsonContent).valueOr:
298300
raise newException(JsonParsingError, $error)
299301
except JsonParsingError:
300-
deallocShared(jwm)
301302
let msg = fmt"Error parsing json message: {getCurrentExceptionMsg()}"
302303
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
303304
return RET_ERR
304-
finally:
305-
deallocShared(jwm)
306305

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

0 commit comments

Comments
 (0)