diff --git a/.jules/sentinel.md b/.jules/sentinel.md new file mode 100644 index 00000000..ba1dda6a --- /dev/null +++ b/.jules/sentinel.md @@ -0,0 +1,5 @@ +## 2025-02-18 - Prevent Command Injection via git config + +**Vulnerability:** Invoking `git` commands via `subprocess` against potentially untrusted directories can lead to local code execution via a malicious `.git/config` file (e.g., using a malicious `core.fsmonitor` script). +**Learning:** `subprocess.run` calls that execute `git` must be secured against malicious repository configurations, even for read-only commands like `rev-parse` or `ls-files`. +**Prevention:** Always apply the configuration `("-c", "core.fsmonitor=false")` (typically defined as `_SAFE_GIT_CONFIG`) to prevent local code execution when running `git` via `subprocess`. diff --git a/src/wardline/core/delta.py b/src/wardline/core/delta.py index c4d63f0e..0c073664 100644 --- a/src/wardline/core/delta.py +++ b/src/wardline/core/delta.py @@ -8,6 +8,8 @@ from wardline.core.errors import WardlineError +_SAFE_GIT_CONFIG = ("-c", "core.fsmonitor=false") + if TYPE_CHECKING: from wardline.scanner.index import Entity @@ -22,7 +24,7 @@ def get_changed_files_since(ref: str, root: Path) -> set[str]: # 1. Get the git toplevel directory. try: res = subprocess.run( - ["git", "rev-parse", "--show-toplevel"], + ["git", *_SAFE_GIT_CONFIG, "rev-parse", "--show-toplevel"], cwd=root, capture_output=True, text=True, @@ -38,7 +40,7 @@ def get_changed_files_since(ref: str, root: Path) -> set[str]: # 2. Resolve ref to a verified object id before passing it to git diff. try: res = subprocess.run( - ["git", "rev-parse", "--verify", "--end-of-options", ref], + ["git", *_SAFE_GIT_CONFIG, "rev-parse", "--verify", "--end-of-options", ref], cwd=git_toplevel, capture_output=True, text=True, @@ -54,7 +56,7 @@ def get_changed_files_since(ref: str, root: Path) -> set[str]: # 3. Get changed files since ref (committed since ref, staged, unstaged). try: res = subprocess.run( - ["git", "diff", "--name-only", verified_ref, "--"], + ["git", *_SAFE_GIT_CONFIG, "diff", "--name-only", verified_ref, "--"], cwd=git_toplevel, capture_output=True, text=True, @@ -68,7 +70,7 @@ def get_changed_files_since(ref: str, root: Path) -> set[str]: # 4. Get untracked files. try: res = subprocess.run( - ["git", "ls-files", "--others", "--exclude-standard"], + ["git", *_SAFE_GIT_CONFIG, "ls-files", "--others", "--exclude-standard"], cwd=git_toplevel, capture_output=True, text=True, diff --git a/src/wardline/core/legis.py b/src/wardline/core/legis.py index 87144974..f0156280 100644 --- a/src/wardline/core/legis.py +++ b/src/wardline/core/legis.py @@ -49,6 +49,8 @@ from wardline.core.safe_paths import safe_project_file from wardline.core.taints import TaintState +_SAFE_GIT_CONFIG = ("-c", "core.fsmonitor=false") + if TYPE_CHECKING: from wardline.core.config import WardlineConfig from wardline.core.run import ScanResult @@ -199,7 +201,7 @@ def _git_tree_sha(root: Path) -> str | None: """ try: rev = subprocess.run( - ["git", "rev-parse", "HEAD^{tree}"], + ["git", *_SAFE_GIT_CONFIG, "rev-parse", "HEAD^{tree}"], cwd=root, capture_output=True, text=True, @@ -215,7 +217,7 @@ def _git_repo_root(root: Path) -> Path | None: """The containing git repository root, or None when unavailable.""" try: rev = subprocess.run( - ["git", "rev-parse", "--show-toplevel"], + ["git", *_SAFE_GIT_CONFIG, "rev-parse", "--show-toplevel"], cwd=root, capture_output=True, text=True, diff --git a/tests/unit/core/test_delta.py b/tests/unit/core/test_delta.py index 5b16c7af..9d0ca7ba 100644 --- a/tests/unit/core/test_delta.py +++ b/tests/unit/core/test_delta.py @@ -43,8 +43,24 @@ def run_dispatch(args, **kwargs): res = get_changed_files_since("HEAD~1", root) assert res == {"foo.py", "bar.py", "baz.py"} - assert mock_run.call_args_list[1].args[0] == ["git", "rev-parse", "--verify", "--end-of-options", "HEAD~1"] - assert mock_run.call_args_list[2].args[0] == ["git", "diff", "--name-only", "abc123", "--"] + assert mock_run.call_args_list[1].args[0] == [ + "git", + "-c", + "core.fsmonitor=false", + "rev-parse", + "--verify", + "--end-of-options", + "HEAD~1", + ] + assert mock_run.call_args_list[2].args[0] == [ + "git", + "-c", + "core.fsmonitor=false", + "diff", + "--name-only", + "abc123", + "--", + ] @patch("subprocess.run")