Skip to content

Fix: shell-quote the install path in install_agy_hooks.py's shared hook command - #584

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/577
Sep 5, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/577

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #577.

build_remember_entry() interpolated the plugin's install path into a shell command line as bash "{command_path}", with hand-written double quotes as the only protection. A literal double quote in the install path broke the command outright (a shell syntax error), and a $(...) sequence in the path was still executed as a POSIX command substitution -- double quotes do not suppress that, only single quotes do.

plugin_root is always the user's own checkout path (os.path.abspath(os.path.dirname(__file__)/'..')), never remote-controlled, so this is a low-severity hardening fix rather than a real injection vector -- the audit that filed #577 says the same.

Fix: replace the whole construction with shlex.quote() applied to the entire command_path, then interpolated as bash {command_path} -- not nested inside the old bash "{...}" wrapper. Nesting is unsafe on its own: shlex.quote() switches to single-quoting whenever the path needs it, and a path containing a literal double quote produces a single-quoted string that still embeds that ", which breaks out of an outer pair of double quotes exactly the same way the original bug did. Quoting the whole argument, rather than only the interpolated segment, is what actually closes the hole.

Tests (TDD, red before the fix): three new tests in tests/test_install_agy_hooks_563.py actually invoke a real, correctly-resolved bash (via this repo's own _bash_runner.resolve_bash(), since shutil.which("bash") is documented unreliable on Windows) against a root containing a literal ", a root containing $(...), and an ordinary root as a positive control. Two pre-existing tests that parsed the emitted command by splitting on a literal " were migrated to shlex.split(), since the ordinary case is now emitted with no quotes at all (shlex.quote() leaves a metacharacter-free path unquoted).

Self-review (Explore + oss:auditor, against the first commit) found four issues, all fixed in a second commit, all in the new tests rather than in the fix itself: a Windows CRLF hazard in the marker-script helper (newline="" now used), the shutil.which("bash")/literal-"bash" unreliability just mentioned, a docstring left describing pre-diff test internals, and a comment overclaiming that POSIX quoting is safe for every step a Windows-hosted agy install might take before reaching bash (narrowed to REASONED, not observed, matching this file's existing Windows caveats).

below-bar item (not filed, recorded here per the audit bar): the reviewer also raised whether shlex.quote()'s single-quoting could itself be mis-tokenized by cmd.exe if some other, unconfirmed invocation path spawns this command line through cmd.exe before bash ever sees it, rather than exec'ing bash directly. This is a real, stateable concern but not a reachable one from anything this diff can observe: nothing in this codebase or its docs establishes that agy invokes hook commands via cmd.exe on Windows at all (Windows agy support is REASONED, not observed throughout this file already), so there is no input in hand that reaches the class -- it is a hypothesis about a mechanism nobody has confirmed exists, not a defect with a caller.

Changelog: changelog.d/577.fixed.md added and passes assemble_changelog.py --check. No README change needed -- internal hardening, no interface change.

[AI-generated]

fdaviddpt and others added 2 commits September 5, 2026 22:18
…and line (#577)

build_remember_entry() interpolated the plugin's install path into a
`bash "{path}"` shell command line with only hand-written double quotes.
A literal double quote in the path broke the command outright, and
double quotes still permit POSIX $(...) command substitution, so a path
containing one was executed rather than treated as literal.

Replace the whole construction with shlex.quote() applied to the entire
argument, rather than nesting shlex.quote()'s output inside the existing
double-quote wrapper -- shlex.quote() switches to single-quoting when the
path holds a double quote, and embedding that inside another pair of
double quotes reopens the exact same hole. Extend the existing test file
with real-bash execution controls for the quote and $(...) hazards (red
before the fix, green after) plus a positive control for an ordinary
path, and update the two tests that parsed the emitted command by
splitting on a literal '"' to use shlex.split() instead, since the
ordinary case is now emitted unquoted.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bv6BGZD2EdZ9EuJ7tPDTb7
Self-review (Explore + oss:auditor) on the #577 quoting commit found four
issues, all in the test file's own new subprocess-based controls, not in
the fix itself:

- The new tests wrote their marker script with `open(..., "w")` and no
  `newline=""`, so Python's default text-mode translation turns every
  `\n` into `\r\n` on Windows -- a stray CR right after a closing quote
  is not IFS whitespace to bash, so it corrupts the redirect target and
  the marker file is never created there. Fixed with `newline=""`.
- The new tests used `shutil.which("bash")`/literal `"bash"` to find and
  invoke a shell, which this repo's own tests/_bash_runner.py and
  test_hooks_json.py already document as unreliable on Windows (PATH's
  "bash" there commonly resolves to the WSL launcher, not Git Bash --
  the exact ambiguity #432 previously fixed for other test modules).
  Migrated to the shared `resolve_bash()` helper every sibling
  subprocess-bash test module already uses.
- `test_build_entry_command_survives_windows_shell_quoting`'s docstring
  still described the test above it as `command.split('"')[1]`, which
  that test no longer does after the #577 diff rewrote it to use
  `shlex.split()`. Updated to match.
- The comment justifying shlex.quote() as the right quoting model
  overstated its own confidence: it is only established that POSIX
  quoting is correct for bash's OWN parsing of the command string once
  bash receives it, not that a Windows-hosted agy install's own hook
  executor tokenizes the raw string with bash rather than something
  else (e.g. cmd.exe) before that point. Narrowed to say so explicitly
  and marked REASONED not observed, consistent with the rest of this
  file's Windows caveats.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bv6BGZD2EdZ9EuJ7tPDTb7
@fdaviddpt
fdaviddpt merged commit 30ffa49 into main Sep 5, 2026
16 checks passed
@fdaviddpt
fdaviddpt deleted the fix/577 branch September 5, 2026 21:10
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.

install_agy_hooks.py interpolates the plugin root path into a shell command line with only double-quote escaping (splices)

1 participant