diff options
| author | Mohamed Hegazy <8000722+mhegazy@users.noreply.github.com> | 2026-10-20 16:56:28 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-10-20 16:56:28 -0700 |
| commit | 5d6e714341450ba38d85c8202e6a2a15d6af5a04 (patch) | |
| tree | d8d7f8a0673307945d404fbcea8dd713dcade4d0 /plugins/security-guidance/hooks/gitutil.py | |
| parent | 0887770a2d2470ba2e0f5e394ebd22b3e33e360f (diff) | |
| parent | fbfd03863903dc281cc5f56ddf8c1ed4e660f69f (diff) | |
Merge pull request #6591 from anthropics/mhegazy/sg-stop-pathspec
security-guidance: keep the Stop review working when the changed paths do not fit one command
Diffstat (limited to 'plugins/security-guidance/hooks/gitutil.py')
| -rw-r--r-- | plugins/security-guidance/hooks/gitutil.py | 164 |
1 files changed, 145 insertions, 19 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py index 4177a573..d72f3fcf 100644 --- a/plugins/security-guidance/hooks/gitutil.py +++ b/plugins/security-guidance/hooks/gitutil.py @@ -15,9 +15,11 @@ Functions that DO compose patched leaves (``compute_v2_review_set``, ``security_reminder_hook.py`` for that reason. """ import contextlib +import errno import os import re import subprocess +import sys import tempfile import threading import time @@ -169,6 +171,119 @@ def _diff_pathspec(cwd, paths): return ["--"] + rel if rel else [] +# Bytes of file paths per git call once a path list has proved too long for +# one command. Windows caps a whole command line at 32,767 characters; +# elsewhere the limit is about 1 MB (macOS) or 2 MB (Linux), environment +# included. +_MAX_PATH_ARGS_BYTES = ( + 20000 if sys.platform in ("win32", "cygwin", "msys") else 500000) +# More commands than this and the path list is not worth diffing: each one +# matches its paths against the whole index, and a set that large is thrown +# away as pathological after the diff anyway. +_MAX_PATH_BATCHES = 4 + + +def _arg_cost(path): + # The exec limit also counts a terminator and a pointer per argument. + return len(os.fsencode(path)) + 9 + + +def _path_args_budget(env=None): + """_MAX_PATH_ARGS_BYTES, or less where this system's own limit is lower + (a small stack limit, a BSD, a very large environment). Half of what the + system leaves after the environment, so the rest of the command fits.""" + try: + arg_max = os.sysconf("SC_ARG_MAX") + except (AttributeError, ValueError, OSError): + return _MAX_PATH_ARGS_BYTES + env_bytes = sum(len(k) + len(v) + 2 for k, v in (env or os.environ).items()) + return min(_MAX_PATH_ARGS_BYTES, max(4096, (arg_max - env_bytes) // 2)) + + +def _path_batches(paths, budget=None): + if budget is None: + budget = _MAX_PATH_ARGS_BYTES + batch, size = [], 0 + for p in paths: + n = _arg_cost(p) + if batch and size + n > budget: + yield batch + batch, size = [], 0 + batch.append(p) + size += n + if batch: + yield batch + + +# Why a path list too long for one command left the last get_git_diff call +# without a diff, for the Stop review's skip reason: "gave_up" (still too +# long once cut down) or "none_reviewable". +_path_list_too_long = set() + + +def _too_many_args(e): + """The OS refused to start the command because its arguments are too + long. Windows reports ERROR_FILENAME_EXCED_RANGE, which Python turns into + a FileNotFoundError.""" + return e.errno == errno.E2BIG or getattr(e, "winerror", None) == 206 + + +def _run_captured(cmd, timeout, **kwargs): + return subprocess.run(cmd, capture_output=True, timeout=timeout, **kwargs) + + +def _run_git_on_review_paths(args, paths, timeout, run=_run_captured, **kwargs): + """Run `git <args> -- <paths>` for the Stop review; return the results. + `run(cmd, timeout, **kwargs)` starts each command (_run_diff_bounded for + the diff, so its output is never held whole). + + One command with every path whenever the OS takes it. When the OS refuses + the argument list as too long, only reviewable source paths are kept (the + review drops the rest from the diff anyway) and they go out in as few + commands as they need, all within `timeout` seconds together. If even + those need more than _MAX_PATH_BATCHES commands, the refusal is raised + again. + + Secret-shaped paths that are gone from the work tree go into every one + of those commands: `git diff` must still pair a rename away from such a + name, or the secret's content would show as a new source file. A new file + that only resembles the secret may be paired with it too and left out of + the review. Any other rename is paired only when both of its paths are + reviewable source and in the same command; otherwise it shows as a delete + or an add. + """ + cmd = [*GIT_CMD, *args, "--"] + paths = list(paths) + deadline = time.monotonic() + timeout + try: + return [run(cmd + paths, timeout, **kwargs)] + except OSError as e: + if not _too_many_args(e): + raise + too_long = e + budget = _path_args_budget(kwargs.get("env")) + batches = list(_path_batches( + [p for p in paths if _is_reviewable_source(p)], budget)) + root = kwargs.get("cwd") or os.getcwd() + secret = [p for p in paths if _is_secret_shaped(p) + and not os.path.lexists(os.path.join(root, p))] + debug_log(f"git {args[0]}: {len(paths)} paths do not fit one command; " + f"{len(batches)} command(s) for the reviewable ones") + # The secret-shaped paths ride along with every batch, so they get a + # small share of a command of their own. + if (len(batches) > _MAX_PATH_BATCHES + or sum(map(_arg_cost, secret)) > budget // 4): + raise too_long + cmd += secret + results = [] + for batch in batches: + left = deadline - time.monotonic() + if left <= 0: + raise subprocess.TimeoutExpired(cmd, timeout) + results.append(run(cmd + batch, left, **kwargs)) + return results + + _TEMP_INDEX_PREFIX = "security_hook_idx_" # A copy is named <prefix>pid<hook pid>_<8 random chars>, so a later run can # tell that the hook which made it is gone. Names from earlier releases have @@ -472,7 +587,7 @@ def _copied_index(cwd, real_index, tmpdir, untracked_paths): surviving = [p for p in untracked_paths if not p.endswith("/") and os.path.lexists(os.path.join(cwd, p))] - add_args = ["--"] + surviving if surviving else None + add_args = surviving or None else: add_args = None if add_args: @@ -480,11 +595,8 @@ def _copied_index(cwd, real_index, tmpdir, untracked_paths): # still spawns reader threads that decode stderr — git error # messages can reference non-ASCII filenames and crash on # cp1252. See #2099. Drop text=True so bytes stay raw. - subprocess.run( - [*GIT_CMD, "add", "--intent-to-add"] + add_args, - cwd=cwd, capture_output=True, timeout=10, - env=env, - ) + _run_git_on_review_paths(["add", "--intent-to-add"], add_args, + timeout=10, cwd=cwd, env=env) yield env finally: _remove_copy(tmp_index) @@ -1097,12 +1209,15 @@ def get_git_diff(cwd, baseline_sha, full_context=False, paths=None, untracked_pa If `paths` is given, the diff is restricted to those paths (relative to cwd; absolute paths are converted, paths outside cwd are dropped). + A path list too long for one command is cut down to reviewable source + files: see _run_git_on_review_paths. `untracked_paths` (repo-root-relative) is forwarded to _temp_index so it can add only those files instead of scanning the whole worktree. The output is read through _DiffKeeper: files the review would drop come back as their header line only, and a file's diff past MAX_FILE_DIFF_BYTES is cut down to its added lines. """ + _path_list_too_long.clear() pathspec = _diff_pathspec(cwd, paths) if paths and not pathspec: # Caller restricted to specific paths but none are inside this repo @@ -1112,25 +1227,36 @@ def get_git_diff(cwd, baseline_sha, full_context=False, paths=None, untracked_pa return "" # core.quotePath=false comes from GIT_CMD globally (see definition). - cmd = [*GIT_CMD, "diff", IGNORE_SUBMODULES, "--no-color", "--no-ext-diff", "--no-textconv", baseline_sha] + (["--unified=99999"] if full_context else []) + pathspec + args = ["diff", IGNORE_SUBMODULES, "--no-color", "--no-ext-diff", "--no-textconv", baseline_sha] + (["--unified=99999"] if full_context else []) _diff_read.update(bytes=0, cut_files=0, cut_bytes=0) + + def _run(env): + if pathspec: + results = _run_git_on_review_paths(args, pathspec[1:], timeout=30, run=_run_diff_bounded, cwd=cwd, env=env) + else: + results = [_run_diff_bounded([*GIT_CMD, *args], 30, cwd=cwd, env=env)] + if not results: + _path_list_too_long.add("none_reviewable") + for result in results: + if result.returncode != 0: + debug_log(f"git diff failed: {result.stderr[:200].decode('utf-8', errors='replace')}") + return None + # Decode with errors='replace' so binary diffs don't crash + return b"".join(r.stdout for r in results).decode("utf-8", errors="replace") + try: if untracked_paths is not None and not untracked_paths: # Nothing to `add -N`, and the diff only reads the index. - result = _run_diff_bounded(cmd, 30, cwd=cwd) - else: - with _temp_index(cwd, untracked_paths) as env: - # env is None when there is no index (bare repo / not a repo) - # or no copy slot — diff still runs, just without - # untracked-file support. - result = _run_diff_bounded(cmd, 30, cwd=cwd, env=env) - if result.returncode != 0: - debug_log(f"git diff failed: {result.stderr[:200].decode('utf-8', errors='replace')}") - return None - # Decode with errors='replace' so binary diffs don't crash - return result.stdout.decode("utf-8", errors="replace") + return _run(None) + with _temp_index(cwd, untracked_paths) as env: + # env is None when there is no index (bare repo / not a repo) + # or no copy slot — diff still runs, just without + # untracked-file support. + return _run(env) except (subprocess.TimeoutExpired, FileNotFoundError, OSError) as e: debug_log(f"git diff error: {e}") + if isinstance(e, OSError) and _too_many_args(e): + _path_list_too_long.add("gave_up") return None |
