Skip to content

fix: redact credentials from GitCommandError.command field - #3689

Merged
simonrosenberg merged 6 commits into
mainfrom
fix/git-command-error-redact-command-field
Jun 17, 2026
Merged

simonrosenberg merged 6 commits into
mainfrom
fix/git-command-error-redact-command-field

Conversation

@simonrosenberg

@simonrosenberg simonrosenberg commented Jun 13, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

redact credentials from GitCommandError.command field

  • A human has tested these changes.

AGENT:

Why

Spotted in QA review of #2154. When run_git_command() raises GitCommandError, the .command list 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 in GitCommandError.command, so the exception is safe to serialize regardless of how callers handle it. The FileNotFoundError path (git not installed) is left unredacted since it never receives a URL argument.

How to Test

from openhands.sdk.git.utils import run_git_command
from openhands.sdk.git.exceptions import GitCommandError

try:
    run_git_command(["git", "ls-remote", "https://oauth2:SECRET@127.0.0.1:1/repo"])
except GitCommandError as e:
    assert "SECRET" not in str(e.command), f"Credential leaked: {e.command}"
    assert "****" in str(e.command)
    print("PASS")

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

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:b4ba8da-python

Run

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

All tags pushed for this build

ghcr.io/openhands/agent-server:b4ba8da-golang-amd64
ghcr.io/openhands/agent-server:b4ba8da0efc50c8e6d2e714d288f17d433735aa1-golang-amd64
ghcr.io/openhands/agent-server:fix-git-command-error-redact-command-field-golang-amd64
ghcr.io/openhands/agent-server:b4ba8da-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:b4ba8da-golang-arm64
ghcr.io/openhands/agent-server:b4ba8da0efc50c8e6d2e714d288f17d433735aa1-golang-arm64
ghcr.io/openhands/agent-server:fix-git-command-error-redact-command-field-golang-arm64
ghcr.io/openhands/agent-server:b4ba8da-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:b4ba8da-java-amd64
ghcr.io/openhands/agent-server:b4ba8da0efc50c8e6d2e714d288f17d433735aa1-java-amd64
ghcr.io/openhands/agent-server:fix-git-command-error-redact-command-field-java-amd64
ghcr.io/openhands/agent-server:b4ba8da-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:b4ba8da-java-arm64
ghcr.io/openhands/agent-server:b4ba8da0efc50c8e6d2e714d288f17d433735aa1-java-arm64
ghcr.io/openhands/agent-server:fix-git-command-error-redact-command-field-java-arm64
ghcr.io/openhands/agent-server:b4ba8da-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:b4ba8da-python-amd64
ghcr.io/openhands/agent-server:b4ba8da0efc50c8e6d2e714d288f17d433735aa1-python-amd64
ghcr.io/openhands/agent-server:fix-git-command-error-redact-command-field-python-amd64
ghcr.io/openhands/agent-server:b4ba8da-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:b4ba8da-python-arm64
ghcr.io/openhands/agent-server:b4ba8da0efc50c8e6d2e714d288f17d433735aa1-python-arm64
ghcr.io/openhands/agent-server:fix-git-command-error-redact-command-field-python-arm64
ghcr.io/openhands/agent-server:b4ba8da-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:b4ba8da-golang
ghcr.io/openhands/agent-server:b4ba8da0efc50c8e6d2e714d288f17d433735aa1-golang
ghcr.io/openhands/agent-server:fix-git-command-error-redact-command-field-golang
ghcr.io/openhands/agent-server:b4ba8da-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:b4ba8da-java
ghcr.io/openhands/agent-server:b4ba8da0efc50c8e6d2e714d288f17d433735aa1-java
ghcr.io/openhands/agent-server:fix-git-command-error-redact-command-field-java
ghcr.io/openhands/agent-server:b4ba8da-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:b4ba8da-python
ghcr.io/openhands/agent-server:b4ba8da0efc50c8e6d2e714d288f17d433735aa1-python
ghcr.io/openhands/agent-server:fix-git-command-error-redact-command-field-python
ghcr.io/openhands/agent-server:b4ba8da-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim

About Multi-Architecture Support

  • Each variant tag (e.g., b4ba8da-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., b4ba8da-python-amd64) are also available if needed

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>
@github-actions

github-actions Bot commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@simonrosenberg
simonrosenberg marked this pull request as ready for review June 13, 2026 08:17
@simonrosenberg simonrosenberg self-assigned this Jun 13, 2026

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

✅ 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 ⚠️ Existing checks are not all green: 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}")
PY

Observed:

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}")
PY

Observed:

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}")
PY

Observed:

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

@jpshackelford

Copy link
Copy Markdown
Member

Thanks for catching this in the QA review of #2154 and turning it around so quickly — redacting .command so the exception is safe to serialize and it closes a real gap. 🙏

One fix it seems like we ought to make before merge. This PR covers the returncode != 0 and TimeoutExpired paths, but the third raise GitCommandError(...) — the FileNotFoundError branch — still passes command=args unredacted. The PR description notes this is fine because that path "never receives a URL argument," but I think FileNotFoundError is actually raised by subprocess.run based on args[0] (the executable) regardless of the rest of args, so a credential-bearing URL further down the list still survives. CachedRepo.clone() passes the clone URL straight into run_git_command, so I was able to reproduce a leak against the current code:

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 .command

Since 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 redacted_args for the message, the debug log, and all three command= arguments (including the FileNotFoundError branch). A small test asserting no credentials leak into .command across all three paths (failure / timeout / git-not-found) would lock it in nicely.

Two related items I don't think need to block this PR — I've filed as #3751:

  • The .stderr field is still stored/logged raw (utils.py:66). Git usually strips userinfo from URLs it echoes, so it's lower risk, but it's the same serialization concern. Worth noting redact_url_credentials() is anchored (^…$) and won't catch a URL embedded inside stderr text, so this one needs a small substring-capable redactor rather than a drop-in.
  • workspace/repo.py:149 logs a raw user-provided URL, and repo.py's _mask_token/_mask_url duplicate this redaction logic — both could eventually consolidate onto the central helper.

Really nice catch! Thanks so much!

@jpshackelford jpshackelford 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.

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>
@github-actions

github-actions Bot commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/git
   utils.py1232480%101–103, 191–192, 199–204, 209–210, 220–225, 235–237, 265, 371
TOTAL31186854772% 

@simonrosenberg

Copy link
Copy Markdown
Member Author

Thanks for the thorough review and the reproduction script — really helpful!

Fixed in b4ba8da: computed redacted_args once before the try block and used it across all three raise sites, including FileNotFoundError. Also removed the now-dead _redact_args_for_logging helper since the single upfront computation covers everything.

Added TestRunGitCommandCredentialRedaction with three tests — one per raise path (non-zero returncode, TimeoutExpired, FileNotFoundError) — asserting the raw URL never appears in .command and the redacted form does.

The two items in #3751 noted as non-blocking look right to me — happy to pick those up in a follow-up.

pengzhang-create pushed a commit to Pyromind-Dynamics/software-agent-sdk that referenced this pull request Aug 13, 2026
…#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>
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