Repository navigation
Description
I'm an AI agent based on Opus 5.5, helping Engel Nyst (@enyst) with project maintenance.
Is there an existing issue for the same bug?
- I have searched existing issues and this is not a duplicate.
Bug Description
PATCH /api/llm/provider-connections/{id} refuses {"api_key": null} ("api_key cannot be cleared"), but accepts "", whitespace-only strings and the redacted placeholder ********** with 200. Blank values clear the shared key; the placeholder is stored as the literal key. Every linked LLM profile then activates with no key or with **********. A client that sends "" or echoes the redacted value to mean "unchanged" breaks every linked profile.
Creation has the same validation gap: POST /api/llm/provider-connections with a whitespace-only or redacted-placeholder api_key returns 201 with api_key_set: false. The stored model normalizes those values to null, creating a connection that cannot provide credentials. An empty string is already rejected on POST, but min_length=1 does not reject whitespace or the placeholder. This issue covers both creation and updates (F23.conn-create-blank-key and F23.conn-patch-blank-key in #5653).
Expected Behavior
A PATCH whose api_key is "", whitespace-only or ********** returns 422, like api_key: null (or leaves the key unchanged), and the next activation of a linked profile applies the previous key. A POST with an empty, whitespace-only or redacted-placeholder key returns 422 and creates no connection.
Actual Behavior
- POST with
" "or"**********": 201 withapi_key_set: false; a keyless connection is persisted. - PATCH with
""and" ": 200 withapi_key_set: false; activating a linked profile leavesagent_settings.llm.api_keynull. - PATCH with
"**********": 200 withapi_key_set: true; activation setsagent_settings.llm.api_keyto the literal**********. - While the key is blank,
GET /api/profileslists the linked profile withapi_key_set: true, whileGET /api/profiles/{name}saysfalse.
Steps to Reproduce
-
From a checkout:
OH_PERSISTENCE_DIR=$(mktemp -d) OH_SESSION_API_KEYS_0=dev OH_SECRET_KEY=dev-secret uv run python -m openhands.agent_server --port 8000. In a second shell:S=http://localhost:8000; A='X-Session-API-Key: dev'; J='Content-Type: application/json'. -
CONN=$(curl -s -H "$A" -H "$J" -X POST $S/api/llm/provider-connections -d '{"display_name":"qa","api_key":"sk-test-original"}' | jq -r .id) -
curl -s -H "$A" -H "$J" -X POST $S/api/profiles/probe -d "{\"llm\":{\"model\":\"openai/probe\",\"provider_connection_id\":\"$CONN\"}}", thencurl -s -H "$A" -X POST $S/api/profiles/probe/activate. -
curl -s -H "$A" -H 'X-Expose-Secrets: plaintext' $S/api/settings | jq .agent_settings.llm.api_keyprints"sk-test-original". -
curl -s -H "$A" -H "$J" -X PATCH $S/api/llm/provider-connections/$CONN -d '{"api_key":""}'returns 200 with"api_key_set":false. -
Activate
probeagain and repeat step 4: it printsnull. With" "the result is the same; with"**********"step 4 prints"**********". -
The creation case is independent of the PATCH steps:
curl -s -i -H "$A" -H "$J" -X POST $S/api/llm/provider-connections -d '{"display_name":"qa-create-blank","api_key":" "}'returns 201 withapi_key_set: false. Repeat with"api_key":"**********"and another display name: it also creates a keyless connection. Both should return 422 and leave the connection list unchanged.
With the verify-agent-server skill (#5621): control-agent-server map run --file F23 --only F23.conn-create-blank-key,F23.conn-patch-blank-key --fresh reports the known-bug bullet as an expected failure while the bug lasts.
Acceptance Criteria
-
PATCHwithapi_key"", whitespace-only or**********returns 422 (or leaves the key unchanged) and does not bumpupdated_at. - A linked profile activated afterwards still applies the previous key.
- POST with an empty, whitespace-only or redacted-placeholder key returns 422 and leaves the connection list and store unchanged.
- Unit tests cover both creation and update validation, including valid keys.
- The
F23.conn-create-blank-keyandF23.conn-patch-blank-keybullets in the verify-agent-server feature map pass, and their known-bug markers are removed.
Installation Method
uv sync in a checkout of main
Version
main at 2804c12
Additional Context
Root cause, in openhands-agent-server/openhands/agent_server/provider_connections_router.py:
ProviderConnectionCreateRequest.api_keyhas onlymin_length=1, so whitespace-only strings and the redacted placeholder reach the stored model, which normalizes them to null.ProviderConnectionUpdateRequest.api_keyis an unconstrainedSecretStr | None(:63); the request validator only rejects nulldisplay_name/provider(:68-76), and the handler only rejectsapi_key: null(:213-217).- The handler builds the stored model with
connection.model_copy(update=updates)(:225), which skips validators, soProviderConnection._validate_api_key(openhands-sdk/openhands/sdk/llm/provider_connection_store.py:99-127) never sees the value and""or**********is encrypted and written as the key.
Downstream, LLMProfileStore._resolve_provider_connection copies the key only when api_key_value() is not None (openhands-sdk/openhands/sdk/llm/llm_profile_store.py:376-381), so a blank key leaves the profile with none and the placeholder is applied verbatim. LLMProfileStore.list_summaries reads the connection without a cipher (llm_profile_store.py:470-474), so the Fernet token of the empty key counts as set.
Suggested fix: validate api_key on both ProviderConnectionCreateRequest and ProviderConnectionUpdateRequest that rejects empty, whitespace-only and REDACTED_SECRET_VALUE values with 422 before persistence, preserving PATCH's existing null rejection. Replacing model_copy with model_validate alone is not enough: the stored model's validator maps those values to None, which still clears the key.
OpenHands AI triage
The following comments and acceptance criteria were added by the OpenHands AI agent.
Triage
Scope confirmed against main. ProviderConnectionUpdateRequest.api_key is an unconstrained SecretStr | None (openhands-agent-server/openhands/agent_server/provider_connections_router.py), the PATCH handler rejects only api_key: null, and connection.model_copy(update=updates) skips the stored model's _validate_api_key. Blank (""), whitespace-only, and ********** values are therefore accepted and persisted instead of returning 422, clearing or corrupting the shared key for every linked profile. POST also accepts whitespace-only and redacted-placeholder values: its min_length=1 check passes, then the stored model normalizes the key to null and a keyless connection is created with 201. This is a credential-handling correctness gap in an existing endpoint, so it is in scope for this repository; the fix is a small, additive validation change.
Bounded scope: reject "", whitespace-only, and the REDACTED_SECRET_VALUE sentinel with 422 on both POST /api/llm/provider-connections and PATCH /api/llm/provider-connections/{id}. A rejected POST creates nothing; a rejected PATCH preserves the existing key and updated_at, consistent with the existing rejection of api_key: null. Non-goals: no REST contract removal, no new provider-specific behavior, and no change to unrelated display_name/provider/base_url handling.
Priority rationale: priority:low by default. The failure is reachable only by a client that sends a blank or redacted key by choice; there is no user report, support evidence, or reproduced failure in a real workflow, and the defect was surfaced by code inspection plus a synthetic reproduction.
Acceptance Criteria
-
POST /api/llm/provider-connectionswith an empty, whitespace-only, or redacted-placeholder key returns 422 and persists no connection; a valid key still creates a usable connection with 201. -
PATCH /api/llm/provider-connections/{id}withapi_keyequal to"", a whitespace-only string, or**********returns 422 and leaves both the stored key andupdated_atunchanged. - A linked profile activated after such a rejected PATCH still applies the key set before the request (
GET /api/settingswithX-Expose-Secrets: plaintextreports the prior key). - A valid key in the same PATCH still rotates the shared key and returns 200;
api_key: nullcontinues to return 422 (no regression to existing create/update behavior). - The request validator uses the canonical
REDACTED_SECRET_VALUEsentinel fromopenhands.sdk.utils.pydantic_secrets, not a hand-rolled literal, and no secret value appears in the 422 response body or logs. - A test added to
tests/agent_server/test_profiles_router.pycovers both POST and PATCH: the three rejected values (blank, whitespace-only, redacted), no entry after a rejected POST, and the unchanged-key/updated_atoutcome after a rejected PATCH; the suiteuv run pytest tests/agent_serverpasses.
Activity
[-][Bug]: PATCH provider connection with blank or "**********" api_key returns 200 and replaces the key, not 422[/-][+][Bug]: Provider connection POST/PATCH accepts blank or redacted api_key values[/+]enyst commentedon Oct 11, 2026
Adding the creation case from #5653 (
F23.conn-create-blank-key) to this issue.POST /api/llm/provider-connectionswith a whitespace-only or**********key currently returns 201 and stores a keyless connection (api_key_set: false). An empty string already gets 422, butmin_length=1allows whitespace and the placeholder.The earlier triage note excluding POST was therefore too narrow. I updated the title, reproduction, scope and acceptance criteria to cover both POST and PATCH: reject unusable keys before persistence; a rejected POST creates nothing, and a rejected PATCH preserves the existing key and timestamp. Both feature-map cases are now linked here.
Posted by Codex on behalf of @enyst.