Support CIDR notation in IGNORE_IP, closes #544.

This commit is contained in:
Mike Cao 2021-04-25 23:57:49 -07:00
parent 0cf115b2de
commit 92705815ed
3 changed files with 21 additions and 1 deletions

View File

@ -70,6 +70,7 @@
"dotenv": "^8.2.0",
"formik": "^2.2.6",
"immer": "^8.0.1",
"ipaddr.js": "^2.0.0",
"is-localhost-ip": "^1.4.0",
"isbot": "^3.0.26",
"jose": "2.0.5",

View File

@ -1,4 +1,5 @@
import isbot from 'isbot';
import ipaddr from 'ipaddr.js';
import { savePageView, saveEvent } from 'lib/queries';
import { useCors, useSession } from 'lib/middleware';
import { getIpAddress } from 'lib/request';
@ -15,8 +16,21 @@ export default async (req, res) => {
if (process.env.IGNORE_IP) {
const ips = process.env.IGNORE_IP.split(',').map(n => n.trim());
const ip = getIpAddress(req);
const blocked = ips.find(i => {
if (i === ip) return true;
if (ips.includes(ip)) {
// CIDR notation
if (i.indexOf('/') > 0) {
const addr = ipaddr.parse(ip);
const range = ipaddr.parseCIDR(i);
if (addr.match(range)) return true;
}
return false;
});
if (blocked) {
return ok(res);
}
}

View File

@ -4301,6 +4301,11 @@ intl-messageformat@9.6.8:
fast-memoize "^2.5.2"
tslib "^2.1.0"
ipaddr.js@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-2.0.0.tgz#77ccccc8063ae71ab65c55f21b090698e763fc6e"
integrity sha512-S54H9mIj0rbxRIyrDMEuuER86LdlgUg9FSeZ8duQb6CUG2iRrA36MYVQBSprTF/ZeAwvyQ5mDGuNvIPM0BIl3w==
is-alphabetical@^1.0.0:
version "1.0.4"
resolved "https://registry.yarnpkg.com/is-alphabetical/-/is-alphabetical-1.0.4.tgz#9e7d6b94916be22153745d184c298cbf986a686d"