[Bug]: Self-hosted LOCAL_BACKEND_API_KEY is not exported to OH_SESSION_API_KEYS_0 in entrypoint.sh, causing agent-server auth bypass
Operating System
Linux
Installation Method
Docker (ghcr.io/openhands/agent-canvas)
Agent Canvas Version
latest
Bug Description
In docker/entrypoint.sh (lines 198–212), OH_SESSION_API_KEYS_0 is exported inside the conditional block that checks whether no key was supplied:
if [ -z "${LOCAL_BACKEND_API_KEY:-}" ] && [ -z "${OH_SESSION_API_KEYS_0:-}" ]; then
...
export OH_SESSION_API_KEYS_0="$LOCAL_BACKEND_API_KEY"
fiWhen a self-hosting user configures a custom key via LOCAL_BACKEND_API_KEY=xxx, the if condition evaluates to false, causing the entire block (including export OH_SESSION_API_KEYS_0="$LOCAL_BACKEND_API_KEY") to be skipped.
Because openhands-agent-server does not read LOCAL_BACKEND_API_KEY directly (it reads OH_SESSION_API_KEYS_0 and SESSION_API_KEY), agent-server launches with config.session_api_keys = [].
In openhands.agent_server.dependencies, authentication checks are guarded by:
if config.session_api_keys and not key_matches:
raise HTTPException(...)When config.session_api_keys is empty, the authentication check passes unconditionally. As a result, the agent server is left unauthenticated, allowing anyone to execute operations without authentication.
Steps to Reproduce
- Start OpenHands in Docker with a custom
LOCAL_BACKEND_API_KEY: docker run -d -p 8000:8000 -e LOCAL_BACKEND_API_KEY="my-secret-key" ghcr.io/openhands/agent-canvas:latest - Check exported environment variables of openhands-agent-server.
- Send an unauthenticated request to /api/server_info without X-Session-API-Key.
- The request succeeds instead of returning 401 Unauthorized.
Actual Behavior
- OH_SESSION_API_KEYS_0 and SESSION_API_KEY are unset in agent-server environment.
- config.session_api_keys is empty.
- Backend endpoints bypass authentication completely.
Expected Behavior
- When LOCAL_BACKEND_API_KEY is specified, OH_SESSION_API_KEYS_0 and SESSION_API_KEY must be exported.
- agent-server must require the configured key and return 401 if missing.
Relevant Logs
Acceptance Criteria
- In docker/entrypoint.sh, OH_SESSION_API_KEYS_0 is exported outside the if block.
- SESSION_API_KEY is exported as fallback alias.
- Safe variable expansions ${VAR:-} are used under set -uo pipefail.
- Running entrypoint with LOCAL_BACKEND_API_KEY correctly exports keys.
Screenshots
No response
Additional Context
No response
OpenHands AI triage
The following comments and acceptance criteria were added by the OpenHands AI agent.
Triage
Verified against main. The reported defect is real and the mechanism is accurately described. docker/entrypoint.sh:200-211 only sets OH_SESSION_API_KEYS_0 inside the "no key supplied" branch, so a user-supplied LOCAL_BACKEND_API_KEY leaves OH_SESSION_API_KEYS_0 unset. The SDK's Config.session_api_keys (openhands-agent-server/openhands/agent_server/config.py) reads only SESSION_API_KEY and OH_SESSION_API_KEYS_0, not LOCAL_BACKEND_API_KEY, so it defaults to []; check_session_api_key (.../dependencies.py:36) then passes every request unconditionally, leaving all /api/* routes (bash, files, conversations, settings) unauthenticated. This also contradicts the documented contract in docs/DEVELOPMENT.md:111 ("LOCAL_BACKEND_API_KEY... is mapped internally to OH_SESSION_API_KEYS_0") and diverges from the npm launcher, which always maps the resolved key to OH_SESSION_API_KEYS_0 (scripts/dev-safe.mjs).
Scope: fix the env resolution in docker/entrypoint.sh so the effective session key reaches agent-server through OH_SESSION_API_KEYS_0 (the shared mechanism the launcher and __tests__/scripts/dev-safe.test.ts already assert), regardless of whether it came from LOCAL_BACKEND_API_KEY, an existing OH_SESSION_API_KEYS_0, a persisted key file, or generation. Preserve the existing precedence (OH_SESSION_API_KEYS_0 wins when already set) and existing default generation/persistence. Add regression coverage using the entrypoint-extraction pattern already used by __tests__/scripts/docker-vscode-route-sync.test.ts.
Non-goals: no change to the SDK auth guard (owned by OpenHands/software-agent-sdk); /server_info and /health//alive//ready remain intentionally unauthenticated (server_details_router is mounted without the session-key dependency), so the issue's step 3 repro against /api/server_info is not a valid auth probe — use a genuinely protected /api/* route; no SESSION_API_KEY alias is required, since the repository deliberately omits it in favour of OH_SESSION_API_KEYS_0 (scripts/dev-safe.mjs) and the SDK reads it directly; no Helm or launcher changes.
Acceptance Criteria
- Starting the image with only
LOCAL_BACKEND_API_KEY=my-secret-keyexported (noOH_SESSION_API_KEYS_0, noSESSION_API_KEY) causesopenhands-agent-serverto start withconfig.session_api_keyscontainingmy-secret-key. - Against that stack, an unauthenticated request to a protected agent-server route under
/api/*(e.g.GET /api/conversations/search) returns HTTP 401, and the same request with headerX-Session-API-Key: my-secret-keyreturns HTTP 200. - When
OH_SESSION_API_KEYS_0is already set to a different value thanLOCAL_BACKEND_API_KEY, the entrypoint does not overwrite it, and agent-server accepts theOH_SESSION_API_KEYS_0value (existing precedence preserved). - When neither
LOCAL_BACKEND_API_KEYnorOH_SESSION_API_KEYS_0is set and no persisted key file exists, the entrypoint still generates, persists, and exports a key to agent-server as before, and the generated key differs from any user-supplied value. - The entrypoint completes startup (reaches the "All services started" log) without
set -uo pipefailunbound-variable failure in all four combinations: neither key set, onlyLOCAL_BACKEND_API_KEYset, onlyOH_SESSION_API_KEYS_0set, both set. -
OPENHANDS_AUTOMATION_API_KEY,AUTOMATION_LOCAL_API_KEY,AUTOMATION_AGENT_SERVER_API_KEY, andAUTOMATION_KV_SECRETstill resolve to the effective session key when it came fromLOCAL_BACKEND_API_KEY. - A test that executes the shipped entrypoint env-resolution logic under bash with
LOCAL_BACKEND_API_KEYset asserts the exportedOH_SESSION_API_KEYS_0value; it fails on the currentmainentrypoint and passes after the fix.
Source: OpenHands/OpenHands