Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

fix: Optimize IP detection logic and add error handling

Kissablecho9a66a2a461c8
diff --git a/src/lib/detect.ts b/src/lib/detect.ts
index 2e78a1f35..6bba1596f 100644
--- a/src/lib/detect.ts
+++ b/src/lib/detect.ts
@@ -140,31 +140,31 @@ export async function getClientInfo(request: Request, payload: Record<string, an
export function hasBlockedIp(clientIp: string) {
const ignoreIps = process.env.IGNORE_IP;
- if (ignoreIps) {
- const ips = [];
+ if (!clientIp || !ignoreIps) {
+ return false;
+ }
- if (ignoreIps) {
- ips.push(...ignoreIps.split(',').map(n => n.trim()));
- }
+ const ips = ignoreIps.split(',').map(n => n.trim());
- return ips.find(ip => {
- if (ip === clientIp) {
- return true;
- }
+ return ips.find(ip => {
+ if (ip === clientIp) {
+ return true;
+ }
- // CIDR notation
- if (ip.indexOf('/') > 0) {
+ // CIDR notation
+ if (ip.indexOf('/') > 0) {
+ try {
const addr = ipaddr.parse(clientIp);
const range = ipaddr.parseCIDR(ip);
if (addr.kind() === range[0].kind() && addr.match(range)) {
return true;
}
+ } catch {
+ // Ignore parsing errors
}
+ }
- return false;
- });
- }
-
- return false;
+ return false;
+ });
}