Repository navigation
Description
Context
Follow-up to #3154 / its PR, which added cipher encryption for LLM profile secrets in the profiles API. The two endpoints that wire a profile (or a profile-derived LLM) into a live conversation don't know about the cipher, so cipher-encrypted profiles created via the new flow can't actually be used by a conversation — the API key reaches the model provider as Fernet ciphertext and authentication fails.
Affected endpoints
1. POST /api/conversations/{id}/switch_profile (openhands-agent-server/openhands/agent_server/conversation_router.py:290)
Calls conversation.switch_profile(name) → LocalConversation.switch_profile (openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py:656) → self._profile_store.load(profile_name) at line 674. No cipher= is passed. With the cipher absent from the validation context, validate_secret doesn't decrypt — the resulting LLM carries api_key = SecretStr("gAAAAA…").
The same gap exists in conversation_service.py:971 (get_title_llm_from_profile), used for the title-LLM profile.
2. POST /api/conversations/{id}/switch_llm (conversation_router.py:322)
Receives llm: LLM = Body(..., embed=True). FastAPI parses this without a cipher in the validation context, so the natural FE flow — GET /api/profiles/X with X-Expose-Secrets: encrypted, then forward the response into switch_llm — lands the encrypted blob as the api_key and never decrypts it. The PR added a private _decrypt_incoming_secrets helper to profiles_router.save_profile for exactly this problem; it isn't reused here.
What to do in code
Fix 1 — thread cipher into LocalConversation profile loads
- Add an optional
cipher: Cipher | None = Noneparameter toLocalConversation.__init__and store it asself._cipher. - At the construction site in
conversation_service.py(where the conversation is built), passconfig.cipher(the same instance the routers use viaget_cipher). - In
LocalConversation.switch_profile, changeself._profile_store.load(profile_name)toself._profile_store.load(profile_name, cipher=self._cipher). - In
conversation_service.get_title_llm_from_profile, accept/lookup the cipher and passcipher=toprofile_store.load(...).
Fix 2 — share _decrypt_incoming_secrets and call it from switch_llm
- Lift the helper out of
profiles_router.pyinto a shared spot. Reasonable home: alongsideLLM_SECRET_FIELDSinopenhands-sdk/openhands/sdk/llm/llm.py(so both server and any non-router caller can use it), oragent_server/_secrets_exposure.pyif we want it server-only. - Update
switch_conversation_llminconversation_router.py:326to obtaincipher = get_cipher(request)(need to addrequest: Requestto the signature) and apply the helper beforeconversation.switch_llm(llm). - The helper's existing semantics already handle plaintext input untouched (Fernet
gAAAAA…prefix gates decrypt attempts), so backward compat for app-servers that supply plaintext is preserved.
Refactor opportunity (nice-to-have, same PR): the encrypted-blob normalization is now needed in three places (profiles save, conversation switch_llm, and arguably anywhere an LLM is parsed from an external HTTP body). Consider a small middleware-style helper or a Pydantic context flag that validators can opt into — but that's a wider design choice and probably warrants its own discussion.
Test plan
- Save a profile with
OH_SECRET_KEYconfigured (encrypted on disk), thenPOST /api/conversations/{id}/switch_profilewith that profile name; assert the conversation'sagent.llm.api_key.get_secret_value()equals the original plaintext, not the Fernet token. GET /api/profiles/XwithX-Expose-Secrets: encrypted, forward the body intoPOST /api/conversations/{id}/switch_llm, assert the same.- Same for the title-LLM profile path.
- Regression: plaintext
switch_llm(no cipher configured, or plaintext key with cipher configured) still works unchanged.
Activity