chore: handle currupted message in rendezvous

This commit is contained in:
Václav Pavlín 2023-06-01 16:33:11 +02:00
parent 67102873ba
commit 73cba0c33a
No known key found for this signature in database
GPG Key ID: 66AADA19AAF53527
1 changed files with 23 additions and 4 deletions

View File

@ -457,12 +457,19 @@ proc advertisePeer(rdv: RendezVous,
await conn.writeLp(msg) await conn.writeLp(msg)
let let
buf = await conn.readLp(4096) buf = await conn.readLp(4096)
msgRecv = Message.decode(buf).get() decodedMsg = Message.decode(buf)
if decodedMsg.isNone():
trace "Failed to decode message", peer
return
let msgRecv = decodedMsg.get()
if msgRecv.msgType != MessageType.RegisterResponse: if msgRecv.msgType != MessageType.RegisterResponse:
trace "Unexpected register response", peer, msgType = msgRecv.msgType trace "Unexpected register response", peer, msgType = msgRecv.msgType
elif msgRecv.registerResponse.isNone() or elif msgRecv.registerResponse.isNone() or
msgRecv.registerResponse.get().status != ResponseStatus.Ok: msgRecv.registerResponse.get().status != ResponseStatus.Ok:
trace "Refuse to register", peer, response = msgRecv.registerResponse trace "Refuse to register", peer, response = msgRecv.registerResponse
except CatchableError as exc: except CatchableError as exc:
trace "exception in the advertise", error = exc.msg trace "exception in the advertise", error = exc.msg
finally: finally:
@ -530,7 +537,13 @@ proc request*(rdv: RendezVous,
discover: some(d))).buffer) discover: some(d))).buffer)
let let
buf = await conn.readLp(65536) buf = await conn.readLp(65536)
msgRcv = Message.decode(buf).get() decodedMsg = Message.decode(buf)
if decodedMsg.isNone():
trace "Failed to decode message", peer
return
let msgRcv = decodedMsg.get()
if msgRcv.msgType != MessageType.DiscoverResponse or if msgRcv.msgType != MessageType.DiscoverResponse or
msgRcv.discoverResponse.isNone(): msgRcv.discoverResponse.isNone():
debug "Unexpected discover response", msgType = msgRcv.msgType debug "Unexpected discover response", msgType = msgRcv.msgType
@ -627,7 +640,13 @@ proc new*(T: typedesc[RendezVous],
try: try:
let let
buf = await conn.readLp(4096) buf = await conn.readLp(4096)
msg = Message.decode(buf).get() decodedMsg = Message.decode(buf)
if decodedMsg.isNone():
trace "Failed to decode message"
return
let msg = decodedMsg.get()
case msg.msgType: case msg.msgType:
of MessageType.Register: await rdv.register(conn, msg.register.get()) of MessageType.Register: await rdv.register(conn, msg.register.get())
of MessageType.RegisterResponse: of MessageType.RegisterResponse: