Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion hooks/simplify-ignore-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,44 @@ cat "$PROJ/t" > "$RT"
rt_hook_event "$PROJ" "" "" 2>/dev/null || true
assert_eq "no-event removal: Stop puts the protected block back" "1" "$(grep -c 'const keepOne = 42' "$RT" || true)"

# ── Test 18: Stop refuses cached restore paths outside the project ────────────
printf '\nTest 18: Stop refuses cached restore paths outside the project\n'
run_untrusted_restore_path() {
local name="$1" target="$2" project="$TMPDIR/path-guard-$1"
local cache="$project/.claude/.simplify-ignore-cache" id="pathguard"
mkdir -p "$cache"
printf '%s' "$target" > "$cache/${id}.path"
printf '%s\n' "attacker-controlled backup" > "$cache/${id}.bak"

rt_hook_event "$project" "" "" 2>/dev/null || true
assert_eq "$name: external target is unchanged" "original outside content" "$(cat "$outside_file")"
assert_eq "$name: rejected backup remains for recovery" "1" \
"$([ -f "$cache/${id}.bak" ] && echo 1 || echo 0)"
}

outside_file="$TMPDIR/path-guard-outside"
printf '%s\n' "original outside content" > "$outside_file"
run_untrusted_restore_path "absolute path" "$outside_file" "original outside content"
run_untrusted_restore_path "parent-traversal" "$TMPDIR/path-guard-parent-traversal/../path-guard-outside" "original outside content"

mkdir -p "$TMPDIR/path-guard-symlinked-parent"
if ln -s "$TMPDIR" "$TMPDIR/path-guard-symlinked-parent/link" 2>/dev/null &&
[ -L "$TMPDIR/path-guard-symlinked-parent/link" ]; then
run_untrusted_restore_path "symlinked-parent" "$TMPDIR/path-guard-symlinked-parent/link/path-guard-outside" "original outside content"
else
printf ' SKIP: symlinked parent (symlink creation unavailable)\n'
fi

mkdir -p "$TMPDIR/path-guard-symlinked-target"
if ln -s "$outside_file" "$TMPDIR/path-guard-symlinked-target/link" 2>/dev/null &&
[ -L "$TMPDIR/path-guard-symlinked-target/link" ]; then
run_untrusted_restore_path "symlinked-target" "$TMPDIR/path-guard-symlinked-target/link" "original outside content"
else
printf ' SKIP: symlinked target (symlink creation unavailable)\n'
fi

else
printf '\nTests 14-17 skipped: jq not available (hook no-ops without it)\n'
printf '\nTests 14-18 skipped: jq not available (hook no-ops without it)\n'
fi

# ── Summary ──────────────────────────────────────────────────────────────
Expand Down
46 changes: 45 additions & 1 deletion hooks/simplify-ignore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ if ! command -v jq >/dev/null 2>&1; then
printf '%s\n' "error: missing jq" >&2; exit 1
fi

CACHE="${CLAUDE_PROJECT_DIR:-.}/.claude/.simplify-ignore-cache"
PROJECT_DIR="${CLAUDE_PROJECT_DIR:-.}"
CACHE="$PROJECT_DIR/.claude/.simplify-ignore-cache"
if [ -t 0 ]; then INPUT="{}"; else INPUT=$(cat); fi

# Parse hook input — trap errors explicitly so set -e doesn't cause
Expand All @@ -53,6 +54,42 @@ hash_cmd() {
}
file_id() { printf '%s' "$1" | hash_cmd | cut -c1-16; }
block_hash() { printf '%s' "$1" | hash_cmd | cut -c1-8; }

# -- safe_restore_target: may Stop restore the cached target at $1? -----------
# A stale or malformed cache entry can name a file outside the project. Stop
# must not overwrite or copy data through that path:
# - no newline and no `..` component (checked before anything is resolved);
# - the final component is not a symlink (a write would follow it);
# - the nearest existing ancestor, resolved physically with `cd -P` so that
# symlinked parent directories are followed, lies under the physically
# resolved project root. Missing trailing directories are allowed (a moved
# file's directory may be gone) but must not exist as anything else.
# On Git Bash/MSYS, C:\... and C:/... paths are converted with cygpath first.
safe_restore_target() {
local p="$1" root="$PROJECT_DIR" dir next rest="" nl='
'
[ -n "$p" ] || return 1
case "$p" in *"$nl"*) return 1 ;; esac
case "/${p//\\//}/" in */../*) return 1 ;; esac
if command -v cygpath >/dev/null 2>&1; then
p=$(cygpath -u -- "$p" 2>/dev/null) || return 1
root=$(cygpath -u -- "$root" 2>/dev/null) || return 1
fi
case "/${p//\\//}/" in */../*) return 1 ;; esac
if [ -L "$p" ]; then return 1; fi
root=$(cd -P -- "$root" 2>/dev/null && pwd -P) || return 1
dir=$(dirname -- "$p")
while [ ! -d "$dir" ]; do
if [ -L "$dir" ] || [ -e "$dir" ]; then return 1; fi
rest="/$(basename -- "$dir")$rest"
next=$(dirname -- "$dir")
if [ "$next" = "$dir" ]; then return 1; fi
dir="$next"
done
dir=$(cd -P -- "$dir" 2>/dev/null && pwd -P) || return 1
case "${dir%/}$rest/$(basename -- "$p")" in "${root%/}"/*) return 0 ;; esac
return 1
}
# Escape glob metacharacters so ${var/pattern/repl} treats pattern as literal.
# Needed for Bash 3.2 (macOS) where quotes don't suppress globbing in PE patterns.
escape_glob() {
Expand Down Expand Up @@ -262,6 +299,13 @@ if [ -z "$TOOL_NAME" ]; then
pathfile="$CACHE/${fid}.path"
[ -f "$pathfile" ] || { rm -f "$bak"; continue; }
orig=$(cat "$pathfile")
if ! safe_restore_target "$orig"; then
# Never read or write through a path the cache names outside the
# project. Keep the entry so a genuine backup is not destroyed.
printf 'Warning: refusing to restore %s: outside %s or through a symlink. Cache entry kept at %s\n' \
"$orig" "$PROJECT_DIR" "$bak" >&2
continue
fi
if [ -f "$orig" ]; then
# A change can reach the file through a route that fires no Edit or Write
# event (a Bash command, a formatter, an external editor), and the backup
Expand Down
Loading