From a921fae5b0624db3ebe9dbaaa6e3160cbff4f382 Mon Sep 17 00:00:00 2001 From: REI-330 <173389910+REI-330@users.noreply.github.com> Date: Sun, 22 Nov 2026 12:10:23 +0800 Subject: [PATCH 1/2] fix(config): validate and trim llm.url in ocr config set `ocr config set providers..url` and `mcp_servers..url` both reject a value without an http(s) scheme and trim surrounding padding. `ocr config set llm.url` wrote the raw string through, so a value like `gateway.internal.com/v1` was saved as-is and only surfaced later as `Post "/gateway.internal.com/v1/v1/messages": unsupported protocol scheme ""` from the first review request; a padded value survived into the request URL as `%20`. Route the key through the same validateBaseURL the provider path uses, and validate the manual-config writer in provider_cmd.go the same way so the two writers of llm.url cannot disagree. --- cmd/opencodereview/config_cmd.go | 11 +++++++- cmd/opencodereview/config_cmd_test.go | 37 +++++++++++++++++++++++++ cmd/opencodereview/provider_cmd.go | 9 +++++- cmd/opencodereview/provider_cmd_test.go | 28 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/cmd/opencodereview/config_cmd.go b/cmd/opencodereview/config_cmd.go index 6d31296631..aa5cd68db4 100644 --- a/cmd/opencodereview/config_cmd.go +++ b/cmd/opencodereview/config_cmd.go @@ -698,7 +698,16 @@ func setConfigValue(cfg *Config, key, value string) error { } cfg.Effort = string(e) case "llm.url", "llm.URL": - cfg.Llm.URL = value + // Same treatment as providers..url: an untrimmed or scheme-less + // value is accepted verbatim here and only surfaces later as an opaque + // "unsupported protocol scheme" from the LLM request. + trimmedURL := strings.TrimSpace(value) + if trimmedURL != "" { + if err := validateBaseURL(trimmedURL); err != nil { + return fmt.Errorf("invalid URL for %s: %w", key, err) + } + } + cfg.Llm.URL = trimmedURL case "llm.auth_token", "llm.AuthToken": cfg.Llm.AuthToken = value case "llm.auth_token_cmd", "llm.AuthTokenCmd": diff --git a/cmd/opencodereview/config_cmd_test.go b/cmd/opencodereview/config_cmd_test.go index dd89d5b715..f55d2238f9 100644 --- a/cmd/opencodereview/config_cmd_test.go +++ b/cmd/opencodereview/config_cmd_test.go @@ -1129,6 +1129,43 @@ func TestSetConfigValueLlmURL(t *testing.T) { } } +func TestSetConfigValueLlmURLTrimsAndValidates(t *testing.T) { + t.Run("trims a valid URL before storing", func(t *testing.T) { + cfg := &Config{} + + if err := setConfigValue(cfg, "llm.url", " https://gateway.internal:8000/v1 "); err != nil { + t.Fatalf("setConfigValue: %v", err) + } + if got := cfg.Llm.URL; got != "https://gateway.internal:8000/v1" { + t.Errorf("URL = %q, want trimmed URL", got) + } + }) + + t.Run("clears with an empty value", func(t *testing.T) { + cfg := &Config{} + cfg.Llm.URL = "https://old.example/v1" + + if err := setConfigValue(cfg, "llm.url", ""); err != nil { + t.Fatalf("setConfigValue: %v", err) + } + if cfg.Llm.URL != "" { + t.Errorf("URL = %q, want empty to clear the key", cfg.Llm.URL) + } + }) + + for _, value := range []string{"api.example.com/v1", "ftp://gateway.internal/v1", "://bad"} { + t.Run("rejects "+value, func(t *testing.T) { + cfg := &Config{} + if err := setConfigValue(cfg, "llm.url", value); err == nil { + t.Fatalf("setConfigValue accepted invalid URL %q", value) + } + if cfg.Llm.URL != "" { + t.Errorf("URL = %q, want the rejected value left unset", cfg.Llm.URL) + } + }) + } +} + func TestSetConfigValueLlmAuthToken(t *testing.T) { cfg := &Config{} if err := setConfigValue(cfg, "llm.auth_token", "tok-123"); err != nil { diff --git a/cmd/opencodereview/provider_cmd.go b/cmd/opencodereview/provider_cmd.go index 59f467085a..c885b239db 100644 --- a/cmd/opencodereview/provider_cmd.go +++ b/cmd/opencodereview/provider_cmd.go @@ -107,10 +107,17 @@ func applyManualConfig(configPath string, cfg *Config, result providerTUIResult) if result.model == "" { return fmt.Errorf("model is required for manual configuration") } + // The other writer of llm.url (ocr config set) validates the same way; a + // scheme-less or padded value here would be saved and only fail later as an + // opaque "unsupported protocol scheme" from the LLM request. + baseURL := strings.TrimSpace(result.url) + if err := validateBaseURL(baseURL); err != nil { + return fmt.Errorf("invalid URL for manual configuration: %w", err) + } cfg.Provider = "" cfg.Model = "" - cfg.Llm.URL = result.url + cfg.Llm.URL = baseURL cfg.Llm.Model = result.model cfg.Llm.AuthToken = result.apiKey authHeader, err := llm.NormalizeAuthHeader(result.authHeader) diff --git a/cmd/opencodereview/provider_cmd_test.go b/cmd/opencodereview/provider_cmd_test.go index e861aa8280..176829c87e 100644 --- a/cmd/opencodereview/provider_cmd_test.go +++ b/cmd/opencodereview/provider_cmd_test.go @@ -282,6 +282,34 @@ func TestApplyManualConfig_MissingModel(t *testing.T) { } } +func TestApplyManualConfig_InvalidURL(t *testing.T) { + for _, value := range []string{"api.example.com/v1", "ftp://gateway.internal/v1"} { + t.Run(value, func(t *testing.T) { + cfg := &Config{Provider: "openai", Model: "saved-model"} + err := applyManualConfig(filepath.Join(t.TempDir(), "config.json"), cfg, providerTUIResult{url: value, model: "some-model"}) + if err == nil { + t.Fatal("expected error for invalid URL") + } + if cfg.Provider != "openai" || cfg.Model != "saved-model" || cfg.Llm.URL != "" { + t.Fatalf("configuration changed after rejected write: %#v", cfg) + } + }) + } +} + +func TestApplyManualConfig_TrimsURL(t *testing.T) { + configPath := filepath.Join(t.TempDir(), "config.json") + cfg := &Config{} + + result := providerTUIResult{url: " https://gateway.internal:8000/v1 ", model: "some-model", apiKey: "sk-test", protocol: "anthropic"} + if err := applyManualConfig(configPath, cfg, result); err != nil { + t.Fatalf("applyManualConfig: %v", err) + } + if cfg.Llm.URL != "https://gateway.internal:8000/v1" { + t.Errorf("llm.url = %q, want the trimmed URL", cfg.Llm.URL) + } +} + func TestApplyCustomProviderConfig_MissingProvider(t *testing.T) { err := applyCustomProviderConfig("", &Config{}, providerTUIResult{provider: "", model: "m"}) if err == nil { From 2a7e8d9722e7b242b0d8d9d1370da9c146a86787 Mon Sep 17 00:00:00 2001 From: REI-330 <173389910+REI-330@users.noreply.github.com> Date: Sun, 22 Nov 2026 12:30:45 +0800 Subject: [PATCH 2/2] fix(config): reword the llm.url comments to match the change Both comments described the behavior this branch removes (a value saved verbatim and failing later). Say what the code does now. --- cmd/opencodereview/config_cmd.go | 6 +++--- cmd/opencodereview/provider_cmd.go | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/cmd/opencodereview/config_cmd.go b/cmd/opencodereview/config_cmd.go index aa5cd68db4..235145721e 100644 --- a/cmd/opencodereview/config_cmd.go +++ b/cmd/opencodereview/config_cmd.go @@ -698,9 +698,9 @@ func setConfigValue(cfg *Config, key, value string) error { } cfg.Effort = string(e) case "llm.url", "llm.URL": - // Same treatment as providers..url: an untrimmed or scheme-less - // value is accepted verbatim here and only surfaces later as an opaque - // "unsupported protocol scheme" from the LLM request. + // Same treatment as providers..url, so a scheme-less or padded + // value is refused here rather than surfacing as an opaque + // "unsupported protocol scheme" on the first LLM request. trimmedURL := strings.TrimSpace(value) if trimmedURL != "" { if err := validateBaseURL(trimmedURL); err != nil { diff --git a/cmd/opencodereview/provider_cmd.go b/cmd/opencodereview/provider_cmd.go index c885b239db..a9874c558b 100644 --- a/cmd/opencodereview/provider_cmd.go +++ b/cmd/opencodereview/provider_cmd.go @@ -107,9 +107,8 @@ func applyManualConfig(configPath string, cfg *Config, result providerTUIResult) if result.model == "" { return fmt.Errorf("model is required for manual configuration") } - // The other writer of llm.url (ocr config set) validates the same way; a - // scheme-less or padded value here would be saved and only fail later as an - // opaque "unsupported protocol scheme" from the LLM request. + // The other writer of llm.url (ocr config set) validates the same way, so + // the two cannot disagree about what a valid value is. baseURL := strings.TrimSpace(result.url) if err := validateBaseURL(baseURL); err != nil { return fmt.Errorf("invalid URL for manual configuration: %w", err)