Skip to content

fix(config): validate relay_url scheme (ws/wss) at config load - #94

Open
Jah-yee wants to merge 1 commit into
nudgebee:mainfrom
Jah-yee:fix/relay-url-scheme-validation
Open

Jah-yee wants to merge 1 commit into
nudgebee:mainfrom
Jah-yee:fix/relay-url-scheme-validation

Conversation

@Jah-yee

@Jah-yee Jah-yee commented Jun 16, 2026

Copy link
Copy Markdown

Validates relay_url scheme in Load() after unmarshaling, rejecting non-ws/wss schemes (https, http, ftp, etc.) with a clear error message.

Closes #91

@cla-assistant

cla-assistant Bot commented Jun 16, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds validation for the relay_url configuration parameter to ensure it uses the ws:// or wss:// scheme, along with unit tests to verify this behavior. The review feedback points out a bug in the test suite where the invalid URL test case fails on the scheme check rather than the parsing check, and suggests using a truly unparseable URL. Additionally, the reviewer recommends replacing the custom substring search helper function with the standard library's strings.Contains function.

Comment thread pkg/config/config_test.go
Comment on lines +39 to +44
name: "not a URL",
relayURL: "not-a-url",
wantErr: true,
errMsg: "relay_url must be a valid URL",
},
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The string "not-a-url" is successfully parsed by url.Parse as a relative path (with an empty scheme). As a result, it fails the scheme check (u.Scheme != "ws" && u.Scheme != "wss") rather than the url.Parse check, returning the error "relay_url must use ws:// or wss:// scheme..." instead of "relay_url must be a valid URL". This causes this test case to fail.

To properly test the url.Parse error path, use an actually unparseable URL like "ws://[::1".

		{
			name:     "not a URL",
			relayURL: "ws://[::1",
			wantErr:  true,
			errMsg:   "relay_url must be a valid URL",
		},

Comment thread pkg/config/config_test.go
Comment on lines +3 to +7
import (
"os"
"path/filepath"
"testing"
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use the standard library's strings package instead of implementing a custom substring search function.

Suggested change
import (
"os"
"path/filepath"
"testing"
)
import (
"os"
"path/filepath"
"strings"
"testing"
)

Comment thread pkg/config/config_test.go Outdated
if tt.wantErr && tt.errMsg != "" {
if err == nil {
t.Errorf("Load() expected error containing %q, got nil", tt.errMsg)
} else if !containsString(err.Error(), tt.errMsg) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Replace the custom containsString helper with strings.Contains from the standard library.

Suggested change
} else if !containsString(err.Error(), tt.errMsg) {
} else if !strings.Contains(err.Error(), tt.errMsg) {

Comment thread pkg/config/config_test.go Outdated
Comment on lines +74 to +81
func containsString(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove the custom containsString helper function as it is no longer needed.

@blue4209211

Copy link
Copy Markdown
Contributor

@Jah-yee can please sign CLA
#94 (comment)

@Jah-yee
Jah-yee force-pushed the fix/relay-url-scheme-validation branch from 60b63d2 to ff67cac Compare October 25, 2026 07:53
Validate that relay_url uses the ws:// or wss:// scheme in config.Load,
and add table-driven tests covering the valid and invalid cases.

The unparseable-URL test case uses "ws://[::1" to exercise the
url.Parse error path, and "not-a-url" is covered separately as a
missing-scheme case. The test uses strings.Contains instead of a
custom substring helper.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Jah-yee
Jah-yee force-pushed the fix/relay-url-scheme-validation branch from ff67cac to 0b29d16 Compare October 25, 2026 07:55
@Jah-yee

Jah-yee commented Oct 25, 2026

Copy link
Copy Markdown
Author

Thanks for the review notes on pkg/config/config_test.go — both points were valid and I have pushed a fix in 0b29d16 (rebased onto upstream main).

  1. The "not a URL" case was indeed wrong. url.Parse("not-a-url") succeeds and returns a relative path with an empty scheme, so that input never reached the url.Parse error branch — it failed the scheme check instead, and the expected message relay_url must be a valid URL never matched. I changed that case to ws://[::1 (genuinely unparseable, so it exercises the url.Parse error path) and added a separate case for not-a-url that asserts relay_url must use ws:// or wss:// scheme.

  2. The custom containsString helper is gone; the assertion now uses strings.Contains from the standard library, and the strings import is added accordingly.

Verification: go test ./pkg/config/... passes locally (Go 1.25), gofmt -l pkg/config/ is clean, and the branch is a single commit on top of upstream main touching only pkg/config/config.go (12 added lines) and pkg/config/config_test.go (79 added lines).

One open item remains: @blue4209211 asked me to sign the CLA, and the cla-assistant[bot] check on this PR is still not_signed. The cla-assistant.io sign-in flow needs an interactive GitHub OAuth authorization that I could not complete here, so the CLA is still unsigned on my side. If there is a specific sign step (or a re-authorization link for the CLA assistant on the Jah-yee account) you would like me to use, please let me know — I do not want to leave the PR stalled on it.

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.

Validate relay_url scheme at config load

2 participants