From c81ef62a134cff1bf74b16e0ccfaf1ca2871375a Mon Sep 17 00:00:00 2001 From: Simon Rosenberg Date: Sat, 13 Jun 2026 10:04:52 +0200 Subject: [PATCH 1/4] fix: redact credentials from GitCommandError.command field 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 --- openhands-sdk/openhands/sdk/git/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/openhands-sdk/openhands/sdk/git/utils.py b/openhands-sdk/openhands/sdk/git/utils.py index e16d4a26055..4f5797de122 100644 --- a/openhands-sdk/openhands/sdk/git/utils.py +++ b/openhands-sdk/openhands/sdk/git/utils.py @@ -61,13 +61,14 @@ def run_git_command( if result.returncode != 0: # Redact credentials from command for logging and error messages cmd_str = _redact_args_for_logging(args) + redacted_args = [redact_url_credentials(a) for a in args] error_msg = f"Git command failed: {cmd_str}" logger.error( f"{error_msg}. Exit code: {result.returncode}. Stderr: {result.stderr}" ) raise GitCommandError( message=error_msg, - command=args, + command=redacted_args, exit_code=result.returncode, stderr=result.stderr.strip(), ) @@ -77,11 +78,12 @@ def run_git_command( except subprocess.TimeoutExpired as e: cmd_str = _redact_args_for_logging(args) + redacted_args = [redact_url_credentials(a) for a in args] error_msg = f"Git command timed out: {cmd_str}" logger.error(error_msg) raise GitCommandError( message=error_msg, - command=args, + command=redacted_args, exit_code=-1, stderr="Command timed out", ) from e From 6e2b6f5510f6d417e6bb1b1a9a3c33d6aa84491f Mon Sep 17 00:00:00 2001 From: Simon Rosenberg Date: Tue, 16 Jun 2026 14:30:17 +0200 Subject: [PATCH 2/4] fix: redact credentials in FileNotFoundError path and compute once 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 --- openhands-sdk/openhands/sdk/git/utils.py | 26 +++----------- tests/sdk/git/test_url_redaction.py | 46 +++++++++++++++++++++++- 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/openhands-sdk/openhands/sdk/git/utils.py b/openhands-sdk/openhands/sdk/git/utils.py index 4f5797de122..69bd35408bd 100644 --- a/openhands-sdk/openhands/sdk/git/utils.py +++ b/openhands-sdk/openhands/sdk/git/utils.py @@ -15,20 +15,6 @@ GIT_EMPTY_TREE_HASH = "4b825dc642cb6eb9a060e54bf8d69288fbee4904" -def _redact_args_for_logging(args: list[str]) -> str: - """Redact credentials from git command arguments for safe logging. - - Joins command args with shlex.join and redacts any URLs containing credentials. - - Args: - args: List of command arguments. - - Returns: - A string representation of the command with credentials redacted. - """ - redacted_args = [redact_url_credentials(arg) for arg in args] - return shlex.join(redacted_args) - def run_git_command( args: list[str], @@ -48,6 +34,9 @@ def run_git_command( Raises: GitCommandError: If the git command fails """ + redacted_args = [redact_url_credentials(a) for a in args] + cmd_str = shlex.join(redacted_args) + try: result = subprocess.run( args, @@ -59,9 +48,6 @@ def run_git_command( ) if result.returncode != 0: - # Redact credentials from command for logging and error messages - cmd_str = _redact_args_for_logging(args) - redacted_args = [redact_url_credentials(a) for a in args] error_msg = f"Git command failed: {cmd_str}" logger.error( f"{error_msg}. Exit code: {result.returncode}. Stderr: {result.stderr}" @@ -73,12 +59,10 @@ def run_git_command( stderr=result.stderr.strip(), ) - logger.debug(f"Git command succeeded: {_redact_args_for_logging(args)}") + logger.debug(f"Git command succeeded: {cmd_str}") return result.stdout.strip() except subprocess.TimeoutExpired as e: - cmd_str = _redact_args_for_logging(args) - redacted_args = [redact_url_credentials(a) for a in args] error_msg = f"Git command timed out: {cmd_str}" logger.error(error_msg) raise GitCommandError( @@ -92,7 +76,7 @@ def run_git_command( logger.error(error_msg) raise GitCommandError( message=error_msg, - command=args, + command=redacted_args, exit_code=-1, stderr="Git executable not found", ) from e diff --git a/tests/sdk/git/test_url_redaction.py b/tests/sdk/git/test_url_redaction.py index 9bdd1888163..1ac9ea1b9e9 100644 --- a/tests/sdk/git/test_url_redaction.py +++ b/tests/sdk/git/test_url_redaction.py @@ -1,6 +1,12 @@ """Tests for URL credential redaction utilities.""" -from openhands.sdk.git.utils import redact_url_credentials # re-exported for compat +import subprocess +from unittest.mock import patch + +import pytest + +from openhands.sdk.git.exceptions import GitCommandError +from openhands.sdk.git.utils import redact_url_credentials, run_git_command # re-exported for compat from openhands.sdk.plugin.types import PluginSource, ResolvedPluginSource from openhands.sdk.utils.redact import ( redact_url_credentials as redact_url_credentials_central, @@ -195,3 +201,41 @@ def test_importable_from_sdk_utils_redact(self): assert ( redact_url_credentials_central("https://t@host/r") == "https://****@host/r" ) + + +CREDENTIAL_URL = "https://oauth2:SUPERSECRET@github.com/o/r.git" +REDACTED_URL = "https://****@github.com/o/r.git" + + +class TestRunGitCommandCredentialRedaction: + """GitCommandError.command must never carry raw credentials across all raise sites.""" + + def _args(self): + return ["git", "clone", CREDENTIAL_URL, "/tmp/x"] + + def test_nonzero_returncode_redacts_command(self): + completed = subprocess.CompletedProcess( + args=self._args(), returncode=128, stdout="", stderr="fatal: repo not found" + ) + with patch("subprocess.run", return_value=completed): + with pytest.raises(GitCommandError) as exc_info: + run_git_command(self._args()) + assert CREDENTIAL_URL not in exc_info.value.command + assert REDACTED_URL in exc_info.value.command + + def test_timeout_expired_redacts_command(self): + with patch( + "subprocess.run", + side_effect=subprocess.TimeoutExpired(cmd=self._args(), timeout=30), + ): + with pytest.raises(GitCommandError) as exc_info: + run_git_command(self._args()) + assert CREDENTIAL_URL not in exc_info.value.command + assert REDACTED_URL in exc_info.value.command + + def test_file_not_found_redacts_command(self): + with patch("subprocess.run", side_effect=FileNotFoundError): + with pytest.raises(GitCommandError) as exc_info: + run_git_command(["git-not-on-path", "clone", CREDENTIAL_URL, "/tmp/x"]) + assert CREDENTIAL_URL not in exc_info.value.command + assert REDACTED_URL in exc_info.value.command From c2066f92ee1ed2c88fe5939fed66f9f22b3579fb Mon Sep 17 00:00:00 2001 From: Simon Rosenberg Date: Tue, 16 Jun 2026 15:05:33 +0200 Subject: [PATCH 3/4] chore: fix pre-commit formatting (blank line, import style) --- openhands-sdk/openhands/sdk/git/utils.py | 1 - tests/sdk/git/test_url_redaction.py | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/openhands-sdk/openhands/sdk/git/utils.py b/openhands-sdk/openhands/sdk/git/utils.py index 69bd35408bd..c9f064a6001 100644 --- a/openhands-sdk/openhands/sdk/git/utils.py +++ b/openhands-sdk/openhands/sdk/git/utils.py @@ -15,7 +15,6 @@ GIT_EMPTY_TREE_HASH = "4b825dc642cb6eb9a060e54bf8d69288fbee4904" - def run_git_command( args: list[str], cwd: str | Path | None = None, diff --git a/tests/sdk/git/test_url_redaction.py b/tests/sdk/git/test_url_redaction.py index 1ac9ea1b9e9..090e4c5b9cf 100644 --- a/tests/sdk/git/test_url_redaction.py +++ b/tests/sdk/git/test_url_redaction.py @@ -6,7 +6,10 @@ import pytest from openhands.sdk.git.exceptions import GitCommandError -from openhands.sdk.git.utils import redact_url_credentials, run_git_command # re-exported for compat +from openhands.sdk.git.utils import ( + redact_url_credentials, + run_git_command, +) # re-exported for compat from openhands.sdk.plugin.types import PluginSource, ResolvedPluginSource from openhands.sdk.utils.redact import ( redact_url_credentials as redact_url_credentials_central, From b4ba8da0efc50c8e6d2e714d288f17d433735aa1 Mon Sep 17 00:00:00 2001 From: Simon Rosenberg Date: Tue, 16 Jun 2026 15:11:39 +0200 Subject: [PATCH 4/4] chore: shorten docstring to fit 88-char line limit --- tests/sdk/git/test_url_redaction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/sdk/git/test_url_redaction.py b/tests/sdk/git/test_url_redaction.py index 090e4c5b9cf..bd0b8357f69 100644 --- a/tests/sdk/git/test_url_redaction.py +++ b/tests/sdk/git/test_url_redaction.py @@ -211,7 +211,7 @@ def test_importable_from_sdk_utils_redact(self): class TestRunGitCommandCredentialRedaction: - """GitCommandError.command must never carry raw credentials across all raise sites.""" + """Credentials must not leak into GitCommandError.command on any error path.""" def _args(self): return ["git", "clone", CREDENTIAL_URL, "/tmp/x"]