Skip to content

fix: redact API key from validate_profile error responses and logs - #4506

Merged
neubig merged 1 commit into
mainfrom
fix/validate-profile-redact-api-key
Aug 16, 2026
Merged

neubig merged 1 commit into
mainfrom
fix/validate-profile-redact-api-key

Conversation

@all-hands-bot

@all-hands-bot all-hands-bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Collaborator

HUMAN:

Given that this is reusing existing functionality in an appropriate place I think it makes sense.


AGENT:

Why

Source PR reviewed: #4422 — "feat: add pre-flight LLM validation endpoint (POST /api/profiles/{name}/validate)" (commit 46ad3d43).

Risk lens: #8 (Credentials, redaction, and sensitive persistence boundaries) and #10 (Negative-space and silent-success cases).

Deterministic reproduction and impact:

The validate_profile endpoint catches LLMError and generic Exception and returns exc.message / str(exc) directly in the HTTP JSON response body (error.message) and in the server log line. When an LLM provider includes the submitted API key in its error message — as OpenAI does with "Incorrect API key provided: sk-proj-abc123..." — the key is leaked to the API client and persisted in server logs.

Reproduction probe (run against unmodified main):

# Mock LLM.acompletion to raise LLMAuthenticationError containing the API key
from openhands.sdk.llm.exceptions import LLMAuthenticationError

with patch("openhands.sdk.llm.llm.LLM.acompletion",
           side_effect=LLMAuthenticationError(
               "Incorrect API key provided: sk-proj-abc123defGHIjklMNOpqrsTUVwxyz1234567890")):
    response = client.post("/api/profiles/leaky/validate",
        json={"llm": {"model": "gpt-4o", "api_key": "sk-proj-abc123defGHIjklMNOpqrsTUVwxyz1234567890"}})

body = response.json()
# body["error"]["message"] == "Incorrect API key provided: sk-proj-abc123defGHIjklMNOpqrsTUVwxyz1234567890"
# ^ API key is in the response!

Captured log output on main:

INFO profiles_router:profiles_router.py:301 Profile 'leaky' pre-flight failed: LLMAuthenticationError: Incorrect API key provided: sk-proj-abc123defGHIjklMNOpqrsTUVwxyz1234567890

Root cause: The endpoint did not apply any redaction to error messages before returning them to the client or logging them. The SDK already ships redact_text_secrets() in openhands.sdk.utils.redact, but it was not called.

Summary

  • Apply redact_text_secrets() to exc.message in the LLMError catch branch before returning it in the response or logging it.
  • Apply redact_text_secrets() to str(exc) in the generic Exception catch branch for the same reason.
  • Add two regression tests: one for the LLMAuthenticationError path and one for the generic RuntimeError path, both asserting the API key does not appear in the response.

Issue Number

N/A — found during weekly regression hunt.

How to Test

Proof that the regression test fails on unmodified main:

# On main (before fix):
uv run pytest tests/agent_server/test_profiles_router.py -k "redacts_api_key" -x -q
# Result: FAILED
# AssertionError: API key must not appear in the validate error response
# assert 'sk-proj-abc123...yz1234567890' not in 'Incorrect A...yz1234567890'

After fix:

uv run pytest tests/agent_server/test_profiles_router.py -k "redacts_api_key" -x -q
# Result: 2 passed

# Full validate_profile suite:
uv run pytest tests/agent_server/test_profiles_router.py -k "validate_profile" -x -q
# Result: 10 passed (8 existing + 2 new)

# Full profiles_router suite:
uv run pytest tests/agent_server/test_profiles_router.py -x -q
# Result: 88 passed

End-to-end validation: The regression test constructs a real TestClient against the FastAPI app, patches LLM.acompletion to raise an LLMAuthenticationError whose message contains a realistic OpenAI-style API key (sk-proj-abc123...), and asserts the key is absent from the JSON response body. A second test covers the generic Exception path with a RuntimeError containing the same key.

Why the original tests did not catch the case: The existing tests for validate_profile mock LLM.acompletion to raise errors with messages like "Connection refused" that do not contain secrets, so they pass regardless of whether redaction is applied. No test exercised the case where the provider error message contains the submitted API key — which is exactly the real-world scenario (OpenAI returns the key in auth error messages).

Video/Screenshots

N/A — text-based reproduction; output included above.

Design Doc

N/A — minimal fix.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes


🐳 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

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python amd64, arm64 nikolaik/python-nodejs:python3.13-nodejs22-slim Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:648221b-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-648221b-python \
  ghcr.io/openhands/agent-server:648221b-python

All tags pushed for this build

ghcr.io/openhands/agent-server:648221b-golang-amd64
ghcr.io/openhands/agent-server:648221bb52d0ee3a40f32c59c84f23f13dc74f9e-golang-amd64
ghcr.io/openhands/agent-server:fix-validate-profile-redact-api-key-golang-amd64
ghcr.io/openhands/agent-server:648221b-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:648221b-golang-arm64
ghcr.io/openhands/agent-server:648221bb52d0ee3a40f32c59c84f23f13dc74f9e-golang-arm64
ghcr.io/openhands/agent-server:fix-validate-profile-redact-api-key-golang-arm64
ghcr.io/openhands/agent-server:648221b-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:648221b-java-amd64
ghcr.io/openhands/agent-server:648221bb52d0ee3a40f32c59c84f23f13dc74f9e-java-amd64
ghcr.io/openhands/agent-server:fix-validate-profile-redact-api-key-java-amd64
ghcr.io/openhands/agent-server:648221b-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:648221b-java-arm64
ghcr.io/openhands/agent-server:648221bb52d0ee3a40f32c59c84f23f13dc74f9e-java-arm64
ghcr.io/openhands/agent-server:fix-validate-profile-redact-api-key-java-arm64
ghcr.io/openhands/agent-server:648221b-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:648221b-python-amd64
ghcr.io/openhands/agent-server:648221bb52d0ee3a40f32c59c84f23f13dc74f9e-python-amd64
ghcr.io/openhands/agent-server:fix-validate-profile-redact-api-key-python-amd64
ghcr.io/openhands/agent-server:648221b-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:648221b-python-arm64
ghcr.io/openhands/agent-server:648221bb52d0ee3a40f32c59c84f23f13dc74f9e-python-arm64
ghcr.io/openhands/agent-server:fix-validate-profile-redact-api-key-python-arm64
ghcr.io/openhands/agent-server:648221b-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:648221b-golang
ghcr.io/openhands/agent-server:648221bb52d0ee3a40f32c59c84f23f13dc74f9e-golang
ghcr.io/openhands/agent-server:fix-validate-profile-redact-api-key-golang
ghcr.io/openhands/agent-server:648221b-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:648221b-java
ghcr.io/openhands/agent-server:648221bb52d0ee3a40f32c59c84f23f13dc74f9e-java
ghcr.io/openhands/agent-server:fix-validate-profile-redact-api-key-java
ghcr.io/openhands/agent-server:648221b-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:648221b-python
ghcr.io/openhands/agent-server:648221bb52d0ee3a40f32c59c84f23f13dc74f9e-python
ghcr.io/openhands/agent-server:fix-validate-profile-redact-api-key-python
ghcr.io/openhands/agent-server:648221b-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim

About Multi-Architecture Support

  • Each variant tag (e.g., 648221b-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 648221b-python-amd64) are also available if needed

The validate_profile endpoint (POST /api/profiles/{name}/validate) returned
exc.message directly in both the HTTP response body and the log line. When
LLM providers include the submitted API key in error messages (e.g. OpenAI's
'Incorrect API key provided: sk-proj-...'), the key was leaked to the client
and into server logs.

Apply redact_text_secrets() to all error messages before returning or logging
them, covering both the LLMError branch and the generic Exception branch.

Regression test verifies the key does not appear in the response for both
LLMAuthenticationError and generic RuntimeError paths.

Co-authored-by: openhands <openhands@all-hands.dev>
@all-hands-bot
all-hands-bot requested a review from neubig August 16, 2026 00:43
@github-actions

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   profiles_router.py179697%443–448
TOTAL407611719358% 

@all-hands-bot

Copy link
Copy Markdown
Collaborator Author

👋 This PR needs a couple of things fixed before OpenHands can review it:

  • the PR description's HUMAN: section needs at least 20 characters describing what you tested, not just the template placeholder

Push an update once this is addressed and this check re-runs automatically.

This is an automated check - no AI was used to generate this comment.

@neubig neubig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

@neubig
neubig merged commit b562212 into main Aug 16, 2026
39 of 41 checks passed
@neubig
neubig deleted the fix/validate-profile-redact-api-key branch August 16, 2026 23:00
neubig pushed a commit that referenced this pull request Aug 17, 2026
…4506)

Co-authored-by: openhands <openhands@all-hands.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants