Skip to content

Commit

Permalink
Support CIDR notation in IGNORE_IP, closes umami-software#544.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Apr 26, 2021
1 parent 0cf115b commit 9270581
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 15 additions & 1 deletion pages/api/collect.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4301,6 +4301,11 @@ [email protected]:
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"
Expand Down

0 comments on commit 9270581

Please sign in to comment.