IPTools: Fix CIDR ranges at 128.* and up (#11466)
Some checks are pending
Node.js CI / build (18.x) (push) Waiting to run

This commit is contained in:
Alex "Mathy
2025-10-25 17:26:44 -05:00
committed by GitHub
parent 46980af8ce
commit bc044e3037
2 changed files with 5 additions and 0 deletions

View File

@@ -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 };
}

View File

@@ -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');