diff options
| author | Claude <noreply@anthropic.com> | 2026-11-12 23:57:03 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-11-13 00:11:48 +0000 |
| commit | 812ea6bfaa2b99c87f90de52e0cab2a9db839873 (patch) | |
| tree | 9b9edbf371a9e57a72fe905e0ca7429df6e5c814 /plugins | |
| parent | f75f5b25ae94f4332be4ff5f18435741d2589bdb (diff) | |
security-guidance: match git commit and push continued on the next linemhegazy/sg-line-continuation
`git \` with `commit` or `push` on the next line started the hook, but the
command patterns only allowed whitespace between the words, so nothing was
reviewed. They now also allow a line continued with a backslash (Bash) or a
backtick (PowerShell).
Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins')
3 files changed, 97 insertions, 11 deletions
diff --git a/plugins/security-guidance/hooks/security_reminder_hook.py b/plugins/security-guidance/hooks/security_reminder_hook.py index fcd20a7f..160a633a 100755 --- a/plugins/security-guidance/hooks/security_reminder_hook.py +++ b/plugins/security-guidance/hooks/security_reminder_hook.py @@ -1097,14 +1097,17 @@ _COMMIT_SHA_RE = re.compile(r'^\[[^\]]*?\b([0-9a-f]{7,40})\]', re.MULTILINE) # _GIT_COMMIT_RE and _GIT_PUSH_RE: `-C <path>`, `-c key=val`, `--opt=val`, # the long options that take a separate value, and flags that take none # (`--no-pager`, `-p`). A value may be quoted in part (`-c user.name="A B"`). -_GIT_OPT_VALUE = r'''(?:[^\s"']|"[^"]*"|'[^']*')+''' +# Between the words: whitespace, or a line continued with a backslash (Bash) or +# a backtick (PowerShell). +_WORD_GAP = r'(?:\s|[\\`]\r?\n)+' +_GIT_OPT_VALUE = r'''(?:(?![\\`]\r?\n)[^\s"']|"[^"]*"|'[^']*')+''' _GIT_VALUE_OPTS = r'git-dir|work-tree|namespace|config-env|attr-source' _GIT_GLOBAL_OPTS = ( - r'(?:\s+-[Cc]\s+' + _GIT_OPT_VALUE - + r'|\s+--[^\s=]+=' + _GIT_OPT_VALUE - + r'|\s+--(?:' + _GIT_VALUE_OPTS + r')\s+' + _GIT_OPT_VALUE + r'(?:' + _WORD_GAP + r'-[Cc]' + _WORD_GAP + _GIT_OPT_VALUE + + r'|' + _WORD_GAP + r'--[^\s=]+=' + _GIT_OPT_VALUE + + r'|' + _WORD_GAP + r'--(?:' + _GIT_VALUE_OPTS + r')' + _WORD_GAP + _GIT_OPT_VALUE # `--help` / `--version` run no subcommand. - + r'|\s+(?:--(?!(?:' + _GIT_VALUE_OPTS + r'|help|version)\s)[^\s=]+|-[pP])(?=\s))*' + + r'|' + _WORD_GAP + r'(?:--(?!(?:' + _GIT_VALUE_OPTS + r'|help|version)\s)[^\s=]+|-[pP])(?=\s))*' ) # PowerShell runs programs by any case and with their extension, and a quoted # full path ends in a quote. @@ -1114,15 +1117,15 @@ _GIT_COMMIT_RE = re.compile( # dropped by the handler — see #2089's secondary finding. The gt branch # has no global-option layer to worry about. # `(?!-)`: not the plumbing commands `git commit-graph` / `git commit-tree`. - r'\b' + _GIT_PROGRAM + _GIT_GLOBAL_OPTS + r'\s+commit\b(?!-)' - r'|\bgt\s+(?:create|modify)\b' + r'\b' + _GIT_PROGRAM + _GIT_GLOBAL_OPTS + _WORD_GAP + r'commit\b(?!-)' + r'|\bgt' + _WORD_GAP + r'(?:create|modify)\b' ) # Match either the `--amend` flag (with the leading whitespace boundary # preserved from the original) OR `gt modify` which is semantically an # amend. The handler treats matches as "find the pre-amend SHA via reflog # and diff against THAT, not against the post-amend HEAD's parent" — same # code path for both git --amend and gt modify. -_GIT_AMEND_RE = re.compile(r'(?:\s--amend\b|\bgt\s+modify\b)') +_GIT_AMEND_RE = re.compile(r'(?:\s--amend\b|\bgt' + _WORD_GAP + r'modify\b)') # Rolling-window cap on LLM commit-review calls. See atomic_check_rate_limit # docstring for the rationale that motivated the switch from a lifetime cap. @@ -1155,7 +1158,7 @@ COMMIT_REVIEW_RATE_WINDOW_S = int( # but the bash hook fires on Claude's top-level command so we need to # recognize gt submit at the matcher level. See #2048. _GIT_PUSH_RE = re.compile( - r'(?:\b' + _GIT_PROGRAM + _GIT_GLOBAL_OPTS + r'\s+push\b|\bgt\s+submit\b)' + r'(?:\b' + _GIT_PROGRAM + _GIT_GLOBAL_OPTS + _WORD_GAP + r'push\b|\bgt' + _WORD_GAP + r'submit\b)' ) # `git push` stdout: "abc1234..def5678 branch -> branch" (or `+abc..def` on diff --git a/plugins/security-guidance/tests/test_line_continuation.py b/plugins/security-guidance/tests/test_line_continuation.py new file mode 100644 index 00000000..794fa789 --- /dev/null +++ b/plugins/security-guidance/tests/test_line_continuation.py @@ -0,0 +1,81 @@ +"""A git command continued on the next line is still a commit or a push.""" +import time + +import pytest + +from conftest import bash_payload, commit_file, git, make_repo, metrics_of, run_hook + +import security_reminder_hook as hook +from diffstate import _load_reviewed_shas + + +@pytest.mark.parametrize("command", [ + "git \\\n commit -m x", + "git \\\r\n commit -m x", + "git `\r\n commit -m x", + "git -C \\\n . commit", + "git \\\n -c user.name='A B' \\\n --no-pager \\\n commit -m x", + "git --git-dir \\\n .git commit", + "gt \\\n create x", + "# kept in C:\\temp\\\ngit commit -m x", + "git commit \\\n -m x", +]) +def test_is_a_commit(command): + assert hook._GIT_COMMIT_RE.search(command) + + +@pytest.mark.parametrize("command", [ + "git \\\n push origin main", "git -C x `\n push", "gt \\\n submit", "echo a\\\ngit push", +]) +def test_is_a_push(command): + assert hook._GIT_PUSH_RE.search(command) + + +@pytest.mark.parametrize("command", [ + "git log \\\n commit", "git \\ commit", "git \\\n --help commit", "git \\\n commit-graph write", + "git log `date`\ncommit", "legit\\\ncommit", +]) +def test_is_neither(command): + assert not hook._GIT_COMMIT_RE.search(command) + assert not hook._GIT_PUSH_RE.search(command) + + +def test_continued_gt_modify_is_an_amend(): + assert hook._GIT_AMEND_RE.search("gt \\\n modify -m x") + + +def test_long_command_is_matched_quickly(): + for part in (" \\\n", " -c \\\n", " -c a=b \\\n", " --x \\\n", "\\\n", " `\n-p"): + start = time.monotonic() + hook._GIT_COMMIT_RE.search("git" + part * 3000 + " x") + assert time.monotonic() - start < 2 + + +@pytest.mark.parametrize("tool, command", [ + ("Bash", "git \\\n commit -m change"), + ("Bash", "git -c core.abbrev=12 \\\n commit -m change"), + ("PowerShell", "git `\n commit -m change"), + ("Bash", "cd .. && git -C \\\n repo \\\n commit -m change"), +]) +def test_continued_commit_is_reviewed(tmp_path, hook_env, stub_api, tool, command): + repo = make_repo(tmp_path / "repo") + sha, out = commit_file(repo, "a.py", "print('a')\n") + payload = {**bash_payload(str(repo), command, out), "tool_name": tool} + _, so, _ = run_hook(payload, hook_env) + assert metrics_of(so).get("commit_review") is True + assert sha in _load_reviewed_shas(str(repo)) + + +def test_continued_push_is_reviewed(tmp_path, hook_env, stub_api): + repo = make_repo(tmp_path / "repo") + remote = tmp_path / "remote.git" + git(tmp_path, "init", "-q", "--bare", str(remote)) + git(repo, "remote", "add", "origin", str(remote)) + git(repo, "push", "-q", "-u", "origin", "main") + base = git(repo, "rev-parse", "HEAD").strip() + sha, _ = commit_file(repo, "a.py", "print('a')\n") + git(repo, "push", "-q", "origin", "main") + out = f"To {remote}\n {base[:7]}..{sha[:7]} main -> main\n" + _, so, _ = run_hook(bash_payload(str(repo), "git \\\n push origin main", out), hook_env) + assert metrics_of(so).get("push_sweep") is True + assert sha in _load_reviewed_shas(str(repo)) diff --git a/plugins/security-guidance/tests/test_unmatched_bash_metric.py b/plugins/security-guidance/tests/test_unmatched_bash_metric.py index 8933bbd1..9c49f6c4 100644 --- a/plugins/security-guidance/tests/test_unmatched_bash_metric.py +++ b/plugins/security-guidance/tests/test_unmatched_bash_metric.py @@ -66,8 +66,10 @@ def test_one_call_in_ten_is_sampled(): def test_pairs_cover_every_verb_in_the_regexes(): # A verb added to either regex has to be added to _SHELL_PRECHECK_PAIRS # and to _commands() below. - assert hook._GIT_COMMIT_RE.pattern.endswith(r"\s+commit\b(?!-)|\bgt\s+(?:create|modify)\b") - assert hook._GIT_PUSH_RE.pattern.endswith(r"\s+push\b|\bgt\s+submit\b)") + assert hook._GIT_COMMIT_RE.pattern.endswith( + rf"{hook._WORD_GAP}commit\b(?!-)|\bgt{hook._WORD_GAP}(?:create|modify)\b") + assert hook._GIT_PUSH_RE.pattern.endswith( + rf"{hook._WORD_GAP}push\b|\bgt{hook._WORD_GAP}submit\b)") def _commands(): |
