diff --git a/cmd/opencodereview/config_cmd.go b/cmd/opencodereview/config_cmd.go index 6d3129663..235145721 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, 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 { + 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 dd89d5b71..f55d2238f 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 59f467085..a9874c558 100644 --- a/cmd/opencodereview/provider_cmd.go +++ b/cmd/opencodereview/provider_cmd.go @@ -107,10 +107,16 @@ 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, 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) + } 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 e861aa828..176829c87 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 {