Skip to content

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

Merged
lizhengfeng101 merged 4 commits into
alibaba:mainfrom
REI-330:fix/config-set-llm-url-validation
Nov 24, 2026
Merged

lizhengfeng101 merged 4 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
Contributor

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.

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_TrimsURL now calls isolateLLMConnectionTest(t) and asserts the URL that was written to disk, so it no longer depends on a live endpoint.
  • TestApplyManualConfig_InvalidURL starts from a valid llm.url already saved to a temp config and asserts that both the in-memory config and the file survive the rejected update.
  • The manual tab (ocr config provider -> Manual) validates at the URL step with the same validateBaseURL, 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/v1 keeps working (new TestApplyManualConfig_AcceptsLocalhostURL), and ocr config set llm.url "" still clears the key. No stricter hostname validation was added: http://:8080 is 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 through handleManualFormEnter. Baseline is this PR's previous head 2a7e8d9; after is e6fb774.

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:

===== typo: scheme-less URL =====
Enter -> step 1 (0=URL, 1=Protocol)

  Manual Configuration

  URL: api.example.com/v1
  Protocol:
    > anthropic
      openai
      openai-responses
  Model:
  Auth Token:
  Auth Header: (Authorization)

After (e6fb774) - the form stays on the URL field and names the problem:

===== typo: scheme-less URL =====
Enter -> step 0 (0=URL, 1=Protocol)

  Manual Configuration

  URL:
    > api.example.com/v1
    Invalid URL: Base URL must use http or https scheme, got ""
  Protocol: anthropic
  Model:
  Auth Token:
  Auth Header: (Authorization)

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 trimmed http://localhost:8000/v1 at 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.524s
  • go vet ./cmd/opencodereview/: clean
  • gofmt -s -l cmd/opencodereview/: no output
  • go run scripts/verify-english-only.go: No unapproved non-English text in 915 scanned source files.

`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]>
@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.

Co-authored-by: lizhengfeng101 <[email protected]>
@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.

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]>
@REI-330
REI-330 force-pushed the fix/config-set-llm-url-validation branch from 2a7e8d9 to e6fb774 Compare November 23, 2026 01:50
@REI-330

REI-330 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

All three are in e6fb774, with the assertions you asked for.

  • TestApplyManualConfig_TrimsURL now calls isolateLLMConnectionTest(t) and checks the URL that reached the file, not just the in-memory struct.
  • TestApplyManualConfig_InvalidURL now starts from a valid llm.url already saved to a temp config and asserts that both the loaded config and the file are unchanged after the rejected update.
  • The manual tab validates at the URL step with the same validateBaseURL, keeps the cursor on the field and renders the error under the URL input, so a typo is fixed in place. The save-time check is unchanged.

http://localhost:8000/v1 still works, pinned by TestApplyManualConfig_AcceptsLocalhostURL, and ocr config set llm.url "" still clears the key. I left http://:8080 alone as you suggested.

The before/after captures of the form and the test commands are in the PR description.

@chaojixinren chaojixinren left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@lizhengfeng101
lizhengfeng101 merged commit 6e63a26 into alibaba:main Nov 24, 2026
12 checks passed
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.

3 participants