diff options
| author | Mohamed Hegazy <8000722+mhegazy@users.noreply.github.com> | 2026-10-15 14:54:34 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-10-15 14:54:34 -0700 |
| commit | 4e0888ee4bd63c231ce722a60a9079182eea43d5 (patch) | |
| tree | cb0cc91d85cc07c67aca7835132218bc7eb39e4a /plugins | |
| parent | 49a4d974b935e20dc2b9a49a26ab175d37a01aa7 (diff) | |
| parent | 4aa979897a448b5f298ec7b183503daadd0c3783 (diff) | |
Merge pull request #6619 from anthropics/mhegazy/sg-git-valueless-global-options
security-guidance: recognize more git global options before commit and push
Diffstat (limited to 'plugins')
| -rwxr-xr-x | plugins/security-guidance/hooks/security_reminder_hook.py | 28 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_repo_resolution.py | 19 |
2 files changed, 34 insertions, 13 deletions
diff --git a/plugins/security-guidance/hooks/security_reminder_hook.py b/plugins/security-guidance/hooks/security_reminder_hook.py index b7eb0966..1dcb865c 100755 --- a/plugins/security-guidance/hooks/security_reminder_hook.py +++ b/plugins/security-guidance/hooks/security_reminder_hook.py @@ -665,15 +665,26 @@ _COMMIT_SHA_RE = re.compile(r'^\[[^\]]*?\b([0-9a-f]{7,40})\]', re.MULTILINE) # semantics); `gt modify` amends the current commit (mapped to git commit # --amend, also flagged by _GIT_AMEND_RE below). hooks.json has a # `Bash(gt *)` entry so the hook subprocess spawns for gt invocations. +# +# Global options allowed between `git` and the subcommand, shared by +# _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"']|"[^"]*"|'[^']*')+''' +_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 + # `--help` / `--version` run no subcommand. + + r'|\s+(?:--(?!(?:' + _GIT_VALUE_OPTS + r'|help|version)\s)[^\s=]+|-[pP])(?=\s))*' +) _GIT_COMMIT_RE = re.compile( - # `git -C <path>` and `git -c key=val` global options are allowed between - # `git` and `commit` (mirrors the long-standing tolerance in - # _GIT_PUSH_RE). Without this, `git -C /repo commit` is silently dropped - # by the handler — see #2089's secondary finding. The gt branch has no - # global-option layer to worry about. - r'\bgit(?:\s+-[Cc]\s+(?:"[^"]*"|\'[^\']*\'|[^\s"\']\S*)|\s+--[^\s=]+=\S+' + # Without the global-option tolerance, `git -C /repo commit` is silently + # 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'|\s+--(?:git-dir|work-tree)\s+\S+)*\s+commit\b(?!-)' + r'\bgit' + _GIT_GLOBAL_OPTS + r'\s+commit\b(?!-)' r'|\bgt\s+(?:create|modify)\b' ) # Match either the `--amend` flag (with the leading whitespace boundary @@ -714,8 +725,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'(?:\bgit(?:\s+-[cC]\s+(?:"[^"]*"|\'[^\']*\'|[^\s"\']\S*)|\s+--[^\s=]+=\S+' - r'|\s+--(?:git-dir|work-tree)\s+\S+)*\s+push\b|\bgt\s+submit\b)' + r'(?:\bgit' + _GIT_GLOBAL_OPTS + r'\s+push\b|\bgt\s+submit\b)' ) # `git push` stdout: "abc1234..def5678 branch -> branch" (or `+abc..def` on diff --git a/plugins/security-guidance/tests/test_repo_resolution.py b/plugins/security-guidance/tests/test_repo_resolution.py index 60288029..30c94a7c 100644 --- a/plugins/security-guidance/tests/test_repo_resolution.py +++ b/plugins/security-guidance/tests/test_repo_resolution.py @@ -191,6 +191,10 @@ class TestRegexes: "git -c core.editor=true -C sub commit --amend", "git --git-dir=x/.git --work-tree=x commit -m x", "git --git-dir x/.git commit -m x", + "git --no-pager commit -m x", + "git -p --no-optional-locks -c user.name=x --bare commit -m x", + 'git -c user.name="First Last" -c user.email=a@b.c commit -S -m x', + "git --git-dir='a b/.git' --namespace ns commit -m x", "cd sub && git commit -m x", "gt create -m x", ]) @@ -199,22 +203,29 @@ class TestRegexes: @pytest.mark.parametrize("cmd", [ "git push", "git -C sub push origin main", 'git -C "a b" push', - "git --work-tree x push -u origin HEAD", "gt submit", + "git --work-tree x push -u origin HEAD", "git --no-pager push", + 'git -c http.extraHeader="X-A: b" push', "git --config-env http.extraheader=TOK push", "gt submit", ]) def test_push_re(self, cmd): assert hook._GIT_PUSH_RE.search(cmd) @pytest.mark.parametrize("cmd", ["git status", "git log --oneline", "echo commit", "git commit-graph write --reachable", "git commit-tree HEAD^{tree}", - "git log --grep commit", "git stash push"]) + "git log --grep commit", "git stash push", + "git log --oneline commit", "git --no-pager log --grep commit", + "git merge --no-commit topic", "git --git-dir commit status", + "git --namespace commit log", "git --help commit", + 'git -c user.name="commit status']) def test_commit_re_negative(self, cmd): assert not hook._GIT_COMMIT_RE.search(cmd) - @pytest.mark.parametrize("cmd", ["git stash push", "git log --grep push", "git commit-graph write"]) + @pytest.mark.parametrize("cmd", ["git stash push", "git log --grep push", "git commit-graph write", + "git --no-pager stash push", "git --no-pager log push"]) def test_push_re_negative(self, cmd): assert not hook._GIT_PUSH_RE.search(cmd) - @pytest.mark.parametrize("unit", ['-c "a"', "-c 'a'", "--no-a=b", "--a=b=c=d", "--git-dir x"]) + @pytest.mark.parametrize("unit", ['-c "a"', "-c 'a'", "--no-a=b", "--a=b=c=d", "--git-dir x", + "--no-pager", "-p", '-c a="b c"', "--namespace x"]) @pytest.mark.parametrize("regex,verb", [("_GIT_COMMIT_RE", "commit"), ("_GIT_PUSH_RE", "push")]) def test_global_option_prefix_is_linear(self, regex, verb, unit): cre = getattr(hook, regex) |
