Skip to content

The #2126 shell-allowlist regression tests pass vacuously on Windows — _shell_verdict mangles the glob before bash ever sees it #2641

Closed

Description

@rajarshidattapy

Labels: bug, tests, windows, security-testing
Version: 0.9.40
Environment: Windows 11, Git Bash 5.2.37, Python 3.13.6

Summary

tests/test_hooks.py guards the post-commit hook's interpreter allowlist — the fix for
#2126, and the guard that stops ;, backticks and $(...) reaching a shell. On Windows the
helper that runs the glob passes the pattern through subprocess as an argv element, and
Windows argv quoting rewrites the backslashes inside the bracket expression. bash receives a
different pattern, one that matches nothing.

Consequences, both bad in opposite directions:

The shipped hook is correct. I verified it separately. This is purely a test-harness
defect — but one that disables a security regression test on the affected platform.

Steps to reproduce

$ pytest tests/test_hooks.py -q -k allowlist
FAILED tests/test_hooks.py::test_python_detect_allowlists_still_reject_shell_metacharacters[foo;rm -rf /]
FAILED tests/test_hooks.py::test_python_detect_allowlists_still_reject_shell_metacharacters[foo`id`]
FAILED tests/test_hooks.py::test_python_detect_allowlists_still_reject_shell_metacharacters[foo$(id)]
FAILED tests/test_hooks.py::test_python_detect_allowlists_still_reject_shell_metacharacters[foo$IFS]
E   AssertionError: _FROM_FILE="" allowlist wrongly accepted dangerous input 'foo;rm -rf /'

Demonstrating that the harness, not the hook, is wrong

Through _shell_verdict's subprocess.run([...]) path, every input is ACCEPTED — even
/usr/bin/python3, which the allowlist must accept, and foo;rm -rf /, which it must
reject. A pattern that never matches anything:

'foo;rm -rf /'                        -> ACCEPTED
'foo`id`'                             -> ACCEPTED
'foo$(id)'                            -> ACCEPTED
'C:\Users\u\.venv\Scripts\python.exe' -> ACCEPTED
'/usr/bin/python3'                    -> ACCEPTED

Writing the same case arm to a .sh file and running bash case_test.sh <input> — which
is how the hook actually executes — gives the correct answers:

$ cat case_test.sh
case "$1" in
  *[!a-zA-Z0-9/_.@:\\-]*) echo REJECTED ;;
  *) echo ACCEPTED ;;
esac

foo;rm -rf /                          REJECTED
foo`id`                               REJECTED
foo$(id)                              REJECTED
foo$IFS                               REJECTED
C:\Users\u\.venv\Scripts\python.exe   ACCEPTED
/usr/bin/python3                      ACCEPTED
C:/Users/u/python.exe                 ACCEPTED

Both the #2126 property (Windows paths accepted) and the injection property (metacharacters
rejected) hold.

Root cause

tests/test_hooks.py:463-473:

def _shell_verdict(pattern: str, candidate: str) -> str:
    result = subprocess.run(
        ["bash", "-c", f'case "$1" in\n{pattern}) echo REJECTED ;;\n*) echo ACCEPTED ;;\nesac', "_", candidate],
        capture_output=True, text=True,
    )

On Windows, subprocess has no execve; the argument list is joined into a single command
string by subprocess.list2cmdline, whose rules double backslashes that precede a quote and
otherwise pass them through. The pattern contains \\ inside a bracket expression, so what
bash.exe parses is not what was passed. assert result.returncode == 0 does not catch it,
because a mangled-but-still-valid case arm exits 0.

Suggested fix

Write the snippet to a file and invoke bash script.sh <candidate> — no shell-metacharacter
payload transits argv:

def _shell_verdict(pattern: str, candidate: str, tmp_path) -> str:
    script = tmp_path / "case_test.sh"
    script.write_text(
        f'case "$1" in\n  {pattern}) echo REJECTED ;;\n  *) echo ACCEPTED ;;\nesac\n',
        encoding="utf-8", newline="\n",
    )
    result = subprocess.run(["bash", str(script), candidate],
                            capture_output=True, text=True)
    assert result.returncode == 0, result.stderr
    return result.stdout.strip()

This also matches how the hook runs in production (a script file read by bash), so the test
exercises the real code path.

To stop the vacuous-pass failure mode returning, add a control assertion to the accept tests:

# sanity: the pattern must actually be capable of rejecting something,
# otherwise an ACCEPTED verdict proves nothing
assert _shell_verdict(pattern, "foo;rm -rf /", tmp_path) == "REJECTED"

Related

Activity

  1. ayushcodes10 commented on Sep 14, 2026

    @ayushcodes10
    Contributor

    This is already fixed — shipped in v0.9.42 via commit 243f3e3 ("test(hooks): stop argv mangling the shell-arg verdict test on Windows (#2126)"), closed via PR #2647 (thanks @rajarshidattapy). Confirmed the fix is present on current v8. Closing this out since the acceptance criteria are already met; let me know if you'd like it reopened for any remaining edge case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions