Skip to content

Use the address width as prefix when banning an IP - #200

Merged
AltamashShaikh merged 1 commit into
5.x-devfrom
AS-896
Dec 4, 2026
Merged

AltamashShaikh merged 1 commit into
5.x-devfrom
AS-896

Conversation

@AltamashShaikh

Copy link
Copy Markdown
Contributor

Description

Bug: When a visit reaches the "maximum number of actions per visit" limit, BlockedIpRanges::banIp() bans the visitor's IP. It chose the prefix by looking for a dot in the address: /32 with a dot, /128 without. An IPv6 address written with a dotted quad (IPv4-mapped ::ffff:192.0.2.1, IPv4-compatible ::192.0.2.1, or 64:ff9b::192.0.2.1) contains a dot but is 128 bits wide, so it was banned as a /32 range. That range covers far more than the one IP.

In practice the extra blocking is limited: isExcluded() only checks ranges stored under the same index (the address up to the first dot, eg ::ffff:192.), and banned IPs are cleared by the daily task. Still, other visitors who share that index could be blocked for up to a day.

Fix (BlockedIpRanges.php): the prefix now comes from the binary length of the address (IPUtils::stringToBinaryIP()), so IPv4 addresses still get /32 and every IPv6 address gets /128. The stored format, the index and the TrackingSpamPrevention.banIp event are unchanged; for these addresses the event now receives the /128 range. Ranges already stored with the wrong prefix are removed by the daily updateBlockedIpRanges task, so no migration is needed.

Tests: a new data-provider test in BlockedIpRangesTest covers IPv4-mapped, IPv4-compatible and NAT64-style addresses. It checks the stored range is /128, that the banned IP is blocked, and that another IP with the same index is not.

Version bumped to 5.2.3 with a changelog entry.

Backport of matomo-org/matomo#25913.

Checklist

  • [✔] I have understood, reviewed, and tested all AI outputs before use
  • [✔] All AI instructions respect security, IP, and privacy rules

Review

An IPv6 address written with a dotted quad, such as ::ffff:192.0.2.1,
was banned with a /32 prefix because the address contains a dot. The
range then covered far more than the banned IP. The prefix now comes
from the binary length of the address.

@snake14 snake14 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM 👍

@AltamashShaikh
AltamashShaikh merged commit 1ff8abf into 5.x-dev Dec 4, 2026
21 checks passed
@AltamashShaikh
AltamashShaikh deleted the AS-896 branch December 4, 2026 03:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants