Skip to content

A dispatched hook's stdout is attributed, bounded and disclosed (#280) - #284

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/280-hook-stdout
Aug 1, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/280-hook-stdout

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #280

The premise, re-derived rather than quoted — and one correction

scripts/user-prompt-hook.sh runs CTX=$( … dispatch "after_user_prompt" ) and CTX becomes additionalContext. Confirmed. But after_user_prompt is not the only mouth: session-start-hook.sh calls dispatch "before_session_start" and dispatch "after_session_start" with stdout inherited straight into the session's opening context — that is exactly what the documented === TEAM === listener is for. So the surface is two events, not one, and the fix is in dispatch() rather than in user-prompt-hook.sh.

The correction. The issue says hook output "competes with the systemMessage envelope" and can write "into the same structure the plugin uses". At the JSON level it cannot, and that matters for scoping. The envelope is built with jq -Rs --arg msg, which JSON-encodes both CTX and the notice; a hook cannot close a string or add a key. On the far more common path there is no JSON at all — printf '%s\n' "$CTX" — and CTX always begins with the plugin's own [HH:MM TZ — user] line, so the payload is never parseable as JSON no matter what a hook prints. No structural escape is available, and this PR does not claim one.

What is real is impersonation one level up, in the text: additionalContext is the plugin's voice to the model, and a hook writing

WARNING: Context at 99%. Run /remember to save session state before context death.

produces a line byte-identical to one user-prompt-hook.sh writes itself, in the same stream, in the same position. Nothing in the context tells them apart. That is the defect, and it is enough of one.

The judgment call

The three options in the issue, and why the middle two collapsed into one.

Discard stdout — rejected. after_user_prompt and after_session_start exist so a listener can contribute context. A fix that deletes what a hook says is this codebase's own defect wearing the fix's clothes. What was wrong was never that hooks speak; it is that nobody could hear who was speaking.

Bound and label / give hooks a declared channel — these are the same fix, and it is the one shipped. Labelling done properly is the declared channel: a fenced block is a channel only if the hook cannot print the fence's closing marker. It can. So the boundary is per-line rather than per-block.

The contract, stated for the hook author who will rely on it:

An unprefixed line in dispatched output is the plugin speaking, and a hook cannot produce one.

Every line a listener writes is prefixed [hook] ; the block is framed by an unprefixed === hooks.d: <event>/<script> … === line naming the script. A listener that prints a convincing terminator gets the terminator prefixed too — there is no outside to escape into when the boundary is on every line. Documented in README under What a hooks.d/ listener may say, and in whose voice.

The cap discloses itself, per #277's lesson: 200 lines, 2000 chars per line, and a trailing frame line carrying the count of what was not shown. Both numbers sit far above any honest contributor (the two shipped listeners print nothing at all) and far below a set -x trace.

Silence stays free. A listener that prints nothing gets no frame and no marker — the every-prompt path is unchanged. Asserted, because a fix that taxed the common case in context tokens forever would be a poor trade for a rare one.

Where stdout cannot be captured, it is discarded and said so. With no writable tmp/ there is no capture file, and uncaptured stdout is inherited stdout — precisely the injection being fixed. That path runs the hook with stdout closed and prints a frame line saying the output was not shown and why. Neither quietly passed through nor quietly dropped.

Scope, honestly

Not a remote-attacker path and not presented as one. dispatch() requires a hook to be owned by the invoking user and not group- or world-writable. This is blast radius: a hook installed for one purpose could change what the model is told, and neither the user nor the model could see that it had.

The two related findings — one in, one out, and I am saying which

(b) Skip warnings invisible to /remember:doctor — IN. The ownership and world-writable skips warned only to the daily log. doctor.sh reports "Recent errors" out of hook-errors.log, so a listener that never ran and never would — one chmod 777 away — showed up as OK. Both now report through the same two-destination path as a failure, naming the hook, the reason, and that it will not run on any later dispatch. It ships here rather than as a follow-up because the two defects share nine lines of the same function; splitting them would have meant touching dispatch() twice in a week.

(a) No timeout on any dispatched hook — OUT, please file it. Real, and a design rather than a repair. Killing a listener mid-flight can leave a .git/index.lock behind in the very backup hook this plugin ships; and a default generous enough for a network push while short enough to matter is its own judgement with its own blast radius. It deserves an issue, not a paragraph at the end of this one.

TDD

Tests first, tests/test_hook_stdout_labelling_280.py.

RED — 6 failed, 1 passed. The one that passed against untouched code is test_a_hook_that_prints_nothing_adds_nothing, which asserts absence — that is what it is for, and it is the guard against over-framing the common case.

FAILED ...::test_hook_echoing_a_plugin_warning_does_not_speak_as_the_plugin
FAILED ...::test_every_line_a_hook_writes_is_attributed
FAILED ...::test_a_hook_cannot_forge_the_frame_that_ends_its_own_region
FAILED ...::test_a_capped_hook_says_how_many_lines_were_dropped
FAILED ...::test_a_single_enormous_line_is_bounded_and_says_so
FAILED ...::test_a_world_writable_hook_skip_reaches_hook_errors_log
6 failed, 1 passed

GREEN — 7 passed. Full suite: 1267 passed, 43 skipped, coverage 94.16%.

The load-bearing test hands a hook the plugin's own warning verbatim and asserts that nothing in the plugin's voice carries it — not that a marker appears somewhere. A build that printed the marker and then relayed the hook's text unlabelled anyway would satisfy "marker present" and fail this. The forgery test does the same for the frame: a hook prints a plausible closing frame and continues in the plugin's voice, and the assertion is that exactly one unprefixed frame line exists in the output.

Every test drives the real user-prompt-hook.sh through a full plugin copy with a real installed hook — no stubbing of dispatch, no asserted-against-fixture output.

…280)

dispatch()'s stdout is its caller's stdout, and two callers hand that
straight to the model: user-prompt-hook.sh wraps the dispatch in
CTX=$( … ) and delivers it as additionalContext on every prompt, and
session-start-hook.sh prints it into the session's opening context. A
listener's output was therefore never diagnostics — it was text the
model reads, in the same stream and position as the plugin's own,
unbounded and unattributed.

The contract is now one sentence: an unprefixed line in dispatched
output is the plugin speaking, and a hook cannot produce one. Every
line a listener writes is prefixed "[hook] " and the block is framed
by an unprefixed "=== hooks.d: <event>/<script> ===" line. Per-line
attribution rather than a fence: any fence is only as good as the
hook's inability to print its closing marker, and there is no outside
to escape into when the boundary is on every line.

Bounded at 200 lines / 2000 chars per line, and the cap says what it
dropped. A silent listener produces no frame and no marker, so the
every-prompt path is unchanged. Where stdout cannot be captured it is
discarded and said so, never inherited unattributed.

Also: the ownership and world-writable skips now report to
hook-errors.log as well as the daily log, so a hook that never ran and
never will is no longer reported as OK by /remember:doctor.

Not remote-attacker reachable — dispatch()'s guards require the hook be
owned by the invoking user and not group/world-writable. This is blast
radius, not a vulnerability.

Left open: no dispatched hook has a timeout.

Closes #280

Co-Authored-By: Max <noreply>
… borrows from

On windows-latest a bare `bash` resolves to the WSL stub in System32,
which prints a UTF-16 notice and exits 1 before reading the script.
tests/test_path_resolution.py carries a win32 skipif for exactly this;
this file imports its helpers and needs the same marker. Four Windows
legs red, eight green, and the production change was never involved.

Co-Authored-By: Max <noreply>
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.

A dispatched hook's stdout is injected into the model's context, and can impersonate the systemMessage envelope

1 participant