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 500 on /api/send when client IP is malformed.

Mike Cao3ebd5a44e837
diff --git a/src/lib/detect.ts b/src/lib/detect.ts
index 2e78a1f35..643ed01a2 100644
--- a/src/lib/detect.ts
+++ b/src/lib/detect.ts
@@ -77,8 +77,10 @@ function decodeHeader(s: string | undefined | null): string | undefined | null {
}
export async function getLocation(ip: string = '', headers: Headers, skipHeaders: boolean) {
- // Ignore local ips
- if (!ip || (await isLocalhost(ip))) {
+ const cleanIp = stripPort(ip);
+
+ // Ignore local or invalid ips
+ if (!cleanIp || !ipaddr.isValid(cleanIp) || (await isLocalhost(cleanIp))) {
return null;
}
@@ -108,7 +110,7 @@ export async function getLocation(ip: string = '', headers: Headers, skipHeaders
);
}
- const result = globalThis[MAXMIND]?.get(stripPort(ip));
+ const result = globalThis[MAXMIND]?.get(cleanIp);
if (result) {
const country = result.country?.iso_code ?? result?.registered_country?.iso_code;
diff --git a/src/lib/ip.ts b/src/lib/ip.ts
index a78c7e56e..08e77c65e 100644
--- a/src/lib/ip.ts
+++ b/src/lib/ip.ts
@@ -84,9 +84,7 @@ export function getIpAddress(headers: Headers) {
if (header === 'forwarded') {
const match = ip.match(/for=(\[?[0-9a-fA-F:.]+]?)/);
- if (match) {
- return resolveIp(match[1]);
- }
+ return match ? resolveIp(match[1]) : undefined;
}
return resolveIp(ip);