Skip to content

fix: keep every netmask of an @ipMatch prefix in the radix tree (2.x) - #3671

Open
fzipi wants to merge 1 commit into
owasp-modsecurity:v2/masterfrom
fzipi:v2/issue-3588-ipmatch-netmasks
Open

fzipi wants to merge 1 commit into
owasp-modsecurity:v2/masterfrom
fzipi:v2/issue-3588-ipmatch-netmasks

Conversation

@fzipi

@fzipi fzipi commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

What

Port of #3660 (3.x) to the 2.x radix tree in apache2/msc_tree.c, which is the same implementation:

  1. Dropped netmask on an existing key. CPTAddElement only handled the "same key" case when the netmask was an exact duplicate. A different netmask on an existing key (10.0.0.0/16 followed by 10.0.0.0/8, or 172.16.0.0 followed by 172.16.0.0/12) was ignored, so the wider prefix never matched.
  2. Netmask array replaced instead of grown. InsertNetmask and the duplicate branch allocated a fresh, empty netmasks array from the pool each time, losing the earlier entries on the node. Insertion is now one helper (CPTAddNetmask) that copies the existing entries into a larger pool allocation and keeps them sorted from most to least specific.
  3. Lookup only applied the first netmask. CPTFindElementIPNetblock never reset the byte index between the netmasks of a node, so only the most specific one was applied to the address. The index is now reset per netmask.

Why

Reported in #3588 for both branches, and consistent with the "stops matching past a certain number of entries" reports in #2192 and #2893.

Tests

New tests/regression/misc/60-ipmatch-overlap.t (auto-discovered), 8 cases using @ipMatch on ARGS:ip: narrow-then-wide and wide-then-narrow on the same key, host-then-network, duplicates, IPv6 narrow-then-wide, and negative cases for both families.

Verified in a Debian bookworm container (apache2 2.4): on untouched v2/master cases 1, 4 and 7 fail (200 instead of 403); on this branch all 8 pass.

References

Fixes #3588 (2.x)
Related to #2192, #2893, #3660

🤖 Generated with Claude Code

Port of owasp-modsecurity#3660 to the 2.x tree code in
apache2/msc_tree.c, which is the same implementation:

- CPTAddElement only handled the "same key" case when the netmask was
  an exact duplicate; a different netmask on an existing key
  (10.0.0.0/16 followed by 10.0.0.0/8) was silently dropped.
- InsertNetmask and the duplicate branch allocated a fresh, empty
  netmasks array instead of growing it, losing the earlier netmasks on
  the node.
- CPTFindElementIPNetblock never reset the byte index between the
  netmasks of a node, so only the most specific one was applied.

Netmask insertion is now one helper that copies the existing entries
into a larger pool allocation and keeps them sorted; the same key
branch appends the new netmask to the existing prefix; the lookup
masks the address again for every netmask.

Fixes owasp-modsecurity#3588
Related to owasp-modsecurity#2192, owasp-modsecurity#2893

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0181jBv5JdNQviwDzfnkf1Gy
@coderabbitai

coderabbitai Bot commented Oct 13, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ebcd4a7c-dcd6-4d67-856f-c31bce173557

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
B Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

This branch has not been deployed

No deployments
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.

1 participant