Skip to content

feat(profiles): Add cipher encryption for LLM profile secrets - #3161

Merged
VascoSch92 merged 3 commits into
mainfrom
feat/profiles-cipher-encryption
May 8, 2026
Merged

VascoSch92 merged 3 commits into
mainfrom
feat/profiles-cipher-encryption

Conversation

@VascoSch92

@VascoSch92 VascoSch92 commented May 8, 2026 •

Copy link
Copy Markdown
Member

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:

  • At-rest encryption. When OH_SECRET_KEY is configured, LLMProfileStore.save encrypts secret-bearing fields (api_key, AWS keys) via serialize_secret(cipher=…) before writing to
    disk. With no key set, profiles continue to store plaintext (backward-compatible with PR feat(profiles): LLM profile management API #3129).
  • X-Expose-Secrets header on GET /api/profiles/{name}. encrypted returns Fernet-encrypted values for safe FE round-trip; plaintext returns raw values for backend clients;
    absent/default returns the existing nulled-api_key shape.
  • Encrypted round-trip on POST /api/profiles/{name}. The save endpoint detects incoming Fernet-prefixed (gAAAAA…) blobs and decrypts them with the configured cipher before
    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:

  • parse_expose_secrets_header — single parser, single 400 error message, single legacy "true" → "encrypted" alias.
  • build_expose_context — one place that builds the pydantic serialization context.
  • translate_missing_cipher — context manager that catches PydanticSerializationError and walks the cause chain for the new MissingCipherError(ValueError). Replaces the
    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.
  • get_config / get_cipher — one access pattern for both routers; profiles previously had its own defensive variant.

Other touch-ups:

  • New LLM_SECRET_FIELDS constant in openhands.sdk.llm.llm so the field-list used by the serializer and the new decrypt helper stays in sync.
  • LLM.load_from_json now accepts a context= kwarg, used by the store to thread the cipher into validate_secret on load.

Test plan

  • uv run pytest tests/agent_server/test_profiles_router.py tests/agent_server/test_settings_router.py tests/sdk/utils/test_pydantic_secrets.py — 121/121 passing locally.
  • uv run pytest tests/sdk/llm — 730/730 passing locally (covers LLM_SECRET_FIELDS extraction).
  • uv run pre-commit run --files — clean (ruff, pyright, dependency rules).
  • New tests cover the three behaviors directly: invalid header → 400, plaintext header → raw secret, encrypted header → cipher-encrypted, missing cipher + encrypted header → 503,
    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).
  • Manual: OH_SECRET_KEY=… server, POST an encrypted blob obtained from GET encrypted, GET plaintext returns the original value.

Closes #3154


Agent Server images for this PR

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python amd64, arm64 nikolaik/python-nodejs:python3.13-nodejs22-slim Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:3eed4a2-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-3eed4a2-python \
  ghcr.io/openhands/agent-server:3eed4a2-python

All tags pushed for this build

ghcr.io/openhands/agent-server:3eed4a2-golang-amd64
ghcr.io/openhands/agent-server:3eed4a25fe3ed1b2620b97adccbbfe968baa4fe5-golang-amd64
ghcr.io/openhands/agent-server:feat-profiles-cipher-encryption-golang-amd64
ghcr.io/openhands/agent-server:3eed4a2-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:3eed4a2-golang-arm64
ghcr.io/openhands/agent-server:3eed4a25fe3ed1b2620b97adccbbfe968baa4fe5-golang-arm64
ghcr.io/openhands/agent-server:feat-profiles-cipher-encryption-golang-arm64
ghcr.io/openhands/agent-server:3eed4a2-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:3eed4a2-java-amd64
ghcr.io/openhands/agent-server:3eed4a25fe3ed1b2620b97adccbbfe968baa4fe5-java-amd64
ghcr.io/openhands/agent-server:feat-profiles-cipher-encryption-java-amd64
ghcr.io/openhands/agent-server:3eed4a2-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:3eed4a2-java-arm64
ghcr.io/openhands/agent-server:3eed4a25fe3ed1b2620b97adccbbfe968baa4fe5-java-arm64
ghcr.io/openhands/agent-server:feat-profiles-cipher-encryption-java-arm64
ghcr.io/openhands/agent-server:3eed4a2-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:3eed4a2-python-amd64
ghcr.io/openhands/agent-server:3eed4a25fe3ed1b2620b97adccbbfe968baa4fe5-python-amd64
ghcr.io/openhands/agent-server:feat-profiles-cipher-encryption-python-amd64
ghcr.io/openhands/agent-server:3eed4a2-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:3eed4a2-python-arm64
ghcr.io/openhands/agent-server:3eed4a25fe3ed1b2620b97adccbbfe968baa4fe5-python-arm64
ghcr.io/openhands/agent-server:feat-profiles-cipher-encryption-python-arm64
ghcr.io/openhands/agent-server:3eed4a2-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:3eed4a2-golang
ghcr.io/openhands/agent-server:3eed4a25fe3ed1b2620b97adccbbfe968baa4fe5-golang
ghcr.io/openhands/agent-server:feat-profiles-cipher-encryption-golang
ghcr.io/openhands/agent-server:3eed4a2-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:3eed4a2-java
ghcr.io/openhands/agent-server:3eed4a25fe3ed1b2620b97adccbbfe968baa4fe5-java
ghcr.io/openhands/agent-server:feat-profiles-cipher-encryption-java
ghcr.io/openhands/agent-server:3eed4a2-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:3eed4a2-python
ghcr.io/openhands/agent-server:3eed4a25fe3ed1b2620b97adccbbfe968baa4fe5-python
ghcr.io/openhands/agent-server:feat-profiles-cipher-encryption-python
ghcr.io/openhands/agent-server:3eed4a2-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim

About Multi-Architecture Support

  • Each variant tag (e.g., 3eed4a2-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 3eed4a2-python-amd64) are also available if needed

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>
@github-actions

github-actions Bot commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   _secrets_exposure.py45393%21, 78, 95
   settings_router.py121893%234, 236–237, 335, 337–338, 376, 381
openhands-sdk/openhands/sdk/llm
   llm.py5418883%476, 500, 543, 800, 909, 911–912, 940, 986, 997–999, 1003, 1009–1012, 1014–1021, 1029–1031, 1041–1043, 1046–1047, 1051, 1054–1055, 1057–1058, 1060, 1299–1300, 1512–1513, 1522, 1547, 1549–1554, 1556–1573, 1576–1580, 1582–1583, 1589–1598, 1655, 1657
   llm_profile_store.py117496%69–70, 153–154
TOTAL26380617476% 

@VascoSch92
VascoSch92 requested a review from all-hands-bot May 8, 2026 21:10

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Good taste - Clean implementation with solid deduplication, proper backward compatibility, and comprehensive test coverage. Approved!

@VascoSch92
VascoSch92 marked this pull request as ready for review May 8, 2026 21:13
@VascoSch92
VascoSch92 requested a review from malhotra5 May 8, 2026 21:15

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 malhotra5 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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-profile

Response:

{
  "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: plaintext

Response:

{
  "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: encrypted

Response:

{
  "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: encrypted

Response: {"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: plaintext

Response:

{
  "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-value

Response: 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: true

Response:

{
  "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: encrypted

Response: 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.py

Result: 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.py

Result: 27/27 tests passing, confirming the shared _secrets_exposure module integrates correctly with both routers.

Issues Found

None.

@VascoSch92

Copy link
Copy Markdown
Member Author

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?

Good point. :-) #3164

@VascoSch92
VascoSch92 merged commit 3166003 into main May 8, 2026
40 checks passed
@VascoSch92
VascoSch92 deleted the feat/profiles-cipher-encryption branch May 8, 2026 21:34
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
pengzhang-create pushed a commit to Pyromind-Dynamics/software-agent-sdk that referenced this pull request Aug 13, 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.

feat(profiles): Add cipher encryption for LLM profile secrets

4 participants