Repository navigation
Description
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. TheHOOK_CWDread is unchanged by fix(guardrails): close git-alias-chain re-expansion bypass of both git guards #1503; both guards onmainstrip identically. fix(guardrails): close git-alias-chain re-expansion bypass of both git guards #1503 closes the launch-directory probe variant (a-Cdescent into a newline-named nested repo, attacker-reachable via repo layout) with sentinel-framedgit_rev_parse_field; this is a distinct entry point. - Lower severity than the probe variant. The session
cwdis 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 -remitsCRLF(verified viaod -c), not a bareLF, 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 thegit rev-parseprobe (git emits a bareLFthere, 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
fix(guardrails): defer newline-cwd strip; drop conflated launch-dir f…
kyle-sexton commentedon Jul 26, 2026
This was generated by AI during triage.
Verification
Confirmed the core claim against current
main:plugins/guardrails/hooks/block-noncanonical-commit.sh:100is exactly
HOOK_CWD=$(printf '%s' "$INPUT" | jq -r '.cwd // empty' 2>/dev/null | tr -d '\r')$(...)strips every trailing newline, andjq -rappends its own terminator, so a.cwdvaluewhose path legitimately ends in
\nloses that trailing byte andHOOK_CWDsilently resolves to adifferent (sibling) directory.
HOOK_CWDis then used as the path-resolution base at line 151(
local base="${HOOK_CWD:-${CLAUDE_PROJECT_DIR:-.}}"). Confirmed forblock-noncanonical-commit.sh.Correction to the issue body: the claim that
block-dangerous-git.shshares "the same idiom" doesnot hold against current code.
block-dangerous-git.shnever reads.cwdfrom the hook payload atall — it reads only
.tool_input.commandand.tool_name(lines 90/92), and its only use ofCLAUDE_PROJECT_DIRis for telemetry (line 110), not repository-resolution. There is noHOOK_CWDvariable 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.shhalf 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'sHOOK_CWDextraction strips a legitimate trailingnewline from the hook payload's
cwdfield via$(...), which can resolve repository operationsagainst the wrong (sibling) directory.
Current behavior:
HOOK_CWDis captured via a bare$(... | tr -d '\r')command substitution,which discards all trailing newlines including one that is part of the real
cwdvalue. Downstreamconsumers of
HOOK_CWD(currently: the path-resolution base) then operate on a stripped, incorrectpath.
Desired behavior:
HOOK_CWDshould preserve a trailing newline that is part of the actualcwdvalue, removing only jq's own line terminator — on both POSIX (bare
LF) and Windows Git Bash(
CRLF, verified viaod -c) — using a sentinel-framed capture (the same technique already usedfor the
git rev-parselaunch-directory probe fix in #1503) rather than relying on$(...)'s strip.Key interfaces:
HOOK_CWDextraction inblock-noncanonical-commit.sh(and any other payload path field read thesame way, e.g. a path pulled out of
.tool_input.command)git rev-parseprobe in fix(guardrails): close git-alias-chain re-expansion bypass of both git guards #1503Acceptance criteria:
cwdpayload value ending in a real trailing newline resolvesHOOK_CWDto that exactvalue, not a newline-stripped sibling
cwdending in\nwith a stripped-sibling directory carrying acanonical alias — asserts the guard still blocks correctly
jq's CRLF terminator on Windows needs separate handling
block-dangerous-git.shis re-checked for whether it independently needs the same fix (perthe correction above, current code suggests it does not — confirm before including it in scope)
Out of scope:
block-dangerous-git.shunless re-verification in acceptance criteria above findsit actually shares the defect
kyle-sexton commentedon Aug 12, 2026
Closing: payload
cwdis now read viahook::jq_fields(NUL-separatedread -d '') instead ofHOOK_CWD=$(jq -r ...), so trailing newlines in the path are preserved. Landed with the guardrails jq batching work (#2120).