Skip to content

fix: use colon count instead of IPv6 regex for port-stripping guard#539

Merged
torlando-tech merged 1 commit intomainfrom
fix/ipv6-port-strip-guard
Feb 24, 2026
Merged

fix: use colon count instead of IPv6 regex for port-stripping guard#539
torlando-tech merged 1 commit intomainfrom
fix/ipv6-port-strip-guard

Conversation

@torlando-tech
Copy link
Copy Markdown
Owner

Summary

  • Replaces !IPV6_REGEX.matches() guard with a colon-count check (colonCount == 1) for port stripping in validateHostname()
  • Compressed IPv6 addresses like ::1 and fe80::1 aren't matched by the existing IPV6_REGEX, so they were being corrupted by the port-stripping logic
  • Simpler invariant: IPv6 always has 2+ colons; hostname:port has exactly one

Follow-up to #537 addressing Greptile review feedback on #538.

Test plan

  • Existing test: validateHostname - does not strip segments from IPv6 address (full-form)
  • New test: validateHostname - does not corrupt compressed IPv6 addresses (::1, fe80::1)
  • Existing port-stripping tests still pass

🤖 Generated with Claude Code

Compressed IPv6 like "::1" or "fe80::1" aren't matched by IPV6_REGEX
and would be corrupted by port stripping. Instead of fixing the regex,
use a simpler invariant: IPv6 always has multiple colons, hostname:port
has exactly one.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Feb 23, 2026

Greptile Summary

Replaces complex IPv6 regex checking with a simpler colon-counting approach to prevent port stripping from corrupting compressed IPv6 addresses like ::1 and fe80::1.

Key improvements:

  • Uses colonCount == 1 as the guard for port stripping instead of !IPV6_REGEX.matches()
  • Simpler invariant: IPv6 addresses always have 2+ colons, while hostname:port has exactly one
  • Fixes bug where compressed IPv6 forms weren't matched by the existing regex and were being corrupted
  • Adds comprehensive test coverage for compressed IPv6 forms

Impact:

  • Port stripping continues to work correctly for hostnames and IPv4 addresses with ports
  • Compressed IPv6 addresses are no longer corrupted by the port-stripping logic
  • Code is simpler and more maintainable than regex-based approach

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • The change is a clear improvement over the previous implementation. The colon-counting approach is simpler, more robust, and correctly handles all edge cases including compressed IPv6 forms, bracketed IPv6 with ports, and standard hostname:port patterns. The test coverage is appropriate and the change fixes a real bug without introducing new issues.
  • No files require special attention

Important Files Changed

Filename Overview
app/src/main/java/com/lxmf/messenger/util/validation/InputValidator.kt Replaced IPv6 regex check with simpler colon count logic for port stripping; correctly prevents corruption of compressed IPv6 addresses
app/src/test/java/com/lxmf/messenger/util/validation/InputValidatorTest.kt Added test for compressed IPv6 forms with appropriate caveats about regex limitations; updated existing test comment for clarity

Last reviewed commit: acc5324

@sentry
Copy link
Copy Markdown
Contributor

sentry bot commented Feb 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@torlando-tech torlando-tech merged commit 6ffdf1b into main Feb 24, 2026
14 checks passed
@torlando-tech torlando-tech deleted the fix/ipv6-port-strip-guard branch February 24, 2026 02:18
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