From bc044e3037325b76a28fa93b216f5d43a56b9e7b Mon Sep 17 00:00:00 2001 From: "Alex \"Mathy" <4866817+MathyFurret@users.noreply.github.com> Date: Sat, 25 Oct 2025 17:26:44 -0500 Subject: [PATCH] IPTools: Fix CIDR ranges at 128.* and up (#11466) --- server/ip-tools.ts | 1 + test/server/ip-tools.js | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/server/ip-tools.ts b/server/ip-tools.ts index 1bc5c02a8b..e248f7b5d5 100644 --- a/server/ip-tools.ts +++ b/server/ip-tools.ts @@ -151,6 +151,7 @@ export const IPTools = new class { // does << with signed int32s. if (low === null || !bits || bits < 2 || bits > 32) return null; low &= ~((1 << (32 - bits)) - 1); + if (low < 0) low += 4294967296; const high = low + (1 << (32 - bits)) - 1; return { minIP: low, maxIP: high }; } diff --git a/test/server/ip-tools.js b/test/server/ip-tools.js index 4de2cceaee..cc69cc6a97 100644 --- a/test/server/ip-tools.js +++ b/test/server/ip-tools.js @@ -31,6 +31,10 @@ describe("IP tools", () => { assert.equal(IPTools.getCidrRange('10.69.42.69/8').minIP, IPTools.ipToNumber('10.0.0.0')); }); + it('should not yield negative values when parsing CIDR ranges at 128.* or higher', () => { + assert.equal(IPTools.getCidrRange('173.255.160.0/19').minIP, IPTools.ipToNumber('173.255.160.0')); + }); + it('should guess whether a range is CIDR or hyphen', () => { const cidrRange = IPTools.stringToRange('42.42.0.0/18'); const stringRange = IPTools.stringToRange('42.42.0.0 - 42.42.63.255');