Skip to content

guardrails: git guards strip a trailing newline from the payload cwd, resolving a sibling repo (fail-open, shared with main) #1536

Closed

Description

@kyle-sexton

Problem

block-noncanonical-commit (and block-dangerous-git, same idiom) reads the hook payload's cwd through a bare command substitution:

HOOK_CWD=$(printf '%s' "$INPUT" | jq -r '.cwd // empty' 2>/dev/null | tr -d '\r')

$(…) strips EVERY trailing newline. jq -r appends its own line-terminator to the value, so a cwd whose path legitimately ends in a newline yields two trailing newlines and both are stripped — HOOK_CWD becomes a different sibling directory. POSIX permits any byte but NUL and / in a path, so a top level ending in \n is representable.

Every downstream repository resolution keyed on HOOK_CWD (the sequencer probe, the alias walk's effective_dir base, the launch-directory probe) then operates on the wrong directory. In the alias-chain threat model a persisted !git -C child p chain would be evaluated against the stripped sibling's benign alias while git launches from the real newline-suffixed repository and runs its non-canonical alias — a fail-open.

Why deferred (not fixed in #1503)

  • Pre-existing and shared with main. The HOOK_CWD read is unchanged by fix(guardrails): close git-alias-chain re-expansion bypass of both git guards #1503; both guards on main strip identically. fix(guardrails): close git-alias-chain re-expansion bypass of both git guards #1503 closes the launch-directory probe variant (a -C descent into a newline-named nested repo, attacker-reachable via repo layout) with sentinel-framed git_rev_parse_field; this is a distinct entry point.
  • Lower severity than the probe variant. The session cwd is the user's own working directory, not attacker-controlled the way a nested repo's name/layout is.
  • Cross-platform fix is non-trivial. On Windows Git Bash, jq -r emits CRLF (verified via od -c), not a bare LF, so a byte-exact capture must distinguish the value's own trailing byte(s) from jq's terminator across both terminator conventions — more than the one-line sentinel that sufficed for the git rev-parse probe (git emits a bare LF there, verified on 2.54.0.windows.1).

Fix direction

Capture HOOK_CWD (and any other payload path field, e.g. .tool_input.command where a path is extracted) through a sentinel that absorbs $(…)'s strip, then remove exactly jq's terminator — accounting for jq's CRLF on Windows vs bare LF on POSIX. Add a POSIX-gated regression fixture (a cwd whose path ends in a newline, with a stripped sibling carrying a canonical alias) asserting the block on POSIX and skipping loudly on Windows.

Related

Same family as the launch-directory newline findings closed in #1503. Sibling deferrals from that PR: #1500, #1501.

Activity

  1. added
    needs-triage
    on Jul 26, 2026
  2. kyle-sexton commented on Jul 26, 2026

    @kyle-sexton
    MemberAuthor

    This was generated by AI during triage.

    Verification

    Confirmed the core claim against current main: plugins/guardrails/hooks/block-noncanonical-commit.sh:100
    is exactly

    HOOK_CWD=$(printf '%s' "$INPUT" | jq -r '.cwd // empty' 2>/dev/null | tr -d '\r')

    $(...) strips every trailing newline, and jq -r appends its own terminator, so a .cwd value
    whose path legitimately ends in \n loses that trailing byte and HOOK_CWD silently resolves to a
    different (sibling) directory. HOOK_CWD is then used as the path-resolution base at line 151
    (local base="${HOOK_CWD:-${CLAUDE_PROJECT_DIR:-.}}"). Confirmed for block-noncanonical-commit.sh.

    Correction to the issue body: the claim that block-dangerous-git.sh shares "the same idiom" does
    not hold against current code. block-dangerous-git.sh never reads .cwd from the hook payload at
    all — it reads only .tool_input.command and .tool_name (lines 90/92), and its only use of
    CLAUDE_PROJECT_DIR is for telemetry (line 110), not repository-resolution. There is no HOOK_CWD
    variable or newline-stripping path in that file. The finding's blast radius is narrower than
    described: one hook file, not two. Whoever picks this up should re-verify against the branch
    they're working from rather than assume both guards need the fix.

    The self-assessed lower severity holds: the precondition (the session's actual working directory
    path ending in a literal newline byte) is user-controlled, not attacker-controlled the way the
    already-fixed launch-directory / nested-repo variants (#1500, #1501, #1503) were — so this is a
    real fail-open, but a narrow and low-likelihood one, narrower still once the block-dangerous-git.sh
    half of the claim is removed.

    Triage decision

    Routing human-gated, consistent with the sibling family (#1469, #1486, #1495, #1500, #1501,
    #1502) — the fix requires a genuine engineering judgment call (a byte-exact capture that
    distinguishes the value's own trailing byte(s) from jq's own terminator, across jq's differing
    LF-vs-CRLF terminator convention on POSIX vs. Windows Git Bash), not a mechanical mirror of
    already-landed work.

    Agent Brief

    Type: Bug
    Summary: block-noncanonical-commit.sh's HOOK_CWD extraction strips a legitimate trailing
    newline from the hook payload's cwd field via $(...), which can resolve repository operations
    against the wrong (sibling) directory.

    Current behavior: HOOK_CWD is captured via a bare $(... | tr -d '\r') command substitution,
    which discards all trailing newlines including one that is part of the real cwd value. Downstream
    consumers of HOOK_CWD (currently: the path-resolution base) then operate on a stripped, incorrect
    path.

    Desired behavior: HOOK_CWD should preserve a trailing newline that is part of the actual cwd
    value, removing only jq's own line terminator — on both POSIX (bare LF) and Windows Git Bash
    (CRLF, verified via od -c) — using a sentinel-framed capture (the same technique already used
    for the git rev-parse launch-directory probe fix in #1503) rather than relying on $(...)'s strip.

    Key interfaces:

    Acceptance criteria:

    • A cwd payload value ending in a real trailing newline resolves HOOK_CWD to that exact
      value, not a newline-stripped sibling
    • Regression fixture: a cwd ending in \n with a stripped-sibling directory carrying a
      canonical alias — asserts the guard still blocks correctly
    • Fixture is POSIX-gated (skips loudly, not silently, on Windows) per the issue's own note that
      jq's CRLF terminator on Windows needs separate handling
    • block-dangerous-git.sh is re-checked for whether it independently needs the same fix (per
      the correction above, current code suggests it does not — confirm before including it in scope)

    Out of scope:

  3. added
    area: security
    needs-human
    and removed
    needs-triage
    on Jul 26, 2026
  4. added theissue type on Jul 26, 2026
  5. kyle-sexton commented on Aug 12, 2026

    @kyle-sexton
    MemberAuthor

    Closing: payload cwd is now read via hook::jq_fields (NUL-separated read -d '') instead of HOOK_CWD=$(jq -r ...), so trailing newlines in the path are preserved. Landed with the guardrails jq batching work (#2120).

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

    area: securityneeds-human

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions