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:
bchanot committed 2026-11-13 18:25:33 +01:00
1 parent 27946d5280
commit 1f107c3c45
10 files changed
+159 -27

No files matched your search

+11 -3
View File
@@ -38,9 +38,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
+11 -3
View File
@@ -38,9 +38,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
View File
@@ -88,7 +88,11 @@ main() {
[ -n "$(field .tool_input.new_string)" ] && export CL_EDIT=1
dir="${TMPDIR:-/tmp}/claude-code-limits-$sid"
mark="$dir/$(hash16 "$file")"
[ "$mark" != "$dir/" ] && mkdir -p "$dir" 2>/dev/null || return 0
[ "$mark" != "$dir/" ] || return 0
# mkdir success is atomic proof the dir is ours; else accept only an
# owned 700 dir (find does not follow a symlink operand)
mkdir -m 700 "$dir" 2>/dev/null || [ -n "$(find "$dir" -maxdepth 0 \
-type d -user "$(id -u)" -perm 700 2>/dev/null)" ] || return 0
[ -e "$mark" ] && return 0
is_ours "$file" "$dir" || return 0
hits="$(over_lines "$file")"
+19 -12
View File
@@ -36,6 +36,17 @@ elif [ -t 0 ]; then
_ss_digest=1
fi
# ── Bounded runner: timeout, else gtimeout, else perl alarm (stock macOS) ──
# perl execs the command, so the child is the same process alarm kills.
_SS_TMO=()
for _ss_t in timeout gtimeout; do
command -v "$_ss_t" >/dev/null 2>&1 && { _SS_TMO=("$_ss_t" 2); break; }
done
if [ ${#_SS_TMO[@]} -eq 0 ] && command -v perl >/dev/null 2>&1; then
_SS_TMO=(perl -e "alarm shift; exec { \$ARGV[0] } @ARGV" 2)
fi
unset _ss_t
# ── Quick health check (filesystem only, no subprocesses) ──
BROKEN=()
for f in CLAUDE.md settings.json agents skills rules; do
@@ -167,12 +178,13 @@ ALWAYS_ON=()
detect_rtk &>/dev/null && ALWAYS_ON+=("rtk")
_toggle_owned=" gstack ui-ux-pro-max context7 graphify "
while IFS= read -r _pl; do
_pl="${_pl%%@*}"
[ -n "$_pl" ] || continue
case "$_toggle_owned" in
*" $_pl "*) : ;;
*) ALWAYS_ON+=("$_pl") ;;
esac
done < <(sed 's/@.*//' <<<"$(list_on_plugins)")
done < <(list_on_plugins)
unset _toggle_owned _pl
ALWAYS_ON_STR="${ALWAYS_ON[*]:-none}"
# 40-char width, greedy split: keeps the right border aligned on overflow
@@ -248,14 +260,12 @@ fi
_mem_lib="$(dirname "${BASH_SOURCE[0]}")/../lib/memory.sh"
if [ -f "$_mem_lib" ] && [ -d .claude/memory ]; then
# a hung memory.sh must not stall every session start
_ss_tmo=()
command -v timeout >/dev/null 2>&1 && _ss_tmo=(timeout 2)
_v1=$("${_ss_tmo[@]}" bash "$_mem_lib" check .claude/memory 2>&1 >/dev/null \
_v1=$("${_SS_TMO[@]}" bash "$_mem_lib" check .claude/memory 2>&1 >/dev/null \
| grep -c '^memory: v1:' || true)
if [ "${_v1:-0}" -gt 0 ]; then
printf "│ ⚠️ %-44s│\n" "registry v1: ${_v1} file(s) (memory.sh check)"
fi
unset _v1 _ss_tmo
unset _v1
fi
unset _mem_lib
@@ -306,9 +316,7 @@ _digest_cut() {
# Open BLK (≤ 3 listed, the rest counted) and upstream count, as digest lines.
_digest_blockers() { # <root> <memory.sh>
local row line rest status title open=0 upstream=0 more="" tmo=()
# a hung memory.sh must not stall every session start
command -v timeout >/dev/null 2>&1 && tmo=(timeout 2)
local row line rest status title open=0 upstream=0 more=""
while IFS= read -r row; do
[[ "$row" == "| BLK-"* ]] || continue
line="${row#| }"; line="${line% |}"
@@ -320,7 +328,7 @@ _digest_blockers() { # <root> <memory.sh>
[ "$open" -le 3 ] && echo "- open: ${line%% | *} $title" ;;
upstream) upstream=$((upstream + 1)) ;;
esac
done < <("${tmo[@]}" bash "$2" -C "$1" index blocker 2>/dev/null)
done < <("${_SS_TMO[@]}" bash "$2" -C "$1" index blocker 2>/dev/null)
[ "$open" -gt 3 ] && more="+$((open - 3)) more"
[ "$upstream" -gt 0 ] && more="${more:+$more, }+${upstream} upstream"
[ -n "$more" ] && echo "- open: $more"
@@ -411,9 +419,8 @@ _memory_digest() { # <project-root>
if [ "$_ss_digest" = 1 ]; then
# phase 4d: the weekly /memory-feedback reminder, bounded, never fatal
_ss_mf="$(dirname "${BASH_SOURCE[0]}")/../lib/memory-feedback-remind.sh"
_ss_tmo=(); command -v timeout >/dev/null 2>&1 && _ss_tmo=(timeout 2)
[ -f "$_ss_mf" ] && { "${_ss_tmo[@]}" bash "$_ss_mf" 2>/dev/null || true; }
unset _ss_mf _ss_tmo
[ -f "$_ss_mf" ] && { "${_SS_TMO[@]}" bash "$_ss_mf" 2>/dev/null || true; }
unset _ss_mf
_ss_root=$(git rev-parse --show-toplevel 2>/dev/null) || _ss_root=$PWD
# A foreign repository (`gitflow.protect` false) gets no digest.
_ss_protect=$(git -C "$_ss_root" config --get gitflow.protect 2>/dev/null)
+11 -3
View File
@@ -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
View File
@@ -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>
+7 -3
View File
@@ -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"
+20
View File
@@ -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 ]
+50
View File
@@ -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 ]
+20 -1
View File
@@ -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 ]