Repository navigation
feat(profiles): Add cipher encryption for LLM profile secrets - #3161
Conversation
Adds at-rest encryption and header-based secret exposure for the profiles API,
achieving parity with the settings flow:
- At-rest encryption: When OH_SECRET_KEY is configured, secrets are encrypted
using the cipher before persisting to disk
- X-Expose-Secrets header: GET /api/profiles/{name} now supports:
- 'encrypted': Return cipher-encrypted values (safe for FE round-trip)
- 'plaintext': Return raw secret values (backend clients only)
- (absent): Return nulled api_key with api_key_set indicator (default)
- Encrypted round-trip: Clients can GET encrypted values and re-submit them,
enabling read-modify-write workflows without exposing secrets
Implementation details:
- LLMProfileStore.save() accepts optional cipher for at-rest encryption
- LLMProfileStore.load() accepts optional cipher for decryption
- LLM.load_from_json() accepts context for Pydantic validation with cipher
- profiles_router gets cipher from app config and passes to store operations
- Backward compatible: Without cipher, stores plaintext (existing behavior)
Closes #3154
Co-authored-by: openhands <openhands@all-hands.dev>
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
Coverage Report •
|
||||||||||||||||||||||||||||||||||||||||
all-hands-bot
left a comment
There was a problem hiding this comment.
🟢 Good taste - Clean implementation with solid deduplication, proper backward compatibility, and comprehensive test coverage. Approved!
all-hands-bot
left a comment
There was a problem hiding this comment.
🟢 Good taste - Clean deduplication, solid backward compatibility, and well-structured cipher encryption flow. The Fernet prefix check elegantly prevents double-encryption, and the typed error handling is much better than substring matching.
[RISK ASSESSMENT]
- [Overall PR]
⚠️ Risk Assessment: 🟢 LOW
Additive security feature with no breaking changes. Backward compatible (plaintext storage when cipher not configured), comprehensive test coverage, and defensive implementation with proper error handling.
malhotra5
left a comment
There was a problem hiding this comment.
LGTM! One question
When the user switches profiles mid conversation, does the client have an endpoint where they pass in the profile information? If so does that endpoint know how to decrypt the secret values?
all-hands-bot
left a comment
There was a problem hiding this comment.
✅ QA Report: PASS
All profile encryption behaviors verified end-to-end through API requests. The PR successfully brings LLM profiles to parity with settings on secret handling.
Does this PR achieve its stated goal?
Yes. The PR set out to bring the LLM profiles API to parity with the settings flow on secret handling, and it delivers on all three promised behaviors: (1) at-rest encryption when OH_SECRET_KEY is configured, (2) X-Expose-Secrets header support with encrypted/plaintext/absent modes, and (3) encrypted round-trip that preserves the original plaintext through GET encrypted → POST → GET plaintext cycles. I verified each behavior by making real HTTP requests to the profiles endpoints and inspecting both the API responses and the on-disk storage format.
| Phase | Result |
|---|---|
| Environment Setup | ✅ make build completed successfully, all dependencies installed |
| CI Status | ✅ 57/57 profile router tests passing, 27/27 settings router tests passing |
| Functional Verification | ✅ All three encryption behaviors verified end-to-end with real API requests |
Functional Verification
Test 1: At-rest encryption with OH_SECRET_KEY configured
Step 1 — Create test client with cipher:
Set up FastAPI TestClient with OH_SECRET_KEY configured and a temporary profile directory.
Step 2 — POST a profile with plaintext secret:
POST /api/profiles/test-profile
{
"llm": {"model": "gpt-4o", "api_key": "sk-plaintext-secret"},
"include_secrets": true
}Response: 201 Created
Step 3 — Verify on-disk encryption:
Inspected /tmp/{profile-dir}/test-profile.json:
{
"api_key": "gAAAAABp_lMYuqJcZ1oo5pKZb1CPBU..."
}✓ Secret is Fernet-encrypted (starts with gAAAAA), not plaintext.
✓ Decrypts back to sk-plaintext-secret with the configured cipher.
This confirms at-rest encryption works when OH_SECRET_KEY is set.
Test 2: X-Expose-Secrets header behavior — absent
Step 1 — GET profile without X-Expose-Secrets header:
GET /api/profiles/test-profileResponse:
{
"config": {"api_key": null},
"api_key_set": true
}✓ Returns null for api_key with api_key_set indicator (existing behavior preserved).
Test 3: X-Expose-Secrets header behavior — plaintext
Step 1 — GET profile with X-Expose-Secrets: plaintext:
GET /api/profiles/test-profile
X-Expose-Secrets: plaintextResponse:
{
"config": {"api_key": "sk-plaintext-secret"}
}✓ Returns the raw secret value (decrypted server-side).
Test 4: X-Expose-Secrets header behavior — encrypted
Step 1 — GET profile with X-Expose-Secrets: encrypted:
GET /api/profiles/test-profile
X-Expose-Secrets: encryptedResponse:
{
"config": {"api_key": "gAAAAABp_lMYWfmvS_X0Py0a783U5O..."}
}✓ Returns a Fernet-encrypted value (safe for client round-trip).
✓ Decrypts back to sk-plaintext-secret with the cipher.
Test 5: Encrypted round-trip workflow
Step 1 — GET encrypted value:
GET /api/profiles/test-profile
X-Expose-Secrets: encryptedResponse: {"api_key": "gAAAAABp_lMYWfmvS_X0Py0a783U5O..."}
Step 2 — POST the encrypted blob back with a model change:
POST /api/profiles/test-profile
{
"llm": {"model": "gpt-4o-mini", "api_key": "gAAAAABp_lMYWfmvS_X0Py0a783U5O..."},
"include_secrets": true
}Response: 201 Created
Step 3 — GET plaintext to verify no double-encryption:
GET /api/profiles/test-profile
X-Expose-Secrets: plaintextResponse:
{
"config": {"api_key": "sk-plaintext-secret", "model": "gpt-4o-mini"}
}✓ Original plaintext preserved (not double-encrypted).
✓ Model updated successfully.
This confirms the encrypted round-trip workflow works correctly.
Test 6: Invalid header value returns 400
Step 1 — GET with invalid header:
GET /api/profiles/test-profile
X-Expose-Secrets: invalid-valueResponse: 400 Bad Request
{
"detail": "Invalid X-Expose-Secrets header value: 'invalid-value'. Valid values are: 'encrypted', 'plaintext'."
}✓ Clear error message guides the user.
Test 7: Legacy "true" treated as "encrypted"
Step 1 — GET with X-Expose-Secrets: true:
GET /api/profiles/test-profile
X-Expose-Secrets: trueResponse:
{
"config": {"api_key": "gAAAAABp_lMY..."}
}✓ Returns encrypted value (not plaintext), confirming the legacy alias maps to encrypted for safety.
Test 8: Backward compatibility without cipher
Step 1 — Create client without OH_SECRET_KEY:
Set up FastAPI TestClient with secret_key=None.
Step 2 — POST a profile with plaintext secret:
POST /api/profiles/no-cipher-profile
{
"llm": {"model": "gpt-4o", "api_key": "sk-no-cipher-secret"},
"include_secrets": true
}Response: 201 Created
Step 3 — Verify plaintext storage:
Inspected /tmp/{profile-dir}/no-cipher-profile.json:
{
"api_key": "sk-no-cipher-secret"
}✓ Secret stored as plaintext (backward compatible with pre-encryption behavior).
Step 4 — Request encryption without cipher:
GET /api/profiles/no-cipher-profile
X-Expose-Secrets: encryptedResponse: 503 Service Unavailable
{
"detail": "Internal Server Error",
"exception": "503: Encryption not available: OH_SECRET_KEY is not configured. Cannot return encrypted secrets."
}✓ Returns 503 with a clear message about missing OH_SECRET_KEY.
Test 9: Unit test coverage
Ran the full test suite for profiles and settings routers:
uv run pytest tests/agent_server/test_profiles_router.pyResult: 57/57 tests passing, including 9 new tests covering:
- Invalid header → 400
- Plaintext/encrypted header exposure
- Encrypted round-trip
- Cipher encryption at rest
- Missing cipher + encrypted header → 503
- Backward compatibility without cipher
uv run pytest tests/agent_server/test_settings_router.pyResult: 27/27 tests passing, confirming the shared _secrets_exposure module integrates correctly with both routers.
Issues Found
None.
Good point. :-) #3164 |
…nds#3161) Co-authored-by: openhands <openhands@all-hands.dev>
…nds#3161) Co-authored-by: openhands <openhands@all-hands.dev>
Summary
Brings the LLM profiles API to parity with the settings flow on secret handling (closes #3154), and along the way deduplicates the X-Expose-Secrets plumbing so both routers share one implementation.
Profile secret handling — three behaviors, matching settings:
disk. With no key set, profiles continue to store plaintext (backward-compatible with PR feat(profiles): LLM profile management API #3129).
absent/default returns the existing nulled-api_key shape.
re-encryption at rest, so a GET encrypted → POST cycle preserves the original plaintext instead of double-encrypting it. Plaintext input passes through untouched (Fernet prefix is
the gate).
Shared _secrets_exposure module for settings_router and profiles_router:
substring-match on "no cipher configured" in both routers with a typed check, so changes to the error message no longer silently degrade to a 500.
Other touch-ups:
Test plan
encrypted round-trip preserves plaintext through GET encrypted → POST → GET plaintext, plaintext POST with cipher configured encrypts at rest, plaintext POST without cipher stores
plaintext (back-compat).
Closes #3154
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.13-nodejs22-slimgolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:3eed4a2-pythonRun
All tags pushed for this build
About Multi-Architecture Support
3eed4a2-python) is a multi-arch manifest supporting both amd64 and arm643eed4a2-python-amd64) are also available if needed