Skip to content

Agent Server: whitespace-only session API keys still count as real keys #5731

Description

@enyst

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 tests not key;
  • becomes the cipher key through the fallback when OH_SECRET_KEY is 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 bind 0.0.0.0.
  • POST /api/init with session_api_keys: [" "] → accepted.
  • OH_SECRET_KEY unset or blank with OH_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_keys drops keys that are empty after .strip(); non-blank keys are kept verbatim.
  • The /api/init guard in InitService.initialize rejects 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 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_N env slots, from the JSON config file, and from the legacy V0 SESSION_API_KEY default factory, so a whitespace-only value in any of those paths leaves session_api_keys empty.
  • 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/init with session_api_keys containing a whitespace-only entry returns HTTP 422, leaves the server in the dormant state, and does not echo the submitted key in the response.
  • _default_secret_key() skips whitespace-only session keys, so when OH_SECRET_KEY is unset or blank the cipher fallback never uses one (falls through to None or the next usable key), and a non-blank session key is still used verbatim.
  • Tests cover each path above: Config.session_api_keys normalization (env slot, JSON config, direct construction, and V0 default), the /api/init 422 guard, and the _default_secret_key() fallback, including the non-blank-with-surrounding-whitespace case.

Activity

  1. added
    bug
    on Oct 13, 2026
  2. added theissue type on Oct 13, 2026
  3. added
    ready-for-dev
    on Oct 13, 2026
  4. added a commit that references this issue on Oct 18, 2026
    3088207
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugready-for-dev

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions