Repository navigation
fix(config): validate and trim llm.url wherever it is written - #2039
Conversation
`ocr config set providers.<name>.url` and `mcp_servers.<name>.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. Co-authored-by: lizhengfeng101 <[email protected]>
|
🔍 OpenCodeReview found 2 issue(s) in this PR.
|
Both comments described the behavior this branch removes (a value saved verbatim and failing later). Say what the code does now. Co-authored-by: lizhengfeng101 <[email protected]>
|
Thanks for catching this. I support validating Before merging, please add For the manual wizard, I’d prefer showing the error at the URL field so users can correct a typo without re-entering their model and credentials. Please keep the save-time check as well. I’d leave stricter hostname validation, such as rejecting |
The URL field only refused an empty value, so a typo such as a scheme-less address advanced to the protocol step and failed only after the model and credentials had been typed in. Validate the value where it is entered and render the error under the active field. Co-authored-by: lizhengfeng101 <[email protected]>
2a7e8d9 to
e6fb774
Compare
|
All three are in e6fb774, with the assertions you asked for.
The before/after captures of the form and the test commands are in the PR description. |
Problem
ocr config set llm.urlstores the value verbatim, while the other two URL keys of the same command (providers.<name>.url,mcp_servers.<name>.url) reject a value without an http(s) scheme and trim padding. The gap is silent: the bad value is accepted and written to~/.opencodereview/config.json, and only the first LLM request fails, with a message that names neither the config key nor the command that wrote it.Reproduced on
mainat 78c2e0e, buildingocrfrom that commit and running it against a throwawayUSERPROFILE:The hint points at the configuration and the API key, but neither is the fault; the URL is.
ocr llm testreports the same opaque message:Padding fails the same way.
ocr config set llm.url 'https://example.com/v1 'is accepted, and the trailing space reaches the request URL:The provider path already avoids both outcomes:
Change
cmd/opencodereview/config_cmd.go: thellm.urlcase now trims the value and runs it through the samevalidateBaseURLthe provider field uses, reporting it under the key the user typed. An empty value still clears the key.cmd/opencodereview/provider_cmd.go:applyManualConfigis the second writer ofllm.url(the manual tab ofocr config provider) and had the same gap; it now trims and validates the same way, so the two writers cannot disagree about what a validllm.urlis. A rejected value leaves the loaded config untouched, which is what the existing provider-write tests already assert for the other fields.A value that was already valid and unpadded is stored exactly as before.
Verification
Manual, on Windows, comparing the
mainbuild (78c2e0e) with the branch build, each against its own emptyUSERPROFILE:ocr config set llm.url <value>gateway.internal.com/v1Error: invalid URL for llm.url: Base URL must use http or https scheme, got "", exit 1, nothing savedftp://gateway.internal/v1Error: invalid URL for llm.url: Base URL must use http or https scheme, got "ftp", exit 1, nothing savedhttps://gateway.internal:8000/v1The config file each run left behind:
Tests added:
TestSetConfigValueLlmURLTrimsAndValidates(cmd/opencodereview/config_cmd_test.go): trims a valid value, clears on empty, rejectsapi.example.com/v1,ftp://鈥,://bad`, and leaves the key unset.TestApplyManualConfig_InvalidURLandTestApplyManualConfig_TrimsURL(cmd/opencodereview/provider_cmd_test.go): the manual-config writer rejects the same values without mutating the config, and stores a padded value trimmed.Run on this branch:
go build ./...: clean.go vet ./cmd/opencodereview/: clean.go run scripts/verify-english-only.go:No unapproved non-English text in 915 scanned source files.go test -count=1 ./cmd/opencodereview/:ok 鈥?128.131s(whole package, verbose).go test -race -count=1 ./cmd/opencodereview/: ok.gofmt -s -won the touched files produced no diff.make checkis not runnable here (nomakeon Windows), so its three steps 鈥?license-check, english-check,go mod tidy+go vet鈥?were run individually as listed (the license script is unchanged by this PR: no new files). The pre-commitocr reviewAGENTS.md requires could not run: this environment has no LLM endpoint configured.AI disclosure
Claude (via DeepSeek Harness, model
deepseek-v4.1-flash) drafted this change and the tests. I reviewed every line, ran the before/after comparison and the test commands above myself, and can answer questions about the change without AI assistance.Related Issues
No tracking issue; found while auditing
ocr config setvalidation coverage.Update (e6fb774): the manual wizard reports a bad URL at the field
Review feedback asked for three things before merging, and they are all in this revision:
TestApplyManualConfig_TrimsURLnow callsisolateLLMConnectionTest(t)and asserts the URL that was written to disk, so it no longer depends on a live endpoint.TestApplyManualConfig_InvalidURLstarts from a validllm.urlalready saved to a temp config and asserts that both the in-memory config and the file survive the rejected update.ocr config provider-> Manual) validates at the URL step with the samevalidateBaseURL, keeps the cursor on the field, and renders the error under the URL input instead of below the whole form. The save-time check stays, so a config edited outside the wizard still fails closed.http://localhost:8000/v1keeps working (newTestApplyManualConfig_AcceptsLocalhostURL), andocr config set llm.url ""still clears the key. No stricter hostname validation was added:http://:8080is still accepted, since that changes a shared validator used by named providers too.Before / after: real form view after pressing Enter on a bad URL
Both captures come from the manual form's own
View()render (the same string the terminal draws), driven throughhandleManualFormEnter. Baseline is this PR's previous head2a7e8d9; after ise6fb774.Before (
2a7e8d9) - a scheme-less URL advances to the protocol step with no warning; the user finds out only after typing the model and credentials, when the save-time check fires:After (
e6fb774) - the form stays on the URL field and names the problem:An empty URL used to do nothing at all when Enter was pressed; it now reports
URL is required (for example https://api.example.com/v1)under the same field.Tests in this revision
TestApplyManualConfig_TrimsURL: isolated connection, on-disk URL asserted.TestApplyManualConfig_InvalidURL: valid URL pre-saved, rejected write leaves memory and disk untouched.TestApplyManualConfig_AcceptsLocalhostURL: localhost compatibility pinned.TestHandleManualFormEnter_Steps: empty URL, scheme-less URL, and trimmedhttp://localhost:8000/v1at the URL step.TestProviderTUIView_ManualForm_URLErrorUnderField: the error is rendered above the Protocol field.Commands run on
e6fb774(Windows, Go 1.26.10):go test ./cmd/opencodereview/ -count=1:ok github.com/alibaba/open-code-review/cmd/opencodereview 127.524sgo vet ./cmd/opencodereview/: cleangofmt -s -l cmd/opencodereview/: no outputgo run scripts/verify-english-only.go:No unapproved non-English text in 915 scanned source files.