Skip to content

fix(config): validate and trim llm.url wherever it is written - #2039

Open
REI-330 wants to merge 2 commits into
alibaba:mainfrom
REI-330:fix/config-set-llm-url-validation
Open

REI-330 wants to merge 2 commits into
alibaba:mainfrom
REI-330:fix/config-set-llm-url-validation

Conversation

@REI-330

@REI-330 REI-330 commented Nov 22, 2026

Copy link
Copy Markdown

Problem

ocr config set llm.url stores 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 main at 78c2e0e, building ocr from that commit and running it against a throwaway USERPROFILE:

$ ocr config set llm.url gateway.internal.com/v1
Set llm.url = gateway.internal.com/v1

$ ocr config set llm.model gpt-4o
$ ocr config set llm.auth_token sk-test

$ ocr review --repo ./lab --effort low --concurrency 1
[ocr] Subtask error for group "small change set": LLM completion error: Post "/gateway.internal.com/v1/v1/messages": unsupported protocol scheme ""
[ocr] Summary: 2 file(s) reviewed, 0 comment(s), ~0 token(s) used, 13s elapsed
Error: review failed: all 2 file review(s) failed — check your LLM configuration and API key

The hint points at the configuration and the API key, but neither is the fault; the URL is. ocr llm test reports the same opaque message:

$ ocr llm test
Error: llm request failed: Post "/gateway.internal.com/v1/v1/messages": unsupported protocol scheme ""

Padding fails the same way. ocr config set llm.url 'https://example.com/v1 ' is accepted, and the trailing space reaches the request URL:

$ ocr llm test
Error: llm request failed: POST "https://example.com/v1%20/v1/messages": 405 Method Not Allowed

The provider path already avoids both outcomes:

$ ocr config set providers.gw.url 'not a url'
Error: invalid URL for providers.gw.url: Base URL must use http or https scheme, got ""
$ ocr config set providers.gw.url 'ftp://example.com/v1'
Error: invalid URL for providers.gw.url: Base URL must use http or https scheme, got "ftp"

Change

cmd/opencodereview/config_cmd.go: the llm.url case now trims the value and runs it through the same validateBaseURL the provider field uses, reporting it under the key the user typed. An empty value still clears the key.

cmd/opencodereview/provider_cmd.go: applyManualConfig is the second writer of llm.url (the manual tab of ocr config provider) and had the same gap; it now trims and validates the same way, so the two writers cannot disagree about what a valid llm.url is. 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 main build (78c2e0e) with the branch build, each against its own empty USERPROFILE:

ocr config set llm.url <value> before after
gateway.internal.com/v1 Set llm.url = …, saved Error: invalid URL for llm.url: Base URL must use http or https scheme, got "", exit 1, nothing saved
ftp://gateway.internal/v1 Set llm.url = …, saved Error: invalid URL for llm.url: Base URL must use http or https scheme, got "ftp", exit 1, nothing saved
https://gateway.internal:8000/v1 saved with the padding saved trimmed

The config file each run left behind:

// before
{ "llm": { "url": "  https://gateway.internal:8000/v1  " } }
// after
{ "llm": { "url": "https://gateway.internal:8000/v1" } }

Tests added:

  • TestSetConfigValueLlmURLTrimsAndValidates (cmd/opencodereview/config_cmd_test.go): trims a valid value, clears on empty, rejects api.example.com/v1, ftp://…, ://bad, and leaves the key unset.
  • TestApplyManualConfig_InvalidURL and TestApplyManualConfig_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 -w on the touched files produced no diff. make check is not runnable here (no make on 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-commit ocr review AGENTS.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 set validation coverage.

`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.
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

🔍 OpenCodeReview found 2 issue(s) in this PR.

  • ✅ Successfully posted inline: 2 comment(s)

Comment thread cmd/opencodereview/config_cmd.go Resolved
Comment thread cmd/opencodereview/provider_cmd.go Resolved
Both comments described the behavior this branch removes (a value saved
verbatim and failing later). Say what the code does now.
@lizhengfeng101

Copy link
Copy Markdown
Contributor

Thanks for catching this. I support validating llm.url before saving it. For compatibility, valid HTTP(S) endpoints, including http://localhost:8000/v1, should continue to work, and an empty value should still clear the setting. A rejected write should leave the previous configuration intact.

Before merging, please add isolateLLMConnectionTest(t) to TestApplyManualConfig_TrimsURL. This test should verify the saved URL without making a real network request. For the rejection case, please start with a valid URL already configured and assert that it survives the failed update.

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 http://:8080, to a separate PR. That changes a shared validator used by named providers too, and deserves its own compatibility review.

This branch has not been deployed

No deployments
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.

2 participants