replace ipaddr.js

This commit is contained in:
Girish Ramakrishnan
2025-05-06 16:16:33 +02:00
parent c8976daf96
commit a7c6e36ec3
10 changed files with 186 additions and 34 deletions
+3 -7
View File
@@ -34,7 +34,7 @@ const assert = require('assert'),
BoxError = require('./boxerror.js'),
database = require('./database.js'),
hat = require('./hat.js'),
ipaddr = require('ipaddr.js'),
ipaddr = require('./ipaddr.js'),
safe = require('safetydance'),
uuid = require('uuid');
@@ -104,7 +104,6 @@ function parseIpRanges(ipRanges) {
// each line can have comma separated list. this complexity is because we changed the UI to take a line input instead of textarea
for (const entry of line.split(',')) {
const rangeOrIP = entry.trim();
// this checks for IPv4 and IPv6
if (!ipaddr.isValid(rangeOrIP) && !ipaddr.isValidCIDR(rangeOrIP)) throw new BoxError(BoxError.BAD_FIELD, `${rangeOrIP} is not a valid IP or range`);
result.push(rangeOrIP);
}
@@ -209,8 +208,6 @@ function isIpAllowedSync(token, ip) {
assert.strictEqual(typeof token, 'object');
assert.strictEqual(typeof ip, 'string');
const parsedIp = ipaddr.process(ip); // will demangle IPv4-mapped IPv6
let allowedIpRanges = gParsedRangesCache.get(token.id); // returns undefined if not found
if (!allowedIpRanges) {
allowedIpRanges = parseIpRanges(token.allowedIpRanges || '');
@@ -219,10 +216,9 @@ function isIpAllowedSync(token, ip) {
for (const ipOrRange of allowedIpRanges) {
if (!ipOrRange.includes('/')) {
if (ip === ipOrRange) return true;
if (ipaddr.isEqual(ipOrRange, ip)) return true;
} else {
const parsedRange = ipaddr.parseCIDR(ipOrRange); // returns [addr, range]
if (parsedRange[0].kind() === parsedIp.kind() && parsedIp.match(parsedRange)) return true;
if (ipaddr.includes(ipOrRange, ip)) return true;
}
}