Fail only if all addresses in PeerRecord are invalid (#898)

Fixes https://github.com/waku-org/nwaku/issues/1768
This commit is contained in:
diegomrsantos 2023-05-31 08:59:50 +02:00 committed by GitHub
parent d40d324160
commit 67102873ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 3 deletions

View File

@ -51,11 +51,13 @@ proc decode*(
for address in addressInfos: for address in addressInfos:
var addressInfo = AddressInfo() var addressInfo = AddressInfo()
let subProto = initProtoBuffer(address) let subProto = initProtoBuffer(address)
if ? subProto.getField(1, addressInfo.address) == false: let f = subProto.getField(1, addressInfo.address)
return err(ProtoError.RequiredFieldMissing) if f.isOk() and f.get():
record.addresses &= addressInfo record.addresses &= addressInfo
if record.addresses.len == 0:
return err(ProtoError.RequiredFieldMissing)
ok(record) ok(record)
proc encode*(record: PeerRecord): seq[byte] = proc encode*(record: PeerRecord): seq[byte] =

View File

@ -77,3 +77,41 @@ suite "Signed Routing Record":
buffer = routingRecord.envelope.encode().tryGet() buffer = routingRecord.envelope.encode().tryGet()
check SignedPeerRecord.decode(buffer).error == EnvelopeInvalidSignature check SignedPeerRecord.decode(buffer).error == EnvelopeInvalidSignature
test "Decode doesn't fail if some addresses are invalid":
let
rng = newRng()
privKey = PrivateKey.random(rng[]).tryGet()
peerId = PeerId.init(privKey).tryGet()
multiAddresses = @[MultiAddress(), MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet()]
routingRecord = PeerRecord.init(peerId, multiAddresses, 42)
buffer = routingRecord.encode()
parsedRR = PeerRecord.decode(buffer).tryGet()
check parsedRR.addresses.len == 1
test "Decode doesn't fail if there are no addresses":
let
rng = newRng()
privKey = PrivateKey.random(rng[]).tryGet()
peerId = PeerId.init(privKey).tryGet()
multiAddresses = newSeq[MultiAddress]()
routingRecord = PeerRecord.init(peerId, multiAddresses, 42)
buffer = routingRecord.encode()
parsedRR = PeerRecord.decode(buffer).tryGet()
check parsedRR.addresses.len == 0
test "Decode fails if all addresses are invalid":
let
rng = newRng()
privKey = PrivateKey.random(rng[]).tryGet()
peerId = PeerId.init(privKey).tryGet()
multiAddresses = @[MultiAddress(), MultiAddress()]
routingRecord = PeerRecord.init(peerId, multiAddresses, 42)
buffer = routingRecord.encode()
check PeerRecord.decode(buffer).isErr