Repository navigation
fix: redact credentials from GitCommandError.command field - #3689
Conversation
When run_git_command() raises GitCommandError, the .command attribute retained the raw argument list including any credential-bearing URLs (e.g. https://user:token@host). The error message string was already redacted, but callers that serialize or log the exception object could still expose raw credentials via .command. Apply redact_url_credentials() to each arg before storing it, so the exception is safe to serialize regardless of how callers handle it. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
all-hands-bot
left a comment
There was a problem hiding this comment.
✅ QA Report: PASS
Verified GitCommandError.command no longer exposes credential-bearing URLs in real run_git_command() failures, while no-credential URLs remain readable.
Does this PR achieve its stated goal?
Yes. On origin/main, calling run_git_command() with fake credential-bearing URLs showed GitCommandError.command still contained the fake secret in both the nonzero-exit and timeout paths, even though the exception message was already redacted. On PR head c81ef62a134cff1bf74b16e0ccfaf1ca2871375a, the same real execution paths produced .command values containing https://****@... and no fake secret; a no-credential URL stayed unchanged.
| Phase | Result |
|---|---|
| Environment Setup | ✅ make build completed successfully; no tests/linters were run. |
| CI Status | Validate PR description and Build & Push (python-arm64) are failing; I did not rerun CI. |
| Functional Verification | ✅ Verified nonzero git failure, timeout failure, and no-credential URL behavior by importing and calling the SDK function. |
Functional Verification
Test 1: Nonzero git failure redacts GitCommandError.command
Step 1 — Reproduce baseline without the fix:
Checked out origin/main and ran:
uv run python - <<'PY'
from openhands.sdk.git.utils import run_git_command
from openhands.sdk.git.exceptions import GitCommandError
secret = "TOPSECRET_QA_TOKEN_3689"
url = f"https://oauth2:{secret}@127.0.0.1:1/repo"
try:
run_git_command(["git", "ls-remote", url])
except GitCommandError as exc:
command_text = str(exc.command)
message_text = str(exc)
print(f"command_has_secret={secret in command_text}")
print(f"command_has_redaction={'****' in command_text}")
print(f"message_has_secret={secret in message_text}")
print(f"message_has_redaction={'****' in message_text}")
print(f"command_field={command_text}")
PYObserved:
BASE_NONZERO_EXCEPTION_CAUGHT
command_has_secret=True
command_has_redaction=False
message_has_secret=False
message_has_redaction=True
command_field=['git', 'ls-remote', 'https://oauth2:TOPSECRET_QA_TOKEN_3689@127.0.0.1:1/repo']
This confirms the bug exists on base: the log/message string is redacted, but the serialized .command field still contains the credential-bearing URL.
Step 2 — Apply the PR's changes:
Checked out fix/git-command-error-redact-command-field at c81ef62a134cff1bf74b16e0ccfaf1ca2871375a.
Step 3 — Re-run with the fix in place:
Ran the same SDK call on the PR branch. Observed:
NONZERO:EXCEPTION_CAUGHT
NONZERO:command_has_secret=False
NONZERO:command_has_redaction=True
NONZERO:message_has_secret=False
NONZERO:message_has_redaction=True
NONZERO:exit_code=128
NONZERO:command_field=['git', 'ls-remote', 'https://****@127.0.0.1:1/repo']
This confirms the nonzero-exit path now stores a redacted .command field and preserves the existing redacted message behavior.
Test 2: Timeout failure redacts GitCommandError.command
Step 1 — Reproduce baseline without the fix:
On origin/main, ran a timed-out command with a fake credential-bearing URL argument:
uv run python - <<'PY'
import sys
from openhands.sdk.git.utils import run_git_command
from openhands.sdk.git.exceptions import GitCommandError
secret = "TOPSECRET_QA_TIMEOUT_3689"
url = f"https://oauth2:{secret}@example.invalid/repo"
try:
run_git_command([sys.executable, "-c", "import time; time.sleep(2)", url], timeout=0.1)
except GitCommandError as exc:
command_text = str(exc.command)
message_text = str(exc)
print(f"command_has_secret={secret in command_text}")
print(f"command_has_redaction={'****' in command_text}")
print(f"message_has_secret={secret in message_text}")
print(f"message_has_redaction={'****' in message_text}")
print(f"exit_code={exc.exit_code}")
print(f"stderr={exc.stderr}")
print(f"command_field={command_text}")
PYObserved:
BASE_TIMEOUT_EXCEPTION_CAUGHT
command_has_secret=True
command_has_redaction=False
message_has_secret=False
message_has_redaction=True
exit_code=-1
stderr=Command timed out
command_field=['.../.venv/bin/python3', '-c', 'import time; time.sleep(2)', 'https://oauth2:TOPSECRET_QA_TIMEOUT_3689@example.invalid/repo']
This confirms the timeout path had the same leak in .command on base.
Step 2 — Apply the PR's changes:
Used the same PR checkout at c81ef62a134cff1bf74b16e0ccfaf1ca2871375a.
Step 3 — Re-run with the fix in place:
Ran the same timeout scenario on the PR branch. Observed:
TIMEOUT:EXCEPTION_CAUGHT
TIMEOUT:command_has_secret=False
TIMEOUT:command_has_redaction=True
TIMEOUT:message_has_secret=False
TIMEOUT:message_has_redaction=True
TIMEOUT:exit_code=-1
TIMEOUT:command_field=['.../.venv/bin/python3', '-c', 'import time; time.sleep(2)', 'https://****@example.invalid/repo']
This confirms timeout exceptions now also store a redacted .command field.
Test 3: No-credential URL remains unchanged
On the PR branch, ran:
OPENHANDS_SUPPRESS_BANNER=1 uv run python - <<'PY'
from openhands.sdk.git.utils import run_git_command
from openhands.sdk.git.exceptions import GitCommandError
url = "https://example.invalid/org/repo"
try:
run_git_command(["git", "ls-remote", url], timeout=5)
except GitCommandError as exc:
command_text = str(exc.command)
print(f"command_contains_original_url={url in command_text}")
print(f"command_has_redaction={'****' in command_text}")
print(f"command_field={command_text}")
PYObserved:
NO_CREDENTIAL_EXCEPTION_CAUGHT
command_contains_original_url=True
command_has_redaction=False
command_field=['git', 'ls-remote', 'https://example.invalid/org/repo']
This shows the change does not over-redact ordinary URL arguments without credentials.
Issues Found
None from functional QA.
This review was created by an AI agent (OpenHands) on behalf of the user.
Verdict: PASS
|
Thanks for catching this in the QA review of #2154 and turning it around so quickly — redacting One fix it seems like we ought to make before merge. This PR covers the args = ['git-not-on-path', 'clone', 'https://oauth2:SUPERSECRET@github.com/o/r.git', '/tmp/x']
try:
run_git_command(args)
except GitCommandError as e:
print(e.command)
# ['git-not-on-path', 'clone', 'https://oauth2:SUPERSECRET@github.com/o/r.git', '/tmp/x']
# -> credential survives in .commandSince the three raise sites share the same intent, what do you think about computing the redacted args once near the top and reusing them everywhere? It also keeps the message/log/exception perfectly in sync: redacted_args = [redact_url_credentials(a) for a in args]…then use Two related items I don't think need to block this PR — I've filed as #3751:
Really nice catch! Thanks so much! |
jpshackelford
left a comment
There was a problem hiding this comment.
Thanks very much for this! Looks like one scenario slips through as described in #3689 (comment) but once addressed this should be go to go. Thanks again!
Compute redacted_args once at the top of run_git_command and use it across all three raise sites (non-zero returncode, TimeoutExpired, FileNotFoundError). Removes the duplicate per-branch computation and closes the credential leak when git is not installed but args contain a credential-bearing URL. Adds tests asserting GitCommandError.command is redacted across all three paths. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Thanks for the thorough review and the reproduction script — really helpful! Fixed in b4ba8da: computed Added The two items in #3751 noted as non-blocking look right to me — happy to pick those up in a follow-up. |
…#3689) Co-authored-by: Simon Rosenberg <simon@openhands.dev> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: John-Mason P. Shackelford <jpshack@gmail.com>
HUMAN:
redact credentials from GitCommandError.command field
AGENT:
Why
Spotted in QA review of #2154. When
run_git_command()raisesGitCommandError, the.commandlist retained raw credential-bearing URLs (e.g.https://user:token@host). The error message string was already redacted via_redact_args_for_logging(), but callers that serialize or log the exception object could still expose credentials through.command.Summary
Apply
redact_url_credentials()to each arg before storing it inGitCommandError.command, so the exception is safe to serialize regardless of how callers handle it. TheFileNotFoundErrorpath (git not installed) is left unredacted since it never receives a URL argument.How to Test
Or run the existing git test suite:
uv run pytest tests/sdk/git/ -q🤖 Generated with Claude Code
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.13-nodejs22-slimgolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:b4ba8da-pythonRun
All tags pushed for this build
About Multi-Architecture Support
b4ba8da-python) is a multi-arch manifest supporting both amd64 and arm64b4ba8da-python-amd64) are also available if needed