Skip to content

fix(agent-server): treat an empty OH_SECRET_KEY as unset - #5660

Merged
enyst merged 9 commits into
mainfrom
claude/harden-empty-secret-key
Oct 13, 2026
Merged

enyst merged 9 commits into
mainfrom
claude/harden-empty-secret-key

Conversation

@enyst

@enyst enyst commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

HUMAN:
This PR fixes a bug found during a full agentic run of agent-server.


AGENT:

I'm an AI agent based on Opus 5.5, helping Engel Nyst (@enyst) with project maintenance.

Why

A blank OH_SECRET_KEY (empty or whitespace only, for example a template variable rendered as OH_SECRET_KEY=) is accepted as a real encryption key: Config.cipher only checks is None, so it builds Cipher("") (or a cipher from the whitespace) and persisted settings and secrets are encrypted under a key anyone can derive. The "OH_SECRET_KEY was not defined" warning never fires, so nothing points the operator at the misconfiguration, and the empty value also takes precedence over the session-key fallback. _default_secret_key already treats an empty session key as absent; this applies the same rule to the secret key. It was found while building the Agent Server feature map in #5621 (F18.empty-secret-key).

Summary

  • A field_validator on Config.secret_key treats a blank value (empty or whitespace only) as unset by returning _default_secret_key(), the same fallback used when the variable is absent. The first session key is used when one is set; otherwise there is no cipher and the existing warning is logged. This covers the env var, the JSON config file and direct Config(...) construction.
  • /api/init applies secret_key with model_copy, which skips validators, so _build_initialized_config now ignores a blank secret_key too and its existing session-key fallback applies.
  • On a deferred-init server, OH_SECRET_KEY is also the credential for POST /api/init, and check_init_api_key leaves the endpoint open when no key is configured. So that a blank value does not turn a closed endpoint into an open one, Config remembers that a blank key was configured (a private attribute, not serialized or in the schema), and /api/init answers 401 when that is the case and no session key fills in. A truly unset key keeps today's behaviour, and a session-key fallback is still the init credential.
  • Rejecting empty session_api_keys during /api/init landed on main with fix(agent-server): ignore empty session API keys #5659; after merging main this PR keeps a single copy of that guard and adds no second one.
  • Cipher and the other consumers are unchanged.

Issue Number

Related to #5688.

How to Test

uv run pytest tests/agent_server/test_config.py tests/agent_server/test_init_router.py

Tests cover a blank OH_SECRET_KEY ("", " ", "\n") with no session keys or with a session key, a blank secret_key in the JSON config file and in direct Config(...), a blank secret_key in the init body (unit and end to end), and /api/init staying closed (401, still dormant) on a deferred-init server whose OH_SECRET_KEY is blank. A non-blank key with surrounding whitespace (" mykey ") is checked to be kept verbatim, from the env var and from the init body. The new cases fail on main and pass with this change; pre-commit passes on all changed files.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes


Generated by Claude Code


🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)

• 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-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime 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:f736419-python

Run

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

All tags pushed for this build

ghcr.io/openhands/agent-server:f736419-golang-amd64
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-golang-amd64
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-golang-amd64
ghcr.io/openhands/agent-server:f736419-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:f736419-golang-arm64
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-golang-arm64
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-golang-arm64
ghcr.io/openhands/agent-server:f736419-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:f736419-java-amd64
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-java-amd64
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-java-amd64
ghcr.io/openhands/agent-server:f736419-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:f736419-java-arm64
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-java-arm64
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-java-arm64
ghcr.io/openhands/agent-server:f736419-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:f736419-python-amd64
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-python-amd64
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-python-amd64
ghcr.io/openhands/agent-server:f736419-python-node-runtime-amd64
ghcr.io/openhands/agent-server:f736419-python-arm64
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-python-arm64
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-python-arm64
ghcr.io/openhands/agent-server:f736419-python-node-runtime-arm64
ghcr.io/openhands/agent-server:f736419-python-minimal-amd64
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-python-minimal-amd64
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-python-minimal-amd64
ghcr.io/openhands/agent-server:f736419-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:f736419-python-minimal-arm64
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-python-minimal-arm64
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-python-minimal-arm64
ghcr.io/openhands/agent-server:f736419-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:f736419-python-slim-amd64
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-python-slim-amd64
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-python-slim-amd64
ghcr.io/openhands/agent-server:f736419-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:f736419-python-slim-arm64
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-python-slim-arm64
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-python-slim-arm64
ghcr.io/openhands/agent-server:f736419-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:f736419-golang
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-golang
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-golang
ghcr.io/openhands/agent-server:f736419-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:f736419-java
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-java
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-java
ghcr.io/openhands/agent-server:f736419-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:f736419-python-minimal
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-python-minimal
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-python-minimal
ghcr.io/openhands/agent-server:f736419-python-node-runtime-minimal
ghcr.io/openhands/agent-server:f736419-python-slim
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-python-slim
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-python-slim
ghcr.io/openhands/agent-server:f736419-python-node-runtime-slim
ghcr.io/openhands/agent-server:f736419-python
ghcr.io/openhands/agent-server:f736419141f7a76b084041499aadee77209edf85-python
ghcr.io/openhands/agent-server:claude-harden-empty-secret-key-python
ghcr.io/openhands/agent-server:f736419-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., f736419-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., f736419-python-amd64) are also available if needed

enyst and others added 2 commits October 9, 2026 06:49
An empty OH_SECRET_KEY parsed to SecretStr(""), so Config.cipher built a
Cipher from the empty string: persisted secrets were encrypted under a
trivially derivable key, the "OH_SECRET_KEY was not defined" warning
never fired, and the session-API-key fallback was skipped.

Normalize an empty secret_key through the same _default_secret_key()
fallback used when the variable is absent, so the existing session-key
fallback and "not defined" warning paths apply unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W9qxyUBKhzmbLrTK2wi7D7
/api/init applies secret_key with model_copy, which skips the Config
validator, so an empty value still produced an empty-key cipher there.
Skip it so the existing session-key fallback applies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W9qxyUBKhzmbLrTK2wi7D7
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   config.py162199%560
   init_router.py159597%190, 192, 196, 198, 200
TOTAL496542019259% 

Co-authored-by: openhands <openhands@all-hands.dev>
Resolve tests/agent_server/test_init_router.py: keep the empty init
secret tests alongside the deferred-init gating tests added on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W9qxyUBKhzmbLrTK2wi7D7
@enyst
enyst requested a review from all-hands-bot October 11, 2026 19:25
@enyst
enyst marked this pull request as ready for review October 11, 2026 19:25
@enyst
enyst requested review from all-hands-bot and removed request for all-hands-bot October 11, 2026 19:25

@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.

This review was posted by an AI agent (OpenHands).

Review summary

Scope check: this change belongs in this repository. openhands-agent-server/ owns the agent-server Config and the /api/init lifecycle, and the diff matches the #5660 scope named in the umbrella issue #5688 (Config.secret_key validator plus /api/init _build_initialized_config). No cross-repo move or product/architecture decision is needed.

Verified on head 04cca9fc0a4a0a63afa142e5e8c2ead46aa2245b:

  • All check-runs for this head are green, including coverage-report, build, cross-tests, build-binary-and-test, and unresolved-review-threads.
  • uv run pytest tests/agent_server/test_config.py tests/agent_server/test_init_router.py -> 45 passed.
  • Behavior confirmed by direct execution: OH_SECRET_KEY= (empty) now yields secret_key is None, cipher is None, and logs the "OH_SECRET_KEY was not defined" warning; an empty value combined with OH_SESSION_API_KEYS_0 falls back to the session key; _build_initialized_config maps an empty secret_key to the first session key. The empty-string bug is genuinely fixed on both real entry points.

One material gap and one non-blocking coordination note are below.

1. Whitespace-only OH_SECRET_KEY still builds Cipher(" ") (material)

The PR notes say whitespace-only values are intentionally "still treated as set, matching _default_secret_key". That premise does not hold for the env/JSON path: load_config() merges OH_SECRET_KEY into Config.model_validate(data), so _default_secret_key() is never consulted there and the new validator is the only guard. if not value.get_secret_value() catches only ""; a whitespace-only value passes through and Config.cipher builds Cipher(" ") -- the same weak-key outcome this PR is fixing. I confirmed this in the workspace: OH_SECRET_KEY=" " and JSON {"secret_key": " "} both produce a live Cipher object (an encrypt/decrypt round-trip succeeds), while the empty value correctly yields None. The umbrella issue #5688 lists this in scope: "Config.secret_key treats a blank value ("" or whitespace-only) as unset for env, JSON config file, and direct Config(...)". Suggest if value is not None and not value.get_secret_value().strip():.

2. Duplicated empty-key guard with #5659 (non-blocking, coordination)

The new 422 in InitService.initialize overlaps the empty-session_api_keys normalization that #5659 adds to Config. The umbrella issue calls this out explicitly ("rebase the other to avoid keeping the same guard twice"), and the PR description already flags it. This is a reminder to rebase whichever lands second, not a defect in this head.

Verdict: the empty-string case is fixed and tested, but a whitespace-only value is still accepted as an encryption key, so the security bug being closed is not fully addressed.

🔄 CHANGES REQUESTED

enyst and others added 2 commits October 12, 2026 19:29
#5659 landed on main. Resolve config.py by keeping both validators
(session_api_keys and secret_key), and keep both sets of tests in
test_config.py and test_init_router.py. The identical empty init key
guard from both branches merges to a single copy in init_router.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W9qxyUBKhzmbLrTK2wi7D7
A secret key that is only whitespace (for example a template that
rendered a stray space or newline) still built a Cipher from it, the
same guessable-key case as an empty value. Strip before the emptiness
check in the Config.secret_key validator and in the /api/init path, and
cover the env var, the JSON config file, direct construction and init.
Reported in the all-hands-bot review of #5660.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W9qxyUBKhzmbLrTK2wi7D7
@enyst
enyst requested a review from all-hands-bot October 12, 2026 19:31

@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.

This review was posted by an AI agent (OpenHands).

Review summary

Scope check: this change belongs in this repository. openhands-agent-server/ owns the agent-server Config and the /api/init lifecycle, and the diff matches the #5660 scope named in umbrella issue #5688. No repository move or product/architecture decision is needed.

Re-reviewed on the new head 253572c0505774838e01c9a6b38e3dde378ae446 (the earlier review's whitespace-only finding was on 04cca9fc and is now addressed).

What I verified

  • The whitespace gap is closed. Both guards now .strip() before the emptiness test: Config._empty_secret_key_is_unset (config.py:495-503) and _build_initialized_config (init_router.py:178-179). I confirmed by execution that OH_SECRET_KEY=" ", OH_SECRET_KEY="\n", JSON {"secret_key": " "}, and Config(secret_key=" ") all now yield secret_key is None + cipher is None + the "OH_SECRET_KEY was not defined" warning, whereas "" was already handled. A blank value combined with a session key still falls back to the first session key.
  • Non-blank behavior is unchanged. A non-blank secret_key is returned as-is by the validator; the init path only substitutes the session-key fallback when req.secret_key is blank and base.secret_key is None.
  • No second guard / no duplication. #5659 has landed (main carries _drop_empty_session_api_keys and the single InitService.initialize 422 guard); this branch keeps exactly one copy of each, per the issue's coordination note.
  • Tests. uv run pytest tests/agent_server/test_config.py tests/agent_server/test_init_router.py -> 54 passed. The added cases cover env ("", " ", "\n"), JSON config file, direct Config(...), and the init body unit + end-to-end paths.
  • CI. Completed checks for this head are green, including coverage-report, build, test (24.x), security, public-type-budget, agent-server-api, Check OpenAPI Schema, windows-tests, and build-binary-and-test (ubuntu/macos). Only Docker image build/push and the Windows binary job remained in progress when read; no failures.

The empty-string security bug and its whitespace-only extension are both fixed on the current head, with appropriate regression coverage. No material findings.

✅ APPROVED

Pyright flagged the constructor test for calling get_secret_value() on
an Optional[SecretStr].

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W9qxyUBKhzmbLrTK2wi7D7
@enyst enyst added the release-note-required PR requires explicit release-note coverage for behavioral or default changes label Oct 13, 2026
enyst and others added 2 commits October 13, 2026 04:50
A blank OH_SECRET_KEY used to leave /api/init closed by accident: no
client can present an empty key. Treating it as unset made the endpoint
open on deferred-init servers without a session key. Remember on Config
that a blank key was configured, and reject init requests when no usable
key remains. A truly unset key keeps the open dev behaviour.

Also pin that a non-blank key with surrounding whitespace is kept
verbatim, from the env var and from the init body.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W9qxyUBKhzmbLrTK2wi7D7
test_init_rejects_empty_session_keys already covers the 422 for empty
session keys. Keep only what this PR adds: a blank init secret_key falls
back to the session key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W9qxyUBKhzmbLrTK2wi7D7
@enyst
enyst requested a review from all-hands-bot October 13, 2026 04:51

@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.

This review was posted by an AI agent (OpenHands).

Review summary

Scope check: this change belongs in this repository. openhands-agent-server/ owns the agent-server Config and the /api/init lifecycle, and the diff matches the #5660 scope in umbrella issue #5688 ("treat a blank OH_SECRET_KEY as missing and fail closed if no usable key remains"). No repository move or product/architecture decision is needed.

Re-reviewed on the new head f736419141f7a76b084041499aadee77209edf85 (my prior approval was on 253572c0; the head moved with the /api/init fail-closed change plus test cleanup, so a fresh review is required).

What I verified

  • Blank-key handling still correct. Config._empty_secret_key_is_unset (config.py:513) uses _is_blank_secret, so "", " ", "\n" from the env var, the JSON config file, and direct construction all resolve to None and fall back to _default_secret_key(). A non-blank value with surrounding whitespace (" mykey ") is kept verbatim (not stripped), matching the new tests and the release note.
  • Fail-closed init is sound. The new _secret_key_was_blank PrivateAttr (config.py:502) is set by the model_validator(mode="wrap") (config.py:523-528) and surfaced via the secret_key_was_blank property. check_init_api_key (init_router.py:357) returns 401 only when the config has no usable secret_key and a blank key was configured, leaving the truly-unset case (dev) open and the session-key fallback case unaffected. I confirmed the flag is set on every construction path I tried -- Config(...), Config.model_validate(dict), Config.model_validate_json(...), and kwargs -- and that it survives model_copy, which matters because _build_initialized_config uses model_copy (validators skipped).
  • Private attr is not a compatibility surface. It is a PrivateAttr, so it is absent from the schema and from model_dump; no REST/OpenAPI or persisted-settings contract changes. The OpenAPI and REST breakage checks are green.
  • Tests. uv run pytest tests/agent_server/test_config.py tests/agent_server/test_init_router.py -> 57 passed. New coverage includes test_blank_secret_key_env_keeps_init_closed (401 + still dormant, parametrized over "" and " ") and the verbatim-whitespace cases; ruff check on all four changed files is clean.
  • CI. All 30 completed check runs for this head are green, including build, cross-tests, agent-server-stress-tests, windows-tests, build-binary-and-test (ubuntu/macos/windows), Check OpenAPI Schema, REST API (OpenAPI), and unresolved-review-threads.

The earlier whitespace-only gap is fixed, the newly added fail-closed behavior is correctly scoped and covered, and the prior review thread is resolved. No material findings. The release-note-required label is addressed by the release note in the PR description.

✅ APPROVED

@enyst
enyst merged commit 7bd668c into main Oct 13, 2026
56 checks passed
@enyst
enyst deleted the claude/harden-empty-secret-key branch October 13, 2026 05:50
@simonrosenberg simonrosenberg mentioned this pull request Oct 15, 2026
1 of 7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note-required PR requires explicit release-note coverage for behavioral or default changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants