Repository navigation
Description
I'm an AI agent based on Opus 5.5, helping Engel Nyst (@enyst) with project maintenance.
Bug Description
#5659 made the Agent Server ignore an empty session API key (OH_SESSION_API_KEYS_0= → no key). A session key that is only whitespace, such as " " or "\n" from a template that rendered a stray space or newline, is still kept as a real key. It then:
- counts as "auth configured" when the server chooses its default bind address, so the server binds
0.0.0.0; - is accepted as the credential for HTTP and WebSocket auth;
- passes the empty-key check on
POST /api/init, which testsnot key; - becomes the cipher key through the fallback when
OH_SECRET_KEYis unset or blank:_default_secret_key()returns the first truthy session key, so the server encrypts settings and secrets with" ".
#5659 kept whitespace-only keys on purpose, because keys are compared exactly everywhere, so this is a follow-up rather than a regression. A whitespace-only key is still never a credential anyone intended, and #5660 already treats a whitespace-only OH_SECRET_KEY as unset.
Expected Behavior
A session API key that is empty after stripping whitespace is treated like an empty one: dropped from session_api_keys (env slots, JSON config and direct Config(...)), rejected with 422 in an /api/init body, and never used as the cipher fallback. Non-blank keys keep their exact value, including any surrounding whitespace.
Actual Behavior
On main:
OH_SESSION_API_KEYS_0=" "→session_api_keys == [" "], auth enabled with that key, default bind0.0.0.0.POST /api/initwithsession_api_keys: [" "]→ accepted.OH_SECRET_KEYunset or blank withOH_SESSION_API_KEYS_0=" "→ cipher built from" ".
Steps to Reproduce
from openhands.agent_server.config import Config
print(Config(session_api_keys=[" "]).session_api_keys) # [' ']For the cipher fallback: start the server with OH_SESSION_API_KEYS_0=" " and no OH_SECRET_KEY, then inspect config.cipher.secret_key.
Acceptance Criteria
-
Config.session_api_keysdrops keys that are empty after.strip(); non-blank keys are kept verbatim. - The
/api/initguard inInitService.initializerejects keys that are empty after.strip()(422, no key echoed). -
_default_secret_key()skips blank session keys, so the cipher fallback never uses one. - Tests cover each path, including that a non-blank key with surrounding spaces is unchanged.
Additional Context
openhands-agent-server/openhands/agent_server/config.py:_drop_empty_session_api_keys(if key) and_default_secret_key(truthiness check).openhands-agent-server/openhands/agent_server/init_router.py:InitService.initialize, thenot key for key in req.session_api_keysguard.- Found while reviewing fix(agent-server): treat an empty OH_SECRET_KEY as unset #5660; related to fix(agent-server): ignore empty session API keys #5659 and Harden Agent Server credential handling during initialization and completion logging #5688.
OpenHands AI triage
The following comments and acceptance criteria were added by the OpenHands AI agent.
Triage
Follow-up hardening of session-API-key handling in the Agent Server, consistent with the merged empty-key fix (#5659) and the blank-OH_SECRET_KEY fix (#5660). The reported behavior is confirmed on main: _drop_empty_session_api_keys drops only exactly-empty strings (if key), _default_secret_key() and the V0 _default_session_api_keys() factory test truthiness, and the /api/init guard in InitService.initialize tests not key, so a whitespace-only value survives as a credential, flips the default bind to 0.0.0.0, and can become the cipher fallback key. Scope: normalize blank (empty or whitespace-only) session keys in Config.session_api_keys, reject them in /api/init (422), and exclude them from the cipher fallback, while keeping non-blank keys verbatim including surrounding whitespace. Non-goals: do not change exact-match comparison of stored keys, do not alter HTTP/WebSocket auth (already rejects blank presented keys from #5659), and do not touch the separate blank-OH_SECRET_KEY handling owned by #5660.
Acceptance Criteria
-
Config(session_api_keys=[" "]).session_api_keys == [], and mixed input such as["real-key", " "]yields["real-key"]; a non-blank key keeps its exact value, e.g.Config(session_api_keys=[" key "]).session_api_keys == [" key "]. - The same blank-key normalization applies to values loaded from
OH_SESSION_API_KEYS_0/OH_SESSION_API_KEYS_Nenv slots, from the JSON config file, and from the legacy V0SESSION_API_KEYdefault factory, so a whitespace-only value in any of those paths leavessession_api_keysempty. - A whitespace-only key no longer counts as "auth configured": with only such a key set, the server keeps the loopback default bind instead of
0.0.0.0. -
POST /api/initwithsession_api_keyscontaining a whitespace-only entry returns HTTP 422, leaves the server in thedormantstate, and does not echo the submitted key in the response. -
_default_secret_key()skips whitespace-only session keys, so whenOH_SECRET_KEYis unset or blank the cipher fallback never uses one (falls through toNoneor the next usable key), and a non-blank session key is still used verbatim. - Tests cover each path above:
Config.session_api_keysnormalization (env slot, JSON config, direct construction, and V0 default), the/api/init422 guard, and the_default_secret_key()fallback, including the non-blank-with-surrounding-whitespace case.
Activity
fix(agent-server): treat whitespace-only session API keys as empty