2024-01-24 08:09:11 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2023-02-21 08:34:26 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
2023-05-10 13:50:04 +00:00
|
|
|
{.push raises: [].}
|
2021-09-13 12:20:26 +00:00
|
|
|
|
|
|
|
import
|
2024-06-18 16:09:27 +00:00
|
|
|
std/[sets, net],
|
2024-05-30 12:46:57 +00:00
|
|
|
results, chronicles, chronos,
|
2023-02-21 08:34:26 +00:00
|
|
|
../../net/utils,
|
2021-09-13 12:20:26 +00:00
|
|
|
"."/[node, enr, routing_table]
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "nodes-verification"
|
|
|
|
|
2023-02-21 08:34:26 +00:00
|
|
|
func validIp(sender, address: IpAddress): bool =
|
|
|
|
let a = initTAddress(address, Port(0))
|
|
|
|
if a.isGlobalUnicast():
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
let s = initTAddress(sender, Port(0))
|
|
|
|
if a.isLoopback() and s.isLoopback():
|
|
|
|
true
|
|
|
|
elif a.isSiteLocal() and s.isSiteLocal():
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
false
|
2021-09-13 12:20:26 +00:00
|
|
|
|
2023-02-21 08:34:26 +00:00
|
|
|
proc verifyNodesRecords(
|
|
|
|
enrs: openArray[Record], src: Node, nodesLimit: int,
|
2024-06-18 16:09:27 +00:00
|
|
|
distances: Opt[seq[uint16]]): seq[Node] =
|
2021-09-13 12:20:26 +00:00
|
|
|
## Verify and convert ENRs to a sequence of nodes. Only ENRs that pass
|
|
|
|
## verification will be added. ENRs are verified for duplicates, invalid
|
|
|
|
## addresses and invalid distances if those are specified.
|
2023-02-21 08:34:26 +00:00
|
|
|
logScope:
|
|
|
|
sender = src.record.toURI
|
|
|
|
|
2021-09-13 12:20:26 +00:00
|
|
|
var seen: HashSet[Node]
|
|
|
|
var count = 0
|
|
|
|
for r in enrs:
|
|
|
|
# Check and allow for processing of maximum `findNodeResultLimit` ENRs
|
|
|
|
# returned. This limitation is required so no huge lists of invalid ENRs
|
|
|
|
# are processed for no reason, and for not overwhelming a routing table
|
|
|
|
# with nodes from a malicious actor.
|
|
|
|
# The discovery v5 specification specifies no limit on the amount of ENRs
|
|
|
|
# that can be returned, but clients usually stick with the bucket size limit
|
|
|
|
# as in original Kademlia. Because of this it is chosen not to fail
|
2022-11-16 16:44:00 +00:00
|
|
|
# immediately, but still process maximum `findNodeResultLimit`.
|
2021-09-13 12:20:26 +00:00
|
|
|
if count >= nodesLimit:
|
2023-02-21 08:34:26 +00:00
|
|
|
debug "Too many ENRs", enrs = enrs.len(), limit = nodesLimit
|
2021-09-13 12:20:26 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
count.inc()
|
|
|
|
|
2024-06-27 14:18:21 +00:00
|
|
|
let n = Node.fromRecord(r)
|
|
|
|
# Check for duplicates in the nodes reply. Duplicates are checked based
|
|
|
|
# on node id.
|
|
|
|
if n in seen:
|
|
|
|
trace "Duplicate node ids", record = n.record.toURI, id = n.id
|
|
|
|
continue
|
|
|
|
# Check if the node has an address and if the address is public or from
|
|
|
|
# the same local network or lo network as the sender. The latter allows
|
|
|
|
# for local testing.
|
|
|
|
if not n.address.isSome() or not
|
|
|
|
validIp(src.address.get().ip, n.address.get().ip):
|
|
|
|
trace "Invalid ip-address", record = n.record.toURI, node = n
|
|
|
|
continue
|
|
|
|
# Check if returned node has one of the requested distances.
|
|
|
|
if distances.isSome():
|
|
|
|
# TODO: This is incorrect for custom distances
|
|
|
|
if (not distances.get().contains(logDistance(n.id, src.id))):
|
|
|
|
debug "Incorrect distance", record = n.record.toURI
|
2021-09-13 12:20:26 +00:00
|
|
|
continue
|
|
|
|
|
2024-06-27 14:18:21 +00:00
|
|
|
# No check on UDP port and thus any port is allowed, also the so called
|
|
|
|
# "well-known" ports.
|
2021-09-13 12:20:26 +00:00
|
|
|
|
2024-06-27 14:18:21 +00:00
|
|
|
seen.incl(n)
|
|
|
|
result.add(n)
|
2021-09-13 12:20:26 +00:00
|
|
|
|
2023-02-21 08:34:26 +00:00
|
|
|
proc verifyNodesRecords*(
|
|
|
|
enrs: openArray[Record], src: Node, nodesLimit: int): seq[Node] =
|
2024-06-18 16:09:27 +00:00
|
|
|
verifyNodesRecords(enrs, src, nodesLimit, Opt.none(seq[uint16]))
|
2021-09-13 12:20:26 +00:00
|
|
|
|
2023-02-21 08:34:26 +00:00
|
|
|
proc verifyNodesRecords*(
|
|
|
|
enrs: openArray[Record], src: Node, nodesLimit: int,
|
|
|
|
distances: seq[uint16]): seq[Node] =
|
2024-06-18 16:09:27 +00:00
|
|
|
verifyNodesRecords(enrs, src, nodesLimit, Opt.some(distances))
|