Skip to content

fix(utils): santize host when target has host port#6759

Merged
dwisiswant0 merged 1 commit intodevfrom
fix/sanitize-host
Jan 19, 2026
Merged

fix(utils): santize host when target has host port#6759
dwisiswant0 merged 1 commit intodevfrom
fix/sanitize-host

Conversation

@knakul853
Copy link
Contributor

@knakul853 knakul853 commented Jan 7, 2026

closes #6758

Proposed changes

Proof

Checklist

  • Pull request is created against the dev branch
  • All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Summary by CodeRabbit

  • Refactor

    • Enhanced host and port extraction normalization to improve handling of IPv6 addresses and various host formats.
  • Tests

    • Added comprehensive test coverage for URL host/port extraction logic across various scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

@auto-assign auto-assign bot requested a review from dwisiswant0 January 7, 2026 19:48
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 7, 2026

Walkthrough

Adds host:port normalization logic via a new extractHostPort() function to properly separate hostname and port components from URLs. Updates GetJsonFieldsFromURL and GetJsonFieldsFromMetaInput to use normalized host values for field assignment, addressing the bug where port was incorrectly included in the host field.

Changes

Cohort / File(s) Summary
Host/Port Extraction Logic
pkg/protocols/utils/fields.go
Introduces extractHostPort() function to parse and normalize host:port pairs, handling IPv6 literals and bracketed formats. Updates GetJsonFieldsFromURL and GetJsonFieldsFromMetaInput to compute normalized host and port via this function before setting Fields values. Adds imports for net and strings packages.
Test Coverage
pkg/protocols/utils/fields_test.go
New test file with two parallel test suites: TestGetJsonFieldsFromURL_HostPortExtraction validates host/port extraction from various URL formats (with/without schemes, IPv4/IPv6, localhost); TestExtractHostPort validates the lower-level extraction function with direct host/port inputs and edge cases.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A port and a host, once tangled as one,
Now neatly divorced—separation is done!
IPv6 brackets and parsing so tight,
The fields align now, everything's right.
Hops proudly

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed The PR successfully implements host/port extraction and normalization logic to ensure host fields contain only hostnames without ports, directly addressing issue #6758's requirement.
Out of Scope Changes check ✅ Passed All changes are scoped to host/port extraction and normalization in fields.go and corresponding tests in fields_test.go, with no unrelated modifications detected.
Title check ✅ Passed The title directly addresses the main change: sanitizing the host field when a target includes an explicit host:port. This matches the core objective of fixing the issue where host was incorrectly including the port.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

🤖 Fix all issues with AI agents
In @pkg/protocols/utils/fields_test.go:
- Around line 9-72: Add a new table-driven test case in
TestGetJsonFieldsFromURL_HostPortExtraction that covers a URL with an explicit
standard HTTPS port (e.g., "https://example.com:443/path") so
GetJsonFieldsFromURL is validated against the exact bug; set name to something
like "https with explicit 443", input to "https://example.com:443/path",
expectedHost to "example.com" and expectedPort to "443", then run the existing
assertions (assert.Equal on fields.Host and fields.Port) to ensure the function
returns the correct values.
🧹 Nitpick comments (1)
pkg/protocols/utils/fields.go (1)

84-107: Consider adding documentation for port precedence logic.

The implementation correctly handles various host formats (standard host:port, IPv6 with/without brackets). However, the port precedence behavior (lines 90-92, 101-102) where an existing port parameter takes precedence over a port extracted from host could benefit from a comment explaining this design choice.

📝 Suggested documentation
+// extractHostPort separates hostname and port from a host string.
+// If port parameter is non-empty, it takes precedence over any port in the host string.
+// Handles IPv6 addresses with brackets (e.g., "[::1]:8080" or "[::1]").
 func extractHostPort(host, port string) (string, string) {
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between aa16e5e and 0914281.

📒 Files selected for processing (2)
  • pkg/protocols/utils/fields.go
  • pkg/protocols/utils/fields_test.go
🧰 Additional context used
📓 Path-based instructions (2)
**/*.go

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.go: Format Go code using go fmt ./...
Run static analysis using go vet ./... on Go code

Files:

  • pkg/protocols/utils/fields.go
  • pkg/protocols/utils/fields_test.go
pkg/protocols/**/*.go

📄 CodeRabbit inference engine (CLAUDE.md)

pkg/protocols/**/*.go: Each protocol implementation should implement the Request interface with Compile(), ExecuteWithResults(), Match(), and Extract() methods
Protocol implementations should embed Operators for matching/extraction functionality

Files:

  • pkg/protocols/utils/fields.go
  • pkg/protocols/utils/fields_test.go
🧬 Code graph analysis (1)
pkg/protocols/utils/fields_test.go (2)
pkg/protocols/utils/fields.go (1)
  • GetJsonFieldsFromURL (23-50)
pkg/protocols/utils/variables.go (2)
  • Host (45-45)
  • Port (46-46)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Lint
🔇 Additional comments (4)
pkg/protocols/utils/fields.go (3)

4-5: LGTM: Necessary imports for host/port parsing.

The net and strings packages are appropriately used in the extractHostPort function for host/port separation and IPv6 bracket handling.


35-48: LGTM: Correctly normalizes host field.

The host normalization logic properly addresses the bug reported in issue #6758. By extracting and using the normalized host value (without port) for field assignments, the Host field will now contain only the hostname while Port contains the port number separately.


67-80: LGTM: Consistent host normalization.

The changes mirror the normalization logic in GetJsonFieldsFromURL, ensuring consistent behavior across both functions.

pkg/protocols/utils/fields_test.go (1)

74-138: LGTM: Comprehensive unit tests for extractHostPort.

The test cases provide good coverage of the host/port extraction logic, including:

  • Standard host:port parsing
  • IPv6 bracket formats (with/without port)
  • Port precedence behavior (line 99-104)
  • IPv4 addresses

The port precedence test (line 99-104) is particularly valuable as it documents that an existing port parameter takes precedence over port extracted from the host string.

@dwisiswant0 dwisiswant0 changed the title fix: santize host when target has host port fix(utils): santize host when target has host port Jan 19, 2026
@dwisiswant0 dwisiswant0 merged commit 6cd2749 into dev Jan 19, 2026
19 checks passed
@dwisiswant0 dwisiswant0 deleted the fix/sanitize-host branch January 19, 2026 05:14
@dwisiswant0 dwisiswant0 added this to the v3.7.0 milestone Jan 19, 2026
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.

[BUG] ResultEvent.Host contains port when URL has explicit port

2 participants