-
Notifications
You must be signed in to change notification settings - Fork 108
fix: correctly filter IPs from CIDR during aggregation #684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
7894e98
to
b60c971
Compare
b60c971
to
18d432c
Compare
WalkthroughImplements a new processing path when both FilterIP and Aggregate are set: expands CIDRs to individual IP /32 or /128 CIDRs excluding filtered IPs, integrates with aggregation pipeline, and updates tests. Adds a guard in IPv4-mapped IPv6 conversion to prevent double-mapping. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant U as User
participant CLI as mapcidr CLI
participant P as Processor
participant R as Ranger/Aggregator
U->>CLI: Run with -cl CIDRs, -fi FilterIP, -a
CLI->>P: Parse options and inputs
alt FilterIP && Aggregate
P->>P: getIPList(CIDR)
P->>P: Exclude IPs in FilterIP
loop remaining IPs
P->>P: Build single-IP CIDR (/32 or /128)
P->>R: Optionally add original CIDR (if Aggregate/Shuffle/Sort/Approx/Count)
end
R-->>CLI: Aggregated result
else Other modes
P->>R: Existing coalesce/sort/shuffle/approx/count path or commonFunc
R-->>CLI: Result
end
CLI-->>U: Output CIDRs
note over P,R: New path expands CIDR to IPs, filters, then aggregates
sequenceDiagram
autonumber
participant N as ipNetToRange
participant IP as IP Inputs
IP->>N: firstIP, lastIP
alt firstIP is IPv4 and not already v4-mapped v6
N->>N: Map to IPv4-mapped IPv6 for both ends
else Already mapped or IPv6
N->>N: Keep as-is
end
N-->>IP: Normalized range
note over N: Guard prevents double-mapping
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Pre-merge checks (4 passed, 1 warning)❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Poem
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal). Please share your feedback with us on this Discord post. ✨ Finishing touches
🧪 Generate unit tests
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
ip.go (1)
282-285
: Good guard against double IPv4-mapped IPv6 conversion; small symmetry nitThe check avoids double-mapping. For readability, consider using To4() for lastIP too.
- firstIP = append(v4Mappedv6Prefix, ip4...) - lastIP = append(v4Mappedv6Prefix, lastIP...) + firstIP = append(v4Mappedv6Prefix, ip4...) + lastIP = append(v4Mappedv6Prefix, lastIP.To4()...)cmd/mapcidr/main_test.go (1)
223-233
: Regression covered — LGTMTest asserts the exact aggregates post-filter; matches the reported issue. Consider adding an IPv6 variant and a case where FilterIP contains a CIDR to harden coverage.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
cmd/mapcidr/main.go
(1 hunks)cmd/mapcidr/main_test.go
(1 hunks)ip.go
(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
cmd/mapcidr/main_test.go (1)
cmd/mapcidr/main.go (1)
Options
(41-69)
if len(options.FilterIP) != 0 && options.Aggregate { | ||
for _, ip := range getIPList([]*net.IPNet{pCidr}) { | ||
if options.FilterIP != nil && sliceutil.Contains(options.FilterIP, ip.String()) { | ||
continue | ||
} | ||
singleCIDR := &net.IPNet{ | ||
IP: ip, | ||
Mask: net.CIDRMask(len(ip)*8, len(ip)*8), | ||
} | ||
allCidrs = append(allCidrs, singleCIDR) | ||
} | ||
if options.Aggregate || options.Shuffle || hasSort || options.AggregateApprox || options.Count { | ||
_ = ranger.Add(cidr) | ||
} | ||
} else if options.Aggregate || options.Shuffle || hasSort || options.AggregateApprox || options.Count { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Avoid per-IP expansion; rely on RemoveCIDRs output to prevent O(N) blowups
Enumerating every IP to re-add /32 or /128 CIDRs is extremely costly for large ranges and unnecessary since the earlier RemoveCIDRs step already yields the filtered CIDRs. Append those CIDRs directly to allCidrs and aggregate.
Apply:
- if len(options.FilterIP) != 0 && options.Aggregate {
- for _, ip := range getIPList([]*net.IPNet{pCidr}) {
- if options.FilterIP != nil && sliceutil.Contains(options.FilterIP, ip.String()) {
- continue
- }
- singleCIDR := &net.IPNet{
- IP: ip,
- Mask: net.CIDRMask(len(ip)*8, len(ip)*8),
- }
- allCidrs = append(allCidrs, singleCIDR)
- }
- if options.Aggregate || options.Shuffle || hasSort || options.AggregateApprox || options.Count {
- _ = ranger.Add(cidr)
- }
- } else if options.Aggregate || options.Shuffle || hasSort || options.AggregateApprox || options.Count {
- // In case of coalesce/shuffle we need to know all the cidrs and aggregate them by calling the proper function
- _ = ranger.Add(cidr)
- allCidrs = append(allCidrs, pCidr)
- } else {
- commonFunc(cidr, outputchan)
- }
+ if options.Aggregate || options.Shuffle || hasSort || options.AggregateApprox || options.Count {
+ // After RemoveCIDRs above, `cidr`/`pCidr` already exclude FilterIP.
+ _ = ranger.Add(cidr)
+ allCidrs = append(allCidrs, pCidr)
+ } else {
+ commonFunc(cidr, outputchan)
+ }
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if len(options.FilterIP) != 0 && options.Aggregate { | |
for _, ip := range getIPList([]*net.IPNet{pCidr}) { | |
if options.FilterIP != nil && sliceutil.Contains(options.FilterIP, ip.String()) { | |
continue | |
} | |
singleCIDR := &net.IPNet{ | |
IP: ip, | |
Mask: net.CIDRMask(len(ip)*8, len(ip)*8), | |
} | |
allCidrs = append(allCidrs, singleCIDR) | |
} | |
if options.Aggregate || options.Shuffle || hasSort || options.AggregateApprox || options.Count { | |
_ = ranger.Add(cidr) | |
} | |
} else if options.Aggregate || options.Shuffle || hasSort || options.AggregateApprox || options.Count { | |
if options.Aggregate || options.Shuffle || hasSort || options.AggregateApprox || options.Count { | |
// After RemoveCIDRs above, `cidr`/`pCidr` already exclude FilterIP. | |
_ = ranger.Add(cidr) | |
allCidrs = append(allCidrs, pCidr) | |
} else { | |
commonFunc(cidr, outputchan) | |
} |
🤖 Prompt for AI Agents
In cmd/mapcidr/main.go around lines 455 to 469, the code expands each IP from a
CIDR into /32 or /128 entries which causes O(N) blowups; instead, append the
filtered CIDR (pCidr) directly to allCidrs and, when appropriate, add the
original cidr to the ranger. Remove the per-IP loop and creation of singleCIDR
entries; simply do allCidrs = append(allCidrs, pCidr) (or equivalent) and keep
the existing conditional that calls ranger.Add(cidr) when options.Aggregate ||
options.Shuffle || hasSort || options.AggregateApprox || options.Count is true
so aggregation and sorting behavior remains unchanged.
Summary
Fixes a regression where filtered IPs (
-fi
) were reintroduced after aggregation (-a
).Closes #523
Fix
FilterIP
is set:pCidr
into its IP list withgetIPList
FilterIP
/32
(or/128
) CIDRspCidr
Summary by CodeRabbit