Skip to content

Harden agy-stop-hook.sh against argv flag injection and field-shift/CR bugs (#576, #578, #579) - #585

Merged
fdaviddpt merged 4 commits into
mainfrom
fix/576
Sep 5, 2026
Merged

fdaviddpt merged 4 commits into
mainfrom
fix/576

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Fixes three related hardening findings on the same four lines of scripts/agy-stop-hook.sh, an Antigravity (agy) CLI Stop-hook adapter. Closes #576, closes #578, closes #579.

#576: conversationId reaches save-session.sh as an unsanitised argv[1]

save-session.sh's own arg-parsing loop (for arg in "$@") treats a literal --force or --dry as a FLAG regardless of where it came from -- its session-id shape check only ever sees whatever landed in the positional slot, never the raw argv. A conversationId of exactly --force would silently flip FORCE=true, bypassing save-session.sh's cooldown timer and minimum-human-message gate. Confirmed by reading save-session.sh's own arg loop, not assumed -- and this is the concrete route the auditor who filed #576 said they had not established.

The sibling hooks' existing character-class guard (session-end-hook.sh, post-tool-hook.sh, session-start-hook.sh: ''|.|..|*[!A-Za-z0-9._-]*) does not close this on its own: --force/--dry consist entirely of characters that guard already allows. Extended here with a leading-dash rejection (-*) so nothing shaped like an option can reach that argv position.

#578: the 4-field stdout protocol has no escaping

A python extractor prints 4 fields (conversationId, transcriptPath, workspacePath, parse status) as 4 lines; the shell re-reads them positionally via sed -n Np. An embedded newline in any one field would shift every field after it. Fixed by rejecting (blanking) a field containing an embedded newline or carriage return at the point of entry, in the python extractor itself -- matching the convention session-end-hook.sh and post-tool-hook.sh already apply to their own stdin-sourced fields.

#579: a trailing CR on Windows/Git-Bash (reasoned, not observed live -- no Windows runner was available)

python3 writes CRLF line endings there, and command substitution only strips a trailing LF from the whole captured stream, not an internal line's own CR -- so every extracted field could carry a trailing CR. Stripped unconditionally from each of the four extracted shell variables.

Self-review

Two reviewers ran against the committed diff: Explore returned NO FINDINGS after independently tracing all three mechanisms and confirming the new test's ability to isolate #578 from #576. oss:auditor returned 3 findings:

  1. Blanking a field for shape/embedded-newline reasons is indistinguishable from a genuinely absent field at the hook's only exit path (no stderr line, same parse status). This mirrors an existing, unlogged pattern already present in session-end-hook.sh, ranked non-blocking by the auditor (misreports, can ship behind a filed issue). Below-bar for this issue's own scope: fixing it well needs a new diagnostic axis applied consistently across at least three sibling hook files, a wider blast radius than this diff. Below-bar receipt: fixing the ambiguity between a shape-rejected field and a genuinely absent one is out of this diff's scope and mirrors an existing, unlogged pattern already in session-end-hook.sh.
  2. The originally-committed agy-stop-hook.sh's field protocol likely leaves a trailing \r on Windows/Git-Bash, misrouting the Stop hook to the wrong session (fails-to-preserve, not covered by CI) #579 unit test never invoked the real script, so it would still pass if the fix were reverted.
  3. The whole new test module skips on win32 (same reason every bash-hook test in this repo does), so the agy-stop-hook.sh's field protocol likely leaves a trailing \r on Windows/Git-Bash, misrouting the Stop hook to the wrong session (fails-to-preserve, not covered by CI) #579 fix had zero CI exercise on any platform.

Findings 2 and 3 are fixed in a follow-up commit: a new integration test stubs a CRLF-translating python3 (same technique tests/test_config_dir_normalisation.py already uses for a different Windows-only branch) and runs the real hook against it on POSIX CI. Verified genuinely red against the pre-fix hook and green against the fix.

Tests

New file tests/test_agy_stop_hook_576_578_579.py, 11 tests, each must-fire case paired with a must-not-fire/control case per the brief's TDD requirements. All pre-existing sibling tests (test_agy_hooks_563.py, test_agy_hooks_malformed_payload_568.py) still pass.

Docs

Internal hardening fix with no interface/behavior change visible to a user; README.md does not describe the internal protocol this touches, so no README change. changelog.d/576.fixed.md added.

🤖 Generated with Claude Code

[AI-generated]

fdaviddpt and others added 2 commits September 5, 2026 22:17
…R bugs (#576, #578, #579)

_CONVERSATION_ID reached save-session.sh as argv[1] with no shape
requirement of its own. save-session.sh's own arg loop reads a literal
"--force"/"--dry" as a FLAG regardless of where it came from -- a
conversationId shaped like one of those strings would silently bypass the
cooldown and minimum-human-message gates. The sibling hooks' existing
character-class guard does not close this on its own (those strings consist
entirely of characters it already allows), so the guard here is extended
with a leading-dash rejection.

The four-field newline-delimited stdout protocol between the python
extractor and the shell (`sed -n Np` per field) had no protection against
an embedded newline in one field shifting every field after it. Each field
is now rejected at the point of entry (python side) if it carries an
embedded \n or \r, matching the convention session-end-hook.sh and
post-tool-hook.sh already apply to their own stdin-sourced fields.

A trailing \r from a CRLF-writing python3 on Windows/Git-Bash (reasoned,
not observed live) could otherwise survive into an extracted field, since
command substitution only strips a trailing newline from the whole captured
stream, not an internal line's own \r. Stripped unconditionally from each
extracted field.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bv6BGZD2EdZ9EuJ7tPDTb7
The two #579 tests committed with the fix reimplemented the fix's own
`${VAR%$'\r'}` line by hand rather than exercising it, and the whole test
module already skips on win32 for unrelated reasons (bash-subprocess
tests are not portable to Windows runners) -- so the actual #579 fix had
zero exercise from any CI leg, on any platform, against the real script.

Adds an integration test using the same technique
tests/test_config_dir_normalisation.py already established for a
different Windows-only branch: stub the platform-specific piece (here, a
python3 wrapper that translates LF to CRLF, mimicking python3's own line
endings on native Windows) and run the real hook against it on POSIX CI.

Caught along the way: the first version of this test used
Path.read_text() on the fake save-session.sh's own marker file, whose
default universal-newline translation silently rewrites CRLF to LF on
read -- making the very CR the test exists to catch invisible to the
assertion. Confirmed by running the test against the pre-fix hook
(git show HEAD~1:scripts/agy-stop-hook.sh): it passed even though `od -c`
on the same marker file showed a real 0x0D byte. Fixed with
read_text(newline="").

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bv6BGZD2EdZ9EuJ7tPDTb7
fdaviddpt and others added 2 commits September 5, 2026 22:55
…triage.md (#497)

The new bash-subprocess test module added for #576/#578/#579 blanket-skips
on win32 like its sibling agy-hook test modules, which tests/test_windows_skip_triage_497.py
requires to be listed in docs/windows-skip-triage.md -- CI on PR #585 caught
the omission (pytest windows-latest legs failing on this guard).

Verdict: unclear, same templated reason and same modelling relationship as
test_agy_hooks_563.py/test_agy_hooks_malformed_payload_568.py.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bv6BGZD2EdZ9EuJ7tPDTb7
… in #579 test

Path.read_text() only gained a `newline` keyword argument in Python 3.13
(a pathlib addition PEP-managed for that release) -- on every earlier
interpreter this repo's own CI runs (3.10/3.11/3.12), read_text(newline="")
raises TypeError: got an unexpected keyword argument 'newline'. Passed
locally (3.13.15) and failed on CI (pytest ubuntu-latest, 3.11), a second
and distinct fault from the earlier windows-latest triage-doc failure.

read_bytes().decode() sidesteps the whole question: bytes.decode() never
performs newline translation regardless of interpreter version, so it
needs no version-gated keyword to disable it -- it is the same fix in
substance as newline="", implemented with an API every supported
interpreter has always had.

Re-verified red/green: genuinely fails against the pre-fix hook
(git show f8a99b7:scripts/agy-stop-hook.sh, this repo's release commit
immediately before #576/#578/#579 landed) with a real AssertionError
showing the actual CR byte, and passes against the fix. Full narrowed
suite (tests/test_agy_stop_hook_576_578_579.py,
tests/test_agy_hooks_563.py, tests/test_agy_hooks_malformed_payload_568.py,
tests/test_windows_skip_triage_497.py): 28 passed.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bv6BGZD2EdZ9EuJ7tPDTb7
@fdaviddpt
fdaviddpt merged commit a07f08e into main Sep 5, 2026
16 checks passed
@fdaviddpt
fdaviddpt deleted the fix/576 branch September 5, 2026 21:39
fdaviddpt added a commit that referenced this pull request Sep 6, 2026
…591)

* Add authoring-time reminder for a new blanket win32-skip test (#589)

#585 and #588 each hit the same gap in one session: a new module-level
`pytestmark = pytest.mark.skipif(sys.platform == "win32", ...)` needs a
`docs/windows-skip-triage.md` row in the same commit, and both times this
was caught only well into a CI run rather than at authoring time.

Add a `.claude/jit-context` tools-dimension entry
(`tools/00-manual/win32-skip-triage-entry.md`) that fires on a `supertool`
Bash call whose command text contains the `pytestmark = pytest.mark.skipif`
pattern -- this repo's own write path always carries a new file's content
inside the Bash command string (`paste:@-`/`edit:@-` heredocs), so a
content-shaped regex can see it even though jit-context deliberately never
inspects a native Edit/Write tool_input.content. Proven to fire on a
matching payload and stay silent on a non-matching one (both checked
directly against pre-tool-hook.sh).

State the same convention in `CONTRIBUTING.md` and in
`docs/windows-skip-triage.md`'s own header, as a backstop for a contributor
whose write does not go through `supertool` -- the one path the jit-context
rule cannot see into.

Audited tests/test_path_resolution.py for other instances of the issue's
second half (a docstring/comment promising an exclusion the code does not
implement, with no positive-control test proving it fires): the
`command -v jq` case is already fixed (#574/#588 follow-up); no other
instance found. Every other per-line exclusion in that file is a plain,
correctly-implemented containment check or a whole-file omission, not a
substring pattern that can quietly amnesty a real violation.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bv6BGZD2EdZ9EuJ7tPDTb7

* Fix win32-skip jit-context regex to match this repo's real skip formatting (#589)

Both spawned reviewers (Explore and oss:auditor) independently caught the same
high-severity bug in the previous commit: `match:
~pytest\.mark\.skipif\(sys\.platform *== *.win32.` requires `sys.platform`
immediately after the literal `(`, with no separator. That only matches a
single-line `pytestmark = pytest.mark.skipif(sys.platform == "win32", ...)`.
Only 5 of 118 blanket-skip test modules in this tree use that form; the other
~113, including `tests/test_plugin_promo_574.py` -- the exact #588 incident
this rule cites as its motivation -- write the condition on its own line. The
rule would not have fired on the incident it was written to catch.

Explore also found a second issue: the old regex had no `pytestmark` anchor,
so it would also fire on a per-test `@pytest.mark.skipif(...)` decorator --
exactly the shape this repo's own declined trap
`487.ostype-immutable-on-windows-gitbash`
(`.claude/jit-context/tools/00-manual/00-README.md`) already reasoned a rule
should not restate, since the skip is already visible at the decorator site.

New pattern: `~pytestmark[^)]*skipif\([^)]*win32`. Requires `pytestmark`
somewhere before an unclosed `skipif(...win32...)` call, tolerating any
whitespace/newlines in between (including the list-form
`pytestmark = [pytest.mark.skipif(...), ...]`) while stopping at the first
literal `)`, so it does not cross into an unrelated statement. Re-verified
against `pre-tool-hook.sh` directly: fires on the real multi-line content of
`tests/test_plugin_promo_574.py`, fires on a synthetic single-line form,
stays silent on a per-test decorator with no `pytestmark`, and stays silent
on ordinary test content.

The auditor's other observation -- `tests/test_windows_skip_triage_497.py`
already catches the same doc/tree drift on every CI leg via an AST scan, so
this rule's only independent value is firing before push -- is now stated
in the entry's own body rather than left implicit.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bv6BGZD2EdZ9EuJ7tPDTb7

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment