From 71beed186b3e659b136f7940866f678dd06a6218 Mon Sep 17 00:00:00 2001 From: acul71 Date: Fri, 29 Nov 2024 14:31:39 +0100 Subject: [PATCH 1/3] fix: add case 43 (ipcidr) to fix the ipcidr range bug --- src/convert.ts | 6 ++++++ test/filter/multiaddr-filter.spec.ts | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/convert.ts b/src/convert.ts index 3cbb8321..824f73c0 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -32,6 +32,7 @@ export function convert (proto: string, a: string | Uint8Array): Uint8Array | st /** * Convert [code,Uint8Array] to string */ +// eslint-disable-next-line complexity export function convertToString (proto: number | string, buf: Uint8Array): string { const protocol = getProtocol(proto) switch (protocol.code) { @@ -40,6 +41,8 @@ export function convertToString (proto: number | string, buf: Uint8Array): strin return bytes2ip(buf) case 42: // ipv6zone return bytes2str(buf) + case 43: // ipcidr + return buf[0].toString() case 6: // tcp case 273: // udp @@ -71,6 +74,7 @@ export function convertToString (proto: number | string, buf: Uint8Array): strin } } +// eslint-disable-next-line complexity export function convertToBytes (proto: string | number, str: string): Uint8Array { const protocol = getProtocol(proto) switch (protocol.code) { @@ -80,6 +84,8 @@ export function convertToBytes (proto: string | number, str: string): Uint8Array return ip2bytes(str) case 42: // ipv6zone return str2bytes(str) + case 43: // ipcidr + return new Uint8Array([parseInt(str, 10)]) case 6: // tcp case 273: // udp diff --git a/test/filter/multiaddr-filter.spec.ts b/test/filter/multiaddr-filter.spec.ts index d618889f..81d39435 100644 --- a/test/filter/multiaddr-filter.spec.ts +++ b/test/filter/multiaddr-filter.spec.ts @@ -14,7 +14,9 @@ describe('MultiaddrFilter', () => { ['/ip4/192.168.10.10/ipcidr/24', '/ip4/192.168.11.2/udp/60', false], ['/ip4/192.168.10.10/ipcidr/24', '/ip6/2001:db8:3333:4444:5555:6666:7777:8888/tcp/60', false], ['/ip6/2001:db8:3333:4444:5555:6666:7777:8888/ipcidr/60', '/ip6/2001:0db8:3333:4440:0000:0000:0000:0000/tcp/60', true], - ['/ip6/2001:db8:3333:4444:5555:6666:7777:8888/ipcidr/60', '/ip6/2001:0db8:3333:4450:0000:0000:0000:0000/tcp/60', false] + ['/ip6/2001:db8:3333:4444:5555:6666:7777:8888/ipcidr/60', '/ip6/2001:0db8:3333:4450:0000:0000:0000:0000/tcp/60', false], + ['/ip6/2001:db8:3333:4444:5555:6666:7777:8888/ipcidr/128', '/ip6/2001:db8:3333:4444:5555:6666:7777:8888/tcp/60', true], + ['/ip6/2001:db8:3333:4444:5555:6666:7777:8888/ipcidr/128', '/ip6/2001:db8:3333:4444:5555:6666:7777:8880/tcp/60', false] ] cases.forEach(([cidr, ip, result]) => { From 5b739f16d46024fe1537971581a4ef91455ca4f5 Mon Sep 17 00:00:00 2001 From: acul71 <34693171+acul71@users.noreply.github.com> Date: Mon, 2 Dec 2024 18:58:40 +0100 Subject: [PATCH 2/3] Update src/convert.ts Co-authored-by: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> --- src/convert.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/convert.ts b/src/convert.ts index 824f73c0..035c18d7 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -42,7 +42,7 @@ export function convertToString (proto: number | string, buf: Uint8Array): strin case 42: // ipv6zone return bytes2str(buf) case 43: // ipcidr - return buf[0].toString() + return uint8ArrayToString(buf, 'base10') case 6: // tcp case 273: // udp From 8b3aa9fd121dbda8fd3d9d824455f59922c16da7 Mon Sep 17 00:00:00 2001 From: acul71 <34693171+acul71@users.noreply.github.com> Date: Mon, 2 Dec 2024 18:58:51 +0100 Subject: [PATCH 3/3] Update src/convert.ts Co-authored-by: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> --- src/convert.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/convert.ts b/src/convert.ts index 035c18d7..cbc5b078 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -85,7 +85,7 @@ export function convertToBytes (proto: string | number, str: string): Uint8Array case 42: // ipv6zone return str2bytes(str) case 43: // ipcidr - return new Uint8Array([parseInt(str, 10)]) + return uint8ArrayFromString(str, 'base10') case 6: // tcp case 273: // udp