Skip to content

[relay] Preserve non-standard port in WS dialer URL prep#6061

Open
lixmal wants to merge 4 commits intomainfrom
fix/relay-healthcheck-non-standard-port
Open

[relay] Preserve non-standard port in WS dialer URL prep#6061
lixmal wants to merge 4 commits intomainfrom
fix/relay-healthcheck-non-standard-port

Conversation

@lixmal
Copy link
Copy Markdown
Collaborator

@lixmal lixmal commented May 4, 2026

Describe your changes

Rewrite prepareURL in the relay WS dialer to use net/url parsing instead of a strings.Replace based scheme rewrite, and add tests covering non-standard ports and other edge cases.

  • Parse the relay address with url.Parse, switch on the scheme, and set the path explicitly
  • Add table-driven tests for ports, IPs, hostnames starting with rel, and rejected schemes

Issue ticket number and link

#6057

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Internal refactor with no user-visible API change.

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__

Summary by CodeRabbit

  • Bug Fixes

    • Improved WebSocket endpoint handling with stricter scheme validation, more reliable host/port/path construction, and clearer error reporting for invalid or unsupported inputs.
  • Tests

    • Added comprehensive tests for URL conversion and validation, covering hostnames, ports, IPv4/IPv6 formats, and malformed or unsupported URLs.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 4, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 62d6c91f-a494-4d55-aacc-8698ead91ec5

📥 Commits

Reviewing files that changed from the base of the PR and between a61a04a and 67dc18e.

📒 Files selected for processing (1)
  • shared/relay/client/dialer/ws/ws.go

📝 Walkthrough

Walkthrough

Replaces string-based WebSocket URL rewriting with a url.Parse-based prepareURL that validates rel/rels, maps them to ws/wss, sets the relay path, and returns explicit errors; Dial now uses the produced URL directly. Adds table-driven tests for mappings and error cases.

Changes

WebSocket URL Preparation Refactoring

Layer / File(s) Summary
Data Shape / Parsing
shared/relay/client/dialer/ws/ws.go
prepareURL now uses net/url to parse input, validates rel/rels schemes, maps them to ws/wss, sets parsed.Path = relay.WebSocketURLPath, and returns explicit parse/unsupported-scheme errors. Removed strings-based logic.
Core Dial Flow
shared/relay/client/dialer/ws/ws.go
Dial calls prepareURL and dials the returned wsURL directly (previous reparse-and-overwrite removed).
Tests / Verification
shared/relay/client/dialer/ws/ws_test.go
Adds TestPrepareURL table-driven tests verifying scheme translation (relws, relswss), host and port preservation (including IPv4/IPv6 and non-standard ports), /relay path presence, and error cases for unsupported or missing schemes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers

  • mlsmaycon

Poem

🐰 I parsed the paths with gentle care,
rels to wss, rel to ws — prepare,
Hosts and ports kept snug and tight,
Tests hop forward in morning light,
A rabbit cheers the dialer's flight.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ 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%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly describes the main change: rewriting prepareURL to preserve non-standard ports in WS dialer URL preparation, which aligns with the core objective.
Description check ✅ Passed The description follows the template structure, includes a clear summary of changes, references issue #6057, and properly completes the checklist with explanations for documentation and test coverage.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/relay-healthcheck-non-standard-port

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.

❤️ Share

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

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 4, 2026

Release artifacts

Built for PR head 67dc18e in workflow run #14952.

Artifact Link
All release artifacts Download
Linux packages Download
Windows packages Download
macOS packages Download
UI artifacts Download
UI macOS artifacts Download

GHCR images (amd64)

This comment is updated by the Release workflow. Artifact links expire according to the workflow retention policy.

Copy link
Copy Markdown
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

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@shared/relay/client/dialer/ws/ws.go`:
- Around line 57-70: After parsing the relay address in ws.go, ensure the parsed
URL contains an authority host before returning: after the scheme mapping (cases
for "rel"/"rels") add a check like if parsed.Host == "" (or parsed.Hostname() ==
"") return "", fmt.Errorf("missing host in relay address %q", address) so opaque
inputs like "rel:example.com:33080" are rejected; keep the rest of the logic
(set parsed.Path = relay.WebSocketURLPath and return parsed.String()) unchanged
and reference the existing parsed variable and the function that constructs the
WS URL.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1f91ec78-3f9d-4a38-bc9c-c772f5fd6695

📥 Commits

Reviewing files that changed from the base of the PR and between abccbde and a61a04a.

📒 Files selected for processing (1)
  • shared/relay/client/dialer/ws/ws.go

Comment thread shared/relay/client/dialer/ws/ws.go
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 6, 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.

1 participant