Skip to content

agent-server: cipher-encrypted LLM profiles can't be used by conversations (switch_profile/switch_llm don't decrypt) #3164

Closed

Description

@VascoSch92

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 = None parameter to LocalConversation.__init__ and store it as self._cipher.
  • At the construction site in conversation_service.py (where the conversation is built), pass config.cipher (the same instance the routers use via get_cipher).
  • In LocalConversation.switch_profile, change self._profile_store.load(profile_name) to self._profile_store.load(profile_name, cipher=self._cipher).
  • In conversation_service.get_title_llm_from_profile, accept/lookup the cipher and pass cipher= to profile_store.load(...).

Fix 2 — share _decrypt_incoming_secrets and call it from switch_llm

  • Lift the helper out of profiles_router.py into a shared spot. Reasonable home: alongside LLM_SECRET_FIELDS in openhands-sdk/openhands/sdk/llm/llm.py (so both server and any non-router caller can use it), or agent_server/_secrets_exposure.py if we want it server-only.
  • Update switch_conversation_llm in conversation_router.py:326 to obtain cipher = get_cipher(request) (need to add request: Request to the signature) and apply the helper before conversation.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_KEY configured (encrypted on disk), then POST /api/conversations/{id}/switch_profile with that profile name; assert the conversation's agent.llm.api_key.get_secret_value() equals the original plaintext, not the Fernet token.
  • GET /api/profiles/X with X-Expose-Secrets: encrypted, forward the body into POST /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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions