Skip to content

fix: Optimize IP detection logic and add error handling#4039

Merged
mikecao merged 2 commits into
umami-software:devfrom
God-2077:umami-v3-dev-2
May 14, 2026
Merged

fix: Optimize IP detection logic and add error handling#4039
mikecao merged 2 commits into
umami-software:devfrom
God-2077:umami-v3-dev-2

Conversation

@God-2077
Copy link
Copy Markdown
Contributor

@God-2077 God-2077 commented Feb 17, 2026

@vercel
Copy link
Copy Markdown

vercel Bot commented Feb 17, 2026

@God-2077 is attempting to deploy a commit to the umami-software Team on Vercel.

A member of the Team first needs to authorize it.

- umami-software#4038
- Check existence of clientIp and ignoreIps in advance
- Add error handling for CIDR parsing
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Feb 17, 2026

Greptile Summary

This PR fixes a crash in the hasBlockedIp function in src/lib/detect.ts that occurred when clientIp was undefined or empty — a situation that arises on Vercel deployments where IP headers may not be present (issue #4038). The fix:

  • Adds an early return when clientIp or ignoreIps is falsy, preventing ipaddr.parse(undefined) from throwing
  • Removes a redundant inner if (ignoreIps) check that was already guarded by the outer condition
  • Wraps CIDR parsing (ipaddr.parse / ipaddr.parseCIDR) in a try-catch to gracefully handle malformed IP addresses or CIDR ranges instead of crashing the entire /api/send route

Confidence Score: 4/5

  • This PR is safe to merge — it adds defensive guards and error handling to an existing function without changing its core logic.
  • The changes are small, well-scoped, and correct. The early return for falsy clientIp directly addresses the reported crash on Vercel. The try-catch around CIDR parsing prevents unhandled exceptions from malformed inputs. The only minor note is the use of .find() instead of .some(), which is a pre-existing style issue that doesn't affect correctness.
  • No files require special attention.

Important Files Changed

Filename Overview
src/lib/detect.ts Refactors hasBlockedIp to add early return for missing clientIp/ignoreIps, removes redundant inner check, and wraps CIDR parsing in try-catch. Fixes a crash when clientIp is undefined (e.g., on Vercel). Minor style note: .find() should be .some() for boolean semantics.

Flowchart

flowchart TD
    A["hasBlockedIp(clientIp)"] --> B{clientIp and ignoreIps exist?}
    B -- No --> C[return false]
    B -- Yes --> D["Split ignoreIps by comma"]
    D --> E["For each ip in list"]
    E --> F{Exact match?}
    F -- Yes --> G[return true]
    F -- No --> H{CIDR notation?}
    H -- No --> I[return false]
    H -- Yes --> J["try: parse clientIp & CIDR range"]
    J --> K{Same kind & match?}
    K -- Yes --> G
    K -- No --> I
    J -- "catch: parse error" --> I
Loading

Last reviewed commit: 9a66a2a

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment thread src/lib/detect.ts Outdated
vdmsmnk added a commit to vdmsmnk/umami that referenced this pull request Mar 18, 2026
@mikecao mikecao merged commit 985aa28 into umami-software:dev May 14, 2026
0 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants