Skip to content

T196: Hook reads PowerShell by its own rules and every field of a command tool #69

Closed

Description

@erickb336

Paths updated after #56 merged (2026-10-06). Sources now live in packages/; plugins/sage/ is build output (never edit it; npm run check fails on a hand edit). Hook: packages/sage-claude/hooks/sage-hook.mjs. State tool logic: packages/sage-core/state.mjs (the Claude wrapper is packages/sage-claude/skills/sage/sage.mjs). Skills and agents: packages/sage-claude/skills/, packages/sage-claude/agents/. Line numbers in this issue are from before the move.

Task: T196 · small · risk flag: auth (needs 2 clean review cycles) · build after #57 (T199), same code

Goal

The hook judges PowerShell command text by PowerShell's rules, and its merge and push rules read every field of a command tool's input, not only command.

Why

A security review of PR #33 found:

  • The hook reads PowerShell text with the bash grammar. In PowerShell the backtick is the escape character and ( ) and $( ) build words, so these name no gh or merge word to the hook and pass with no merge check, for an agent and for the main session: g`h pr me`rge 41 --squash --delete-branch --match-head-commit <sha>, gh pr ("mer"+"ge") 41 ..., gh pr $("mer"+"ge") 41 ....
  • gitGate reads only tool_input.command. A command tool whose command text sits in another field reads as empty and passes. The agent check of PR Hook: only the chief writes the logbook; agents run read commands only (T83) #36 already reads every field (fields(ti)).
  • The terminal tool (mcp__terminal__run_in_terminal) has its own cwd field, which the hook does not read. So the push guard "this checkout is on main" checks the session's folder, not the folder where the command runs.

Scope

  • packages/sage-claude/hooks/sage-hook.mjs: the dispatch to gitGate (about line 278) and gitGate itself; mentionsMerge, mergeIn, pushProblem.
    • For the tool PowerShell: fail closed on a backtick, ( or $( in a command that also has the words gh, git, pr, merge or push, or remove backticks and join the parts before the word tests. Pick one, say why in the pull request.
    • Read the merge and push text from every string field of the tool input, as the agent check does.
    • Use the terminal tool's tool_input.cwd, when present, for the "checkout is on main" guard.
  • Tests in scripts/sage-hook.test.mjs for each example, for an agent and for the main session.

Acceptance

  • Each example above is refused; plain PowerShell commands without those words still pass.
  • The new tests fail on main and pass on the branch.
  • npm run build, npm run check, npm test pass.

How this hand-off works

  • This issue is a task of sage, the agent kit in this repository. The sage keeps its own record of the task (its logbook) under the id in the title.
  • Work on a new branch named codex/<task id in lower case>-<short name>, for example codex/t199-brace-words.
  • Open a pull request whose title ends with the task id in parentheses, for example (T199). Start its body with Closes #<this issue> and Sage task <id>.
  • The sage then runs its reviews (code, security when the task has a risk flag, and QA) and merges when the review cycles are clean. Never merge, never push to main, never force-push.
  • In the pull request body, give the evidence: the commands you ran and their last lines (the test counts), and say what you did not check.

Rules for every sage task

  1. Read AGENTS.md first. Edit only sources; never edit a generated file (a skill marked "Generated", or instructions/core.md), and never edit upstream/pstack/ by hand.
  2. Run npm run build, then npm run check and npm test. All must pass. The full test run can take over 5 minutes.
  3. A rule that matters goes into code with a test. Show that each new test fails on main and passes on your branch: check out main in a separate worktree (git worktree add --detach <tmp> origin/main), never with git stash.
  4. Never let any code you run read or signal the real process list. Put a fake ps, pgrep and kill first on PATH for every test run.
  5. Never pass a test string to a real shell (bash, zsh, sh, eval, child_process with shell: true). Attack strings go to the hook's parser only.
  6. A flaky test is a bug: fix it or remove it in the same task. Never re-run a test until it passes. No assertion on wall-clock or CPU time unless it has a wide margin and a reason in the test.
  7. The README graphics come from scripts/graphics.mjs (npm run graphics); never edit an SVG in docs/assets by hand.
  8. Write text for people at about 80% of Simplified Technical English (writing/ste-80.md), with the approved words of writing/dictionary.md.
  9. Stop at a product question: write it in the pull request with your recommendation instead of guessing.

Activity

  1. added
    codex
    on Oct 6, 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

    codex

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions