Skip to content

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

Closed

Description

@fdaviddpt

Found by the agent fixing #277, while establishing why dispatch() sends hook stderr to /dev/null. #277 is about protecting one stream; this is the other stream, already unprotected.

What happens

scripts/user-prompt-hook.sh:130-161 runs dispatch "after_user_prompt" inside a command substitution:

CTX=$( … dispatch "after_user_prompt" … )

and CTX becomes additionalContext — text handed straight to the model. So anything an installed hooks.d/after_user_prompt/ hook writes to stdout lands in the prompt context verbatim.

Two consequences, and the second is the sharper one:

  1. Arbitrary text into the model's context. A hook that prints a stray debug line, a progress indicator, or a set -x trace injects it into every prompt.
  2. It competes with the systemMessage envelope built below it. A hook writing something JSON-shaped is writing into the same structure the plugin uses to speak to the model in its own voice — so hook output can impersonate the plugin's own framing rather than merely appear alongside it.

Why this is worth filing rather than noting

#277 exists because a hook's stderr was discarded, so a fatal diagnostic reached nobody. This is the mirror image on stdout: a hook's output reaches the model, unbounded and unlabelled, with nothing marking where plugin text ends and hook text begins.

The ownership and world-writable guards in dispatch() mean a hook must be owned by the current user and not group- or world-writable, which is a real bound — this is not a remote-attacker path. It is a blast-radius problem: a local hook the user installed for one purpose can silently change what the model is told, and neither the user nor the model can see that it did.

Not proposing the fix

The obvious moves each carry a cost worth weighing rather than assuming:

Which of those is right depends on what the after_user_prompt contract is meant to promise, and that is a decision rather than a repair.

Related, from the same review

  • No timeout on any dispatched hook. They run sequentially in the foreground of PostToolUse; one that hangs stalls the agent indefinitely and logs nothing, because nothing has failed.
  • The ownership / world-writable skips warn only to the daily log, not to hook-errors.log — so a hook that never runs and never will is invisible to /remember:doctor, which reports OK.

Activity

  1. added a commit that references this issue on Aug 1, 2026
  2. fdaviddpt commented on Aug 1, 2026

    @fdaviddpt
    ContributorAuthor

    Correcting my own framing on this issue, now that it is fixed: the "impersonate the systemMessage envelope" half — which is in the title — does not hold.

    The agent that fixed it re-derived the CTX=$(…) construct (correct) and then disproved the envelope claim rather than inheriting it:

    • The envelope is built with jq -Rs --arg msg, which JSON-encodes both CTX and the notice. A hook cannot close a string or introduce a key.
    • On the far more common path there is no JSON at all (printf '%s\n' "$CTX").
    • CTX always begins with the plugin's own [HH:MM TZ — user] line, so the payload is never parseable as JSON regardless of what a hook prints.

    So there was no structural escape available, and the fix does not claim one.

    What is real, and was enough on its own: impersonation one level up, in the text. A hook printing WARNING: Context at 99%. Run /remember… produces a line byte-identical to one user-prompt-hook.sh writes itself — same stream, same position, no way for the reader to tell them apart.

    Second correction: the surface was two events, not one. I named only user-prompt-hook.sh. session-start-hook.sh inherits dispatch stdout straight into the session's opening context — that is what the documented === TEAM === listener is for. So the fix went into dispatch(), the shared chokepoint, and covers both.

    The contract that shipped, for anyone reading this later: an unprefixed line in dispatched output is the plugin speaking, and a hook cannot produce one. Every hook line is prefixed [hook] ; the block is framed by an unprefixed === hooks.d: <event>/<script> … ===. The two options I offered — "bound and label" versus "declared channel" — collapse into one, because a fenced block is only a channel if the hook cannot print the closing marker, and it can. The boundary has to be per-line.

    A silent hook still produces nothing: no frame, no marker, byte-identical output on the every-prompt path.

    Fixed by #284. Also folded in there: the ownership/world-writable skips now write to hook-errors.log as well as the daily log, so a hook that never runs is no longer invisible to /remember:doctor — the two defects shared nine lines of the same function.

  3. added 3 commits that reference this issue on Oct 4, 2026
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