Repository navigation
fix(agent-server): treat an empty OH_SECRET_KEY as unset - #5660
Conversation
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
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
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
all-hands-bot
left a comment
There was a problem hiding this comment.
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, andunresolved-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 yieldssecret_key is None,cipher is None, and logs the "OH_SECRET_KEY was not defined" warning; an empty value combined withOH_SESSION_API_KEYS_0falls back to the session key;_build_initialized_configmaps an emptysecret_keyto 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
#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
all-hands-bot
left a comment
There was a problem hiding this comment.
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 thatOH_SECRET_KEY=" ",OH_SECRET_KEY="\n", JSON{"secret_key": " "}, andConfig(secret_key=" ")all now yieldsecret_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_keyis returned as-is by the validator; the init path only substitutes the session-key fallback whenreq.secret_keyis blank andbase.secret_key is None. - No second guard / no duplication. #5659 has landed (
maincarries_drop_empty_session_api_keysand the singleInitService.initialize422 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, directConfig(...), 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, andbuild-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
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
all-hands-bot
left a comment
There was a problem hiding this comment.
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 toNoneand 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_blankPrivateAttr(config.py:502) is set by themodel_validator(mode="wrap")(config.py:523-528) and surfaced via thesecret_key_was_blankproperty.check_init_api_key(init_router.py:357) returns 401 only when the config has no usablesecret_keyand 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 survivesmodel_copy, which matters because_build_initialized_configusesmodel_copy(validators skipped). - Private attr is not a compatibility surface. It is a
PrivateAttr, so it is absent from the schema and frommodel_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 includestest_blank_secret_key_env_keeps_init_closed(401 + still dormant, parametrized over""and" ") and the verbatim-whitespace cases;ruff checkon 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), andunresolved-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
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 asOH_SECRET_KEY=) is accepted as a real encryption key:Config.cipheronly checksis None, so it buildsCipher("")(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_keyalready 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
field_validatoronConfig.secret_keytreats 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 directConfig(...)construction./api/initappliessecret_keywithmodel_copy, which skips validators, so_build_initialized_confignow ignores a blanksecret_keytoo and its existing session-key fallback applies.OH_SECRET_KEYis also the credential forPOST /api/init, andcheck_init_api_keyleaves the endpoint open when no key is configured. So that a blank value does not turn a closed endpoint into an open one,Configremembers that a blank key was configured (a private attribute, not serialized or in the schema), and/api/initanswers 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.session_api_keysduring/api/initlanded onmainwith fix(agent-server): ignore empty session API keys #5659; after mergingmainthis PR keeps a single copy of that guard and adds no second one.Cipherand the other consumers are unchanged.Issue Number
Related to #5688.
How to Test
Tests cover a blank
OH_SECRET_KEY(""," ","\n") with no session keys or with a session key, a blanksecret_keyin the JSON config file and in directConfig(...), a blanksecret_keyin the init body (unit and end to end), and/api/initstaying closed (401, still dormant) on a deferred-init server whoseOH_SECRET_KEYis 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 onmainand pass with this change;pre-commitpasses on all changed files.Type
Notes
mainis merged into this branch, keeping bothConfigvalidators and one copy of the init guard.release-note-required): a blankOH_SECRET_KEYnow counts as unset. Secrets stored earlier under an empty key can no longer be decrypted, and a deferred-init server with a blank key and no session key keeps/api/initclosed until a usable key is configured.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
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimepython-node-runtimegolang: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:f736419-pythonRun
All tags pushed for this build
About Multi-Architecture Support
f736419-python) is a multi-arch manifest supporting both amd64 and arm64f736419-python-amd64) are also available if needed