Skip to content

fix: keep the address intact in the 0-optimized IP format - #898

Merged
Mzack9999 merged 2 commits into
projectdiscovery:mainfrom
RaphaelFakhri:fix/zero-optimized-ip-format
Oct 23, 2026
Merged

Mzack9999 merged 2 commits into
projectdiscovery:mainfrom
RaphaelFakhri:fix/zero-optimized-ip-format

Conversation

@RaphaelFakhri

@RaphaelFakhri RaphaelFakhri commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The -if 2 (0-optimized dotted-decimal) format removes every 0. substring and any trailing zero octet. That rewrites the address instead of shortening it:

Input Before Resolves to After
10.0.0.1 11 0.0.0.11 10.1
192.168.10.1 192.168.11 192.168.0.11 192.168.10.1
1.2.3.0 1.2.3 1.2.0.3 1.2.3.0
127.0.1.0 127.1 127.0.0.1 127.0.1.0
127.0.0.1 127.1 127.0.0.1 127.1 (unchanged)

In the shortened a.b.c and a.d forms, the last part fills all remaining bytes, so the only zero octets that can be dropped are the ones directly before the last octet (a.0.0.d to a.d, a.b.0.d to a.b.d). The fix builds the short form from the octets under that rule and returns the address unchanged when no octet can be dropped. IPv6 input keeps its current output.

The README example for 127.0.1.0 showed 127.1; it now shows the address unchanged.

Testing

  • Added cases to TestIpEncodings for 10.0.0.1, 192.168.0.10, 192.168.10.1, 1.2.3.0, and 127.0.1.0.
  • go test ./... passes. The new cases fail without the change in ip.go.

Fixes #897

Summary by CodeRabbit

  • Enhancements

    • Improved IPv4 address formatting to produce more compact representations when applicable, including shortened forms for addresses with zero octets.
    • Preserved the original representation for addresses that do not qualify for shortening.
  • Documentation

    • Updated the IP-format example to reflect the current output.
  • Tests

    • Expanded coverage for shortened, preserved, and standard IPv4 formatting cases.

The -if 2 format removed every "0." substring and any trailing zero
octet, so it changed the address: 10.0.0.1 became 11 (0.0.0.11),
192.168.10.1 became 192.168.11 (192.168.0.11), and 127.0.1.0 became
127.1 (127.0.0.1).

Only omit the zero octets that precede the last octet, which is the
only case where the shortened form resolves to the same address.
@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6f901fd8-faf6-4806-9298-64d11fe6f4ab
📥 Commits

Reviewing files that changed from the base of the PR and between 6ef8f51 and fd5fbd3.

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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 458c5737-d97e-4ba1-bda9-d6bf115d6dbd
📥 Commits

Reviewing files that changed from the base of the PR and between 43eaae9 and 6ef8f51.

📒 Files selected for processing: 3
  • README.md
  • ip.go
  • ip_test.go

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

Walkthrough

AlterIP mode "2" now uses zeroOptimizedIP to format eligible IPv4 addresses. The change removes the regexp import, adds table-driven test cases, and updates the README example output.

Changes

IP formatting

Layer / File(s) Summary
Address-preserving formatting
ip.go, ip_test.go, README.md
zeroOptimizedIP preserves non-IPv4 addresses and addresses with a nonzero third octet. AlterIP mode "2" uses the helper. Tests cover shortened and unchanged outputs, and the README example now shows 127.0.1.0.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: mzack9999, ehsandeep

Merge Risk: ⚪ Minimal · up to 6ef8f

No actionable merge-blocking risk remains.

Security architecture risk: ⚪ Minimal · up to 6ef8f

The change corrects address-preserving output in an existing formatting mode. It introduces no new entrypoint, permissions, network operations, or persistent state, and no material security risk was identified in the changed behavior.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed exposure is the address text returned to existing callers selecting mode 2. The complete production delta adds no caller, privileged sink, or authority transition. Any security consequence in an external consumer would depend on that consumer's handling of the returned representation.

Trust Boundaries and Controls

  • observed — The existing input-parsing step remains in place. Shortened IPv4 output already existed at the base revision, so this PR does not newly introduce alternate address notation or remove a validation or authorization gate from the changed path.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: preserving addresses in the zero-optimized IP format when zero octets cannot be omitted safely.
Linked Issues check Passed Issue #897 requires value-preserving -if 2 output, valid omission of zero octets, unchanged output when no omission is valid, and the corrected README example. The reviewed head adds `TestIpEncoding…
Out of Scope Changes check Passed The changes are limited to ip.go, ip_test.go, and the related README.md example. The implementation, regression tests, and documentation update directly support issue #897. No unrelated change i…
Full details: Linked Issues check

Explanation

Issue #897 requires value-preserving -if 2 output, valid omission of zero octets, unchanged output when no omission is valid, and the corrected README example. The reviewed head adds TestIpEncodings cases for 10.0.0.1, 192.168.0.10, 192.168.10.1, 1.2.3.0, and 127.0.1.0. The implementation delegates mode "2" to IPv4-specific zero optimization and preserves non-IPv4 input. README.md now shows 127.0.1.0 unchanged.

Full details: Out of Scope Changes check

Explanation

The changes are limited to ip.go, ip_test.go, and the related README.md example. The implementation, regression tests, and documentation update directly support issue #897. No unrelated change is demonstrated.

Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

A rabbit checks each dotted row,
And trims the zeros where they go.
The address keeps its proper place,
Tests hop along at measured pace.
The README shows the updated case.

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

@Mzack9999
Mzack9999 merged commit 034c51c into projectdiscovery:main Oct 23, 2026
7 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.

[Issue] 0-optimized IP format (-if 2) outputs a different address

2 participants