From cf44ce4c200db3d203769f46d8cac47796c56dd0 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Tue, 1 Nov 2022 15:24:28 -0500 Subject: [PATCH 1/3] fix: add more checks to handleFindNode --- src/service/service.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/service/service.ts b/src/service/service.ts index 53eda9a5..f7751552 100644 --- a/src/service/service.ts +++ b/src/service/service.ts @@ -740,7 +740,12 @@ 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) => { + let distinctDistances = distances.filter((n, i) => distances.indexOf(n) === i); + distinctDistances.forEach((distance) => { + // filter out invalid distances + if (distance < 0 || distance > 256) { + return; + } // if the distance is 0, send our local ENR if (distance === 0) { this.enr.encodeToValues(this.keypair.privateKey); @@ -749,11 +754,12 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) { 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); From 440cfa29feadc0eaf447ab11435278164e9c7b7d Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Wed, 2 Nov 2022 11:37:38 -0500 Subject: [PATCH 2/3] Iterate over a set of distances --- src/service/service.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/service/service.ts b/src/service/service.ts index f7751552..adfcc948 100644 --- a/src/service/service.ts +++ b/src/service/service.ts @@ -740,8 +740,7 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) { private handleFindNode(nodeAddr: INodeAddress, message: IFindNodeMessage): void { const { id, distances } = message; let nodes: ENR[] = []; - let distinctDistances = distances.filter((n, i) => distances.indexOf(n) === i); - distinctDistances.forEach((distance) => { + for (const distance of new Set(distances)) { // filter out invalid distances if (distance < 0 || distance > 256) { return; @@ -753,7 +752,7 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) { } else { nodes.push(...this.kbuckets.valuesOfDistance(distance)); } - }); + } // limit response to 16 nodes nodes = nodes.slice(0, 16); if (nodes.length === 0) { From 35c7fcf6f52751a3584f359de0846bcbcaca1cd8 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Wed, 2 Nov 2022 11:39:26 -0500 Subject: [PATCH 3/3] Use continue instead of return --- src/service/service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/service/service.ts b/src/service/service.ts index adfcc948..1c2c9b14 100644 --- a/src/service/service.ts +++ b/src/service/service.ts @@ -743,7 +743,7 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) { for (const distance of new Set(distances)) { // filter out invalid distances if (distance < 0 || distance > 256) { - return; + continue; } // if the distance is 0, send our local ENR if (distance === 0) {