Skip to content

fix(tests): stop argv mangling the #2126 shell-allowlist payloads on Windows - #2647

Closed
rajarshidattapy wants to merge 1 commit into
Graphify-Labs:v8from
rajarshidattapy:fix/shell-verdict-argv-mangling
Closed

rajarshidattapy wants to merge 1 commit into
Graphify-Labs:v8from
rajarshidattapy:fix/shell-verdict-argv-mangling

Conversation

@rajarshidattapy

Copy link
Copy Markdown
Contributor

Fixes #2641.

The bug

tests/test_hooks.py guards the post-commit hook's interpreter allowlist — the #2126 fix,
and the thing that stops ;, backticks and $(...) reaching a shell. The harness passed the
glob and the candidate to bash as argv elements. On Windows there is no execve, so
subprocess joins argv into one command string and bash.exe's MSYS2 runtime re-parses it,
which rewrites the values before the case statement ever runs.

Both directions were broken, which is why it went unnoticed:

The shipped hook is correct; this is purely a test-harness defect. Confirmed independently.

The issue's suggested fix is not sufficient

Writing the pattern to a script file fixes half of it, but the candidate still goes
through argv, and it is mangled just as badly:

'foo`id`'                             -> 'foouid=1000(asus)'   # substitution EXECUTED
'foo$(id)'                            -> 'foouid=1000(asus)'   # executed
'foo$IFS'                             -> 'foo'                 # expanded
'C:\Users\u\.venv\Scripts\python.exe' -> 'C:Usersu.venvScriptspython.exe'

So under that fix: foo$IFS arrives as foo, which the allowlist correctly ACCEPTS, so
that parametrization keeps failing; and the backslash path arrives with no backslashes, so
both #2126 accept tests stay vacuous — the very failure being fixed. (The harness was also
executing id on every run, since MSYS performs the substitution.)

Fix

Bake both the pattern and the candidate into the script file, the candidate as a
single-quoted shell literal, and invoke bash case_test.sh by bare filename from cwd — an
absolute Windows path in argv hits the same mangling the script contents just avoided. Nothing
under test transits argv.

This also models production more closely than argv did: the hook is a script file, and the
value being checked is already sitting in a shell variable by the time the allowlist runs.

Tests

  • _assert_harness_can_reject — the issue's suggested control, wired into both accept tests.
    An ACCEPTED verdict only means something if the pattern can say REJECTED.
  • test_shell_verdict_delivers_the_payload_bash_unmodified — pins the root cause rather than
    the symptom, asserting bash receives the exact bytes. A future regression then names the
    transport instead of looking like an allowlist bug.
  • _shell_verdict now asserts the verdict is one of ACCEPTED/REJECTED. As the issue notes,
    returncode == 0 cannot catch mangling; an empty stdout used to be compared against silently.

Verifying the guard is no longer vacuous

Pass/fail counts alone don't show this, so I fed the harness a deliberately broken allowlist —
the pre-#2126 pattern with no backslash (*[!a-zA-Z0-9/_.@-]*), against
C:\Users\u\.venv\Scripts\python.exe, which the test asserts must be ACCEPTED:

shipped allowlist    old harness -> ACCEPTED  pass   |  new harness -> ACCEPTED  pass
BROKEN  allowlist    old harness -> ACCEPTED  pass   |  new harness -> REJECTED  FAIL

The old harness cannot tell a working allowlist from a broken one. The new one also fails the
accept test against the naive :-to-\ range fix that the reject test was written to catch,
so on Windows that regression is now caught from the accept side even where the reject side
passes.

Full suite: 42 pre-existing failures → 38. The four removed are exactly the reported ones; no
new failures.

Note on the mechanism

list2cmdline isn't really the culprit. Its output preserves the pattern; the rewriting
happens when bash.exe's MSYS2 runtime re-parses the joined command string, unescaping
backslashes, expanding variables and running command substitution. MSYS2_ARG_CONV_EXCL does
not suppress it — verified — which is why keeping the payloads out of argv entirely is the
only reliable option.

@graphify-labs graphify-labs Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Looks safe to merge — no coupling regressions and no blocking issues, checked against the code graph (not a self-assessment).


Graphify review — findings

This PR reworks the bash-based test harness in tests/test_hooks.py that exercises the emitted shell allowlist patterns. It replaces the previous approach of passing the pattern and candidate value through bash -c argv with a new helper (_shell_verdict) that writes both into a script file and invokes it by bare filename, along with a _sh_single_quote quoting helper. The intent, per the comments, is to avoid Windows argv mangling so the value under test reaches bash intact. It also adds guard assertions (_assert_harness_can_reject) to the existing accept-path tests and a new test (test_shell_verdict_delivers_the_payload_bash_unmodified) asserting the payload is delivered to bash unmodified. Affected test functions were updated to thread the tmp_path fixture through to the harness. Surface area is confined to the test file; no production/source code is changed.

No blocking issues surfaced.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 115 functions depend on the 115 functions this change touches.

Health — grade A; no new coupling hotspots.

Verification — 115 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 115 function(s) in the blast radius were not formally verified this run

@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.42 (graphifyy==0.9.42 on PyPI) (#2126). Stops argv mangling the shell-arg verdict test on Windows. Credited in the release notes. Thanks @rajarshidattapy!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants