Skip to content

fix: strip port from pasted URLs, sanitize RNode/UDP hosts#537

Merged
torlando-tech merged 2 commits intomainfrom
fix/hostname-port-strip
Feb 23, 2026
Merged

fix: strip port from pasted URLs, sanitize RNode/UDP hosts#537
torlando-tech merged 2 commits intomainfrom
fix/hostname-port-strip

Conversation

@torlando-tech
Copy link
Copy Markdown
Owner

Summary

Follow-up to #532, addressing Greptile review feedback:

  • Strip trailing :port from hostnames (e.g. http://example.com:8080example.com)
  • Use cleaned values from validateHostname() in RNode TCP and UDP entityToConfig() branches (previously discarded)
  • Strip http:// and https:// in UI text field onValueChange handlers for immediate visual feedback

Test plan

  • 2 new unit tests for port stripping (URL with port, bare hostname with port)
  • All existing InputValidator, InterfaceRepository, and InterfaceManagementViewModel tests pass
  • Manual: paste http://example.com:8080 in TCP host field, verify it becomes example.com

🤖 Generated with Claude Code

…vel stripping

Address Greptile review feedback on #533:
- Strip trailing :port from hostnames (e.g. "example.com:8080" → "example.com")
- Use cleaned values from validateHostname() in RNode TCP and UDP entityToConfig branches
- Strip http:// and https:// in UI text field onValueChange handlers for immediate feedback

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

This PR enhances hostname/IP validation by stripping port numbers from user input (e.g., http://example.com:8080example.com).

  • Port stripping logic added to validateHostname() with regex protection for IPv6 addresses
  • UI handlers now strip http:///https:// prefixes for immediate visual feedback
  • RNode TCP and UDP validation now uses the cleaned values from validateHostname() instead of discarding them
  • Two new unit tests verify port stripping for URLs and bare hostnames

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The changes are well-tested, focused, and properly handle edge cases like IPv6 addresses; the port stripping logic uses regex to avoid breaking IPv6, and cleaned values are now correctly used throughout
  • No files require special attention

Important Files Changed

Filename Overview
app/src/main/java/com/lxmf/messenger/util/validation/InputValidator.kt Added port stripping logic to validateHostname() that strips :port suffix from hostnames while preserving IPv6 addresses
app/src/main/java/com/lxmf/messenger/repository/InterfaceRepository.kt Updated RNode TCP and UDP branches to use cleaned values from validateHostname() instead of discarding them
app/src/main/java/com/lxmf/messenger/ui/components/InterfaceConfigDialog.kt Added UI-level stripping of http:// and https:// prefixes in onValueChange handlers for immediate visual feedback
app/src/test/java/com/lxmf/messenger/util/validation/InputValidatorTest.kt Added 2 new unit tests for port stripping: URL with port and bare hostname with port

Last reviewed commit: ce385b7

Add IPV6_REGEX check before port-stripping to prevent corrupting
IPv6 addresses like "2001:db8::1" where the trailing ":1" looks
like a port number.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment on lines +295 to +299
// Strip trailing :port (e.g. "example.com:8080" → "example.com")
// but not from IPv6 addresses which use colons extensively
if (!cleaned.startsWith("[") && !IPV6_REGEX.matches(cleaned) && cleaned.matches(Regex("^.+:\\d+$"))) {
cleaned = cleaned.substringBeforeLast(":")
}
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.

Bug: The IPV6_REGEX does not match compressed IPv6 addresses, causing validateHostname to incorrectly truncate valid addresses by treating part of the address as a port number.
Severity: CRITICAL

Suggested Fix

Update the IPV6_REGEX in ValidationConstants.kt to a more comprehensive pattern that correctly validates all standard IPv6 address formats, including compressed notation (e.g., 2001:db8::1). Consider using a well-vetted, standard regular expression for IPv6 validation to cover all edge cases.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location:
app/src/main/java/com/lxmf/messenger/util/validation/InputValidator.kt#L295-L299

Potential issue: The regular expression `IPV6_REGEX` fails to match compressed IPv6
addresses that use `::` notation (e.g., `2001:db8::1`). When `validateHostname` is
called with such an address, the check `!IPV6_REGEX.matches(cleaned)` incorrectly
returns `true`. This, combined with other conditions, triggers logic that mistakenly
identifies the last segment of the IPv6 address as a port number. As a result, the
function incorrectly truncates the address, for example, turning `2001:db8::1` into the
invalid address `2001:db8:`. The existing tests do not cover compressed IPv6 formats,
which is why this issue was not caught.

Did we get this right? 👍 / 👎 to inform future reviews.

@torlando-tech torlando-tech merged commit 9dedf9a into main Feb 23, 2026
12 checks passed
@torlando-tech torlando-tech deleted the fix/hostname-port-strip branch February 23, 2026 23:14
@sentry
Copy link
Copy Markdown
Contributor

sentry bot commented Feb 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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