diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-10-13 20:57:12 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-10-13 20:57:12 +0000 |
| commit | eb638fbf680ae279a9e6699d2243344ccddc9624 (patch) | |
| tree | 1395791e035b6378d770bd10a7fddda517dba16b /plugins | |
| parent | f359d43b406057f1a8cdbc4ea93b827642c162e6 (diff) | |
security-guidance: read diffs the same whatever the user's prefix settings
With diff.mnemonicPrefix or diff.noprefix set in the user's git config, diff headers no longer read `a/<path> b/<path>`, the parser matched no file, and the review was skipped with nothing shown. mnemonicPrefix switched off the Stop review; noprefix switched off the commit and push reviews as well.
Pass the default prefix settings on every git call the hook makes.
Fixes #4569
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/security-guidance/hooks/gitutil.py | 7 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_diff_prefix_config.py | 70 |
2 files changed, 77 insertions, 0 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py index 2036b45e..03eac98b 100644 --- a/plugins/security-guidance/hooks/gitutil.py +++ b/plugins/security-guidance/hooks/gitutil.py @@ -41,6 +41,13 @@ GIT_CMD = [ # content same) rewrites the index under index.lock, GIT_OPTIONAL_LOCKS or # not. Off, every diff here only reads the index, so none needs a copy. "-c", "diff.autoRefreshIndex=false", + # The diff parsers read `diff --git a/<old> b/<new>` headers. A user's + # diff.mnemonicPrefix (c/ w/ i/), diff.noprefix or diff.srcPrefix / + # diff.dstPrefix changes that shape, and every file would be dropped. + "-c", "diff.mnemonicPrefix=false", + "-c", "diff.noprefix=false", + "-c", "diff.srcPrefix=a/", + "-c", "diff.dstPrefix=b/", ] SAFE_GIT_CONFIG = ( diff --git a/plugins/security-guidance/tests/test_diff_prefix_config.py b/plugins/security-guidance/tests/test_diff_prefix_config.py new file mode 100644 index 00000000..ce2a8017 --- /dev/null +++ b/plugins/security-guidance/tests/test_diff_prefix_config.py @@ -0,0 +1,70 @@ +"""A user's diff prefix settings must not hide files from the reviews.""" +import os +import subprocess + +import pytest + +from conftest import ( + GIT_ENV, VULN_PY, commit_file, bash_payload, git, make_repo, metrics_of, + run_hook, stop_payload, ups_payload, +) + +import gitutil + +PREFIX_CONFIGS = [ + ("diff.mnemonicPrefix", "true"), + ("diff.noprefix", "true"), + ("diff.srcPrefix", "x/"), + ("diff.dstPrefix", "y/"), +] + + +@pytest.fixture +def git_env(monkeypatch): + for k, v in GIT_ENV.items(): + monkeypatch.setenv(k, v) + + +@pytest.mark.parametrize("key,value", PREFIX_CONFIGS) +class TestParsers: + def test_work_tree_diff(self, tmp_path, git_env, key, value): + repo = make_repo(tmp_path / "repo", {"app.py": "x = 1\n"}) + git(repo, "config", key, value) + (repo / "app.py").write_text("x = 1\ny = 2\n") + diff = gitutil.get_git_diff(str(repo), "HEAD") + assert [f for f, _ in gitutil.parse_diff_into_files(diff)] == ["app.py"] + assert gitutil.extract_file_paths_from_diff(diff) == ["app.py"] + + def test_commit_diff(self, tmp_path, git_env, key, value): + repo = make_repo(tmp_path / "repo", {"app.py": "x = 1\n"}) + git(repo, "config", key, value) + commit_file(repo, "app.py", "x = 1\ny = 2\n") + show = subprocess.run( + [*gitutil.GIT_CMD, "show", "-p", "--no-color", "HEAD", "--"], + cwd=repo, capture_output=True, + ).stdout.decode("utf-8", errors="replace") + assert [f for f, _ in gitutil.parse_diff_into_files(show)] == ["app.py"] + diff = gitutil._git_diff_range(str(repo), "HEAD~1") + assert [f for f, _ in gitutil.parse_diff_into_files(diff)] == ["app.py"] + + +@pytest.mark.parametrize("key,value", PREFIX_CONFIGS[:2]) +class TestWholeHook: + def test_stop_review_runs(self, tmp_path, hook_env, stub_api, key, value): + repo = make_repo(tmp_path / "repo", {"app.py": "x = 1\n"}) + git(repo, "config", key, value) + run_hook(ups_payload(repo), hook_env) + (repo / "app.py").write_text(VULN_PY) + rc, so, se = run_hook(stop_payload(repo), hook_env) + m = metrics_of(so) + assert m.get("skip_reason") is None, m + assert stub_api.calls + + +class TestWholeHookCommit: + def test_commit_review_runs_with_noprefix(self, tmp_path, hook_env, stub_api): + repo = make_repo(tmp_path / "repo", {"app.py": "x = 1\n"}) + git(repo, "config", "diff.noprefix", "true") + sha, out = commit_file(repo, "app.py", VULN_PY) + rc, so, se = run_hook(bash_payload(repo, "git commit -m change", stdout=out), hook_env) + assert metrics_of(so).get("skip_reason") != 30 |
