Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add more checks to handleFindNode #216

Merged
merged 3 commits into from
Nov 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,20 +740,25 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) {
private handleFindNode(nodeAddr: INodeAddress, message: IFindNodeMessage): void {
const { id, distances } = message;
let nodes: ENR[] = [];
distances.forEach((distance) => {
for (const distance of new Set(distances)) {
// filter out invalid distances
if (distance < 0 || distance > 256) {
continue;
}
// if the distance is 0, send our local ENR
if (distance === 0) {
this.enr.encodeToValues(this.keypair.privateKey);
nodes.push(this.enr);
} else {
nodes.push(...this.kbuckets.valuesOfDistance(distance));
}
});
nodes = nodes.slice(0, 15);
}
// limit response to 16 nodes
nodes = nodes.slice(0, 16);
if (nodes.length === 0) {
log("Sending empty NODES response to %o", nodeAddr);
try {
this.sessionService.sendResponse(nodeAddr, createNodesMessage(id, 0, nodes));
this.sessionService.sendResponse(nodeAddr, createNodesMessage(id, 1, nodes));
this.metrics?.sentMessageCount.inc({ type: MessageType[MessageType.NODES] });
} catch (e) {
log("Failed to send a NODES response. Error: %s", (e as Error).message);
Expand Down