forked from bchanot/claude
fix(hooks): private code-limits state dir, bounded session start without timeout, pre-commit fails closed
code-limits: mkdir -m 700 without -p as the atomic proof, else one find test (ours, dir, 700). session-start: one runner, timeout then gtimeout then perl alarm (exec, no shell). pre-commit (generator + both copies): a git diff or grep error blocks. memory.sh line rewrap; claude-stub reads its log path from the env. Tests T16, SD19, PC1-4.
This commit is contained in:
1 parent
27946d5280
commit
1f107c3c45
10 files changed
+159
-27
No files matched your search
+11
-3
@@ -489,9 +489,17 @@ esac
|
||||
# .githooks/ (memory/doc/deploy helpers, the hooks themselves) — allow. Rules,
|
||||
# settings, agents, skills stay blocked. --no-renames lists a rename's source
|
||||
# too, so a git mv out of a blocked path cannot hide.
|
||||
if [ -z "\$(git diff --cached --name-only --no-renames | grep -vE '^(\.claude/(memory|tasks|audits|deploy)|\.githooks)/' | head -1)" ]; then
|
||||
exit 0
|
||||
fi
|
||||
staged=\$(git diff --cached --name-only --no-renames) || {
|
||||
echo "gitflow pre-commit: cannot list staged paths: BLOCKED" >&2
|
||||
exit 1
|
||||
}
|
||||
[ -n "\$staged" ] || exit 0
|
||||
printf '%s\\n' "\$staged" | grep -qvE '^(\.claude/(memory|tasks|audits|deploy)|\.githooks)/'
|
||||
case \$? in
|
||||
0) ;;
|
||||
1) exit 0 ;;
|
||||
*) echo "gitflow pre-commit: cannot filter staged paths: BLOCKED" >&2; exit 1 ;;
|
||||
esac
|
||||
|
||||
echo "gitflow pre-commit: BLOCKED — direct commit on '\$br'." >&2
|
||||
echo " Branch from the right base (feature/bugfix->develop, hotfix->main), or merge." >&2
|
||||
|
||||
+5
-1
@@ -435,7 +435,11 @@ _mem_add_locked() { # <file> <PREFIX> <entry-file> <supersedes>
|
||||
|
||||
_mem_path_safe() { # <path> → path, ./-prefixed when awk would take it for
|
||||
# a `var=value` assignment operand and read stdin instead
|
||||
if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then printf '%s\n' "./$1"; else printf '%s\n' "$1"; fi
|
||||
if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
|
||||
printf '%s\n' "./$1"
|
||||
else
|
||||
printf '%s\n' "$1"
|
||||
fi
|
||||
}
|
||||
|
||||
memory_add() { # <kind> <title> <body-file> <status> <supersedes>
|
||||
|
||||
@@ -14,7 +14,8 @@ stub_first_or_die() {
|
||||
}
|
||||
|
||||
# claude_stub_install <work-dir>: stub in <work-dir>/stub, log in
|
||||
# <work-dir>/claude.log, PATH and GIT_CEILING_DIRECTORIES exported
|
||||
# <work-dir>/claude.log (path read by the stub from CLAUDE_STUB_LOG), PATH,
|
||||
# CLAUDE_STUB_LOG and GIT_CEILING_DIRECTORIES exported
|
||||
claude_stub_install() {
|
||||
local dir
|
||||
if [ -z "${1:-}" ] || [ ! -d "$1" ]; then
|
||||
@@ -25,8 +26,11 @@ claude_stub_install() {
|
||||
CLAUDE_LOG="$dir/claude.log"
|
||||
: >"$CLAUDE_LOG" || exit 1
|
||||
mkdir -p "$dir/stub" || exit 1
|
||||
printf '#!/bin/sh\necho "$*" >>"%s"\n' "$CLAUDE_LOG" \
|
||||
>"$dir/stub/claude" || exit 1
|
||||
export CLAUDE_STUB_LOG="$CLAUDE_LOG"
|
||||
cat >"$dir/stub/claude" <<'EOF' || exit 1
|
||||
#!/bin/sh
|
||||
echo "$*" >>"$CLAUDE_STUB_LOG"
|
||||
EOF
|
||||
chmod +x "$dir/stub/claude" || exit 1
|
||||
export PATH="$dir/stub:$PATH"
|
||||
stub_first_or_die claude "$dir/stub/claude"
|
||||
|
||||
@@ -81,5 +81,25 @@ echo short >> "$WORK/ours/sub/t.sh"
|
||||
x="$(jq -n --arg c "$(cat "$WORK/ours/sub/t.sh")" '{content:$c}')"
|
||||
check T15-symlinked-dir-tracked "$(fire "$WORK/link/t.sh" s10 "$x")" ""
|
||||
|
||||
# T16: state dir is 700; a pre-planted symlink dir gets no signal, no write
|
||||
(umask 022; fire "$WORK/ours/new.ts" s11 "$x" >/dev/null)
|
||||
check T16-dir-mode-700 "$(find "$TMPDIR/claude-code-limits-s11" \
|
||||
-maxdepth 0 -type d -perm 700 | wc -l | tr -d ' ')" 1
|
||||
mkdir "$WORK/sink"
|
||||
ln -s "$WORK/sink" "$TMPDIR/claude-code-limits-s12"
|
||||
printf '%s\n' "$big" > "$WORK/ours/sym.ts"
|
||||
x="$(jq -n --arg c "$big" '{content:$c}')"
|
||||
check T16-symlink-dir-silent "$(fire "$WORK/ours/sym.ts" s12 "$x")" ""
|
||||
n="$(find "$WORK/sink" -mindepth 1 | wc -l | tr -d ' ')"
|
||||
check T16-symlink-no-write "$n" 0
|
||||
|
||||
# a pre-existing 755 dir owned by us: no signal, no write
|
||||
mkdir "$TMPDIR/claude-code-limits-s13"
|
||||
chmod 755 "$TMPDIR/claude-code-limits-s13"
|
||||
printf '%s\n' "$big" > "$WORK/ours/open.ts"
|
||||
check T16-755-dir-silent "$(fire "$WORK/ours/open.ts" s13 "$x")" ""
|
||||
check T16-755-no-write "$(find "$TMPDIR/claude-code-limits-s13" \
|
||||
-mindepth 1 | wc -l | tr -d ' ')" 0
|
||||
|
||||
printf 'PASS=%d FAIL=%d\n' "$pass" "$fail"
|
||||
[ "$fail" -eq 0 ]
|
||||
Executable
+50
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env bash
|
||||
# lib/tests/precommit-failclosed.test.sh — githooks/pre-commit blocks a
|
||||
# commit on develop when `git diff --cached` itself fails (fail closed).
|
||||
set -u
|
||||
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
HOOK="$ROOT/githooks/pre-commit"
|
||||
WORK="$(mktemp -d)" || exit 1; trap 'rm -rf "$WORK"' EXIT
|
||||
pass=0; fail=0
|
||||
check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
|
||||
printf 'FAIL %s: got[%s] want[%s]\n' "$1" "$2" "$3"; fi; }
|
||||
|
||||
R="$WORK/repo"; git init -q "$R" && cd "$R" || exit 1
|
||||
git config user.email t@t; git config user.name t
|
||||
git checkout -q -b develop 2>/dev/null
|
||||
git commit -q --allow-empty -m init # a root commit is always allowed
|
||||
mkdir -p .claude/memory && echo a > .claude/memory/x.md && git add .
|
||||
GIT_CEILING_DIRECTORIES="$WORK" bash "$HOOK" >/dev/null 2>&1
|
||||
check PC1-allowlisted-passes "$?" 0
|
||||
|
||||
echo b > code.sh && git add code.sh
|
||||
out=$(bash "$HOOK" 2>&1); rc=$?
|
||||
check PC2-code-blocked "$rc/$(printf '%s' "$out" | grep -c BLOCKED)" 1/1
|
||||
|
||||
REAL=$(command -v git)
|
||||
mkdir "$WORK/shim"
|
||||
export REAL_GIT="$REAL"
|
||||
cat > "$WORK/shim/git" <<'S'
|
||||
#!/bin/sh
|
||||
case "$*" in *"diff --cached"*) exit 128 ;; esac
|
||||
exec "$REAL_GIT" "$@"
|
||||
S
|
||||
chmod +x "$WORK/shim/git"
|
||||
git reset -q code.sh; rm code.sh # only the allowlisted path is staged
|
||||
out=$(PATH="$WORK/shim:$PATH" bash "$HOOK" 2>&1); rc=$?
|
||||
check PC3-git-error-blocks "$rc/$(printf '%s' "$out" \
|
||||
| grep -c 'cannot list staged paths: BLOCKED')" 1/1
|
||||
|
||||
# PC4: the filter stage (grep rc >= 2) blocks too
|
||||
mkdir "$WORK/shim2"
|
||||
cat > "$WORK/shim2/grep" <<'S'
|
||||
#!/bin/sh
|
||||
exit 2
|
||||
S
|
||||
chmod +x "$WORK/shim2/grep"
|
||||
out=$(PATH="$WORK/shim2:$PATH" bash "$HOOK" 2>&1); rc=$?
|
||||
check PC4-grep-error-blocks "$rc/$(printf '%s' "$out" \
|
||||
| grep -c 'cannot filter staged paths')" 1/1
|
||||
|
||||
printf 'precommit-failclosed: %d passed, %d failed\n' "$pass" "$fail"
|
||||
[ "$fail" -eq 0 ]
|
||||
@@ -322,11 +322,30 @@ cp "$ROOT/templates/memory/journal.md" "$p/.claude/memory/journal.md"
|
||||
check SD18-template-no-journal "$(run "$p" "$WORK/startup.json" | digest | grep -c '^- journal:')" 0
|
||||
printf '\n## 2026-11-09\n\n- real entry\n' >> "$p/.claude/memory/journal.md"
|
||||
check SD18-real-entry "$(run "$p" "$WORK/startup.json" | digest | grep '^- journal:')" '- journal: real entry'
|
||||
printf -- '- note on `<!--` parser\n- newest\n' >> "$p/.claude/memory/journal.md"
|
||||
printf -- "- note on \`<!--\` parser\n- newest\n" >> "$p/.claude/memory/journal.md"
|
||||
check SD18-inline-marker "$(run "$p" "$WORK/startup.json" | digest | grep '^- journal:')" '- journal: newest'
|
||||
|
||||
# SD15: no hook run of this suite called the claude CLI (BLK-043)
|
||||
check SD15-no-claude-calls "$(claude_stub_calls)" 0
|
||||
|
||||
# SD19: no timeout and no gtimeout on PATH -> the perl alarm runner still
|
||||
# bounds a hung memory.sh (stock macOS)
|
||||
bin="$WORK/notmo"; mkdir -p "$bin"
|
||||
for c in bash git cat sed awk grep tail head cut dirname readlink wc tr \
|
||||
sort uniq iconv env date basename mktemp find ls cp rm mkdir cmp stat id \
|
||||
uname tee printf perl sleep jq; do
|
||||
src=$(command -v "$c" 2>/dev/null) && ln -sf "$src" "$bin/$c"
|
||||
done
|
||||
cat > "$WORK/sd9/lib/memory.sh" <<'EOF'
|
||||
#!/usr/bin/env bash
|
||||
exec sleep 8
|
||||
EOF
|
||||
p=$(mkproj sd19)
|
||||
SECONDS=0
|
||||
(cd "$p" && PATH="$bin" bash "$S9" </dev/null >/dev/null 2>&1)
|
||||
check SD19-no-timeout-bin "$(PATH="$bin" command -v timeout gtimeout \
|
||||
| wc -l | tr -d ' ')" 0
|
||||
check SD19-perl-bounds-hang "$([ "$SECONDS" -lt 6 ] && echo yes || echo no)" yes
|
||||
|
||||
printf 'session-digest: %d passed, %d failed\n' "$pass" "$fail"
|
||||
[ "$fail" -eq 0 ]
|
||||
Reference in new issue
Block a user