From 9045a60f11c6d8f44144cb35d2b0536b6e85e549 Mon Sep 17 00:00:00 2001 From: bchanot Date: Fri, 13 Nov 2026 22:59:50 +0100 Subject: [PATCH] fix(scripts): close the BUGS-FOUND script items, fail closed on bad reads profile-state.jq sanitizes printed values and shape-checks plugin ids (no forged plugin: line); the digest prints open: none or unknown instead of nothing; push-guard deny_lost checks every pipe stage, T63 split into ask and deny lists; prune-memory mktemp guarded. --- hooks/session-start.sh | 7 +++-- lib/profile-state.jq | 16 ++++++---- lib/tests/curated-config-guard.test.sh | 6 ++++ lib/tests/profile.test.sh | 13 +++++++- lib/tests/push-guard.test.sh | 30 ++++++++++++++----- lib/tests/session-digest.test.sh | 14 ++++++--- skills/prune-memory/SKILL.md | 4 +-- .../prune-memory/tests/run-deterministic.sh | 3 +- 8 files changed, 70 insertions(+), 23 deletions(-) diff --git a/hooks/session-start.sh b/hooks/session-start.sh index 5d9ae27..1653902 100644 --- a/hooks/session-start.sh +++ b/hooks/session-start.sh @@ -316,7 +316,9 @@ _digest_cut() { # Open BLK (≤ 3 listed, the rest counted) and upstream count, as digest lines. _digest_blockers() { # - local row line rest status title open=0 upstream=0 more="" + local row line rest status title open=0 upstream=0 more="" rows + rows=$("${_SS_TMO[@]}" bash "$2" -C "$1" index blocker 2>/dev/null) || { + echo "- open: unknown (memory.sh index failed)"; return 0; } while IFS= read -r row; do [[ "$row" == "| BLK-"* ]] || continue line="${row#| }"; line="${line% |}" @@ -328,7 +330,8 @@ _digest_blockers() { # [ "$open" -le 3 ] && echo "- open: ${line%% | *} $title" ;; upstream) upstream=$((upstream + 1)) ;; esac - done < <("${_SS_TMO[@]}" bash "$2" -C "$1" index blocker 2>/dev/null) + done <<< "$rows" + [ "$open" -eq 0 ] && echo "- open: none" [ "$open" -gt 3 ] && more="+$((open - 3)) more" [ "$upstream" -gt 0 ] && more="${more:+$more, }+${upstream} upstream" [ -n "$more" ] && echo "- open: $more" diff --git a/lib/profile-state.jq b/lib/profile-state.jq index cac974c..974f0d1 100644 --- a/lib/profile-state.jq +++ b/lib/profile-state.jq @@ -14,6 +14,8 @@ def is_pack: startswith("pack-") and endswith("@skills-dir"); def short: sub("@skills-dir$"; ""); def src_word($s): if $s.source == "flag" then "conv \($s.flag)" else $s.source end; +def clean: tostring | gsub("[\n\r]"; " "); +def plugin_id: test("\\A[A-Za-z0-9._-]+@[A-Za-z0-9._-]+\\z"); def listing($plugins; $on): [$plugins | to_entries[] | select(.value == $on) | .key | short] | if length == 0 then "none" else join(" ") end; @@ -50,15 +52,17 @@ def listing($plugins; $on): model: $model, model_source: $msrc, mcp_off: $off} as $s | if $mode == "json" then $s elif $mode == "plain" then - ( ["profile=\($s.name)", "source=\($s.source)", "flag=\($s.flag)"] + ( ["profile=\($s.name | clean)", "source=\($s.source)", + "flag=\($s.flag | clean)"] + [$plugins | to_entries | sort_by(.key)[] | select(.key | is_pack) - | "\(.key | short)=\(.value | onoff)"] + | "\(.key | short | clean)=\(.value | onoff)"] + [$plugins | to_entries | sort_by(.key)[] - | select(.key | is_pack | not) | "plugin:\(.key)=\(.value | onoff)"] - + [$denied[] | "connector:\(.)=denied"] + | select(.key | is_pack | not) | select(.key | plugin_id) + | "plugin:\(.key)=\(.value | onoff)"] + + [$denied[] | "connector:\(. | clean)=denied"] + (if $all then ["connector:*=denied"] else [] end) - + ["model=\($s.model)", "model-source=\($s.model_source)", - "mcp-off=\($s.mcp_off)"] ) | .[] + + ["model=\($s.model | clean)", "model-source=\($s.model_source)", + "mcp-off=\($s.mcp_off | clean)"] ) | .[] else ( [ (if $s.mcp_off != "-" then "profile: custom (off: \($s.mcp_off)) [\(src_word($s))]" diff --git a/lib/tests/curated-config-guard.test.sh b/lib/tests/curated-config-guard.test.sh index 279a61b..5b23d15 100644 --- a/lib/tests/curated-config-guard.test.sh +++ b/lib/tests/curated-config-guard.test.sh @@ -78,4 +78,10 @@ if grep -qi 'mktemp failed' "$ERR6"; then r6msg=yes; else r6msg=no; fi check T6-mktemp-failure-loud "$r6msg" yes rm -f "$ERR6" "$SUT2" +# T7: Step 7 keeps `graphify claude install` inside the `(cd "$REPO" && …)` +# subshell, so the guard's CLAUDE.md block lands in the repo it protects. +if grep -qF "(cd \"\$REPO\" && graphify claude install)" "$INSTALL_SH"; then + r7=yes; else r7=no; fi +check T7-step7-graphify-subshell-lock "$r7" yes + printf 'PASS=%s FAIL=%s\n' "$pass" "$fail"; [ "$fail" -eq 0 ] diff --git a/lib/tests/profile.test.sh b/lib/tests/profile.test.sh index 5196b31..2eaac24 100755 --- a/lib/tests/profile.test.sh +++ b/lib/tests/profile.test.sh @@ -120,7 +120,7 @@ out="$(P diff fullx)"; check_has T17-diff-ui "$out" "ui@mk: off → on" check_has T17-diff-conn "$out" "connectors denied: claude.ai X, claude.ai Y → " out="$(P diff devx)"; check T17-nodiff "$out" "no difference from devx" -# shellcheck + width +# lint: shellcheck + width check T18-shellcheck "$(shellcheck "$ROOT/lib/profile.sh" >/dev/null 2>&1 && echo ok)" ok check T18-width "$(LC_ALL=C awk 'length($0) > 80' "$ROOT/lib/profile.sh" "$ROOT/lib/profile-state.jq" | wc -l | tr -d ' ')" 0 @@ -185,6 +185,17 @@ check T27-home-root-rc "$rc" 2 check_has T27-home-root-msg "$out" "refusing \$HOME as the project root" check T27-home-root-nothing "$([ -e "$H3/.claude" ] && echo leaked)" "" +# a newline in a settings value never starts a forged plain line +printf '{"model": "opus\\nplugin:evil@x=on"}\n' > "$FX/proj/.claude/settings.json" +out="$(P current --plain)" +check T28-forge-no-line "$(printf '%s\n' "$out" | grep -c '^plugin:evil@')" 0 +check T28-forge-kept-inline "$(printf '%s\n' "$out" | grep -c '^model=opus plugin:evil@x=on$')" 1 +printf '{"enabledPlugins": {"ok@x\\n": true, "pack-a\\nb=on@skills-dir": true}}\n' > "$FX/lib/profiles/nlx.json" +out="$(P current --plain)" +check T28-nl-id-no-bare-on "$(printf '%s\n' "$out" | grep -c '^=on$')" 0 +check T28-nl-id-no-plugin-line "$(printf '%s\n' "$out" | grep -c '^plugin:ok@x')" 0 +rm "$FX/lib/profiles/nlx.json" + # timing: two jq calls, under 100 ms here (spec target 50 ms, measured in Task 12) t0=$(date +%s%N); P current --plain >/dev/null; t1=$(date +%s%N) ms=$(( (t1 - t0) / 1000000 )); echo "current --plain: ${ms} ms" diff --git a/lib/tests/push-guard.test.sh b/lib/tests/push-guard.test.sh index fa84602..11acf7b 100644 --- a/lib/tests/push-guard.test.sh +++ b/lib/tests/push-guard.test.sh @@ -358,6 +358,10 @@ deny_lost() { # : base deny entries missing, minus the list | jq -r --slurpfile now "$1" '.permissions.deny[] | select(. as $e | ($now[0].permissions.deny | index($e)) == null)' \ | grep -vxF -f "$WORK/removed-ok" + local st=("${PIPESTATUS[@]}") + # grep rc 1 = nothing left to report; any other failure is a lost check + { [ "${st[0]}" -eq 0 ] && [ "${st[1]}" -eq 0 ] && [ "${st[2]}" -le 1 ]; } || + echo "deny_lost: pipeline failed (rc ${st[*]})" } if [ -z "$base" ]; then echo "SKIP T42 (no main ref)" @@ -371,6 +375,8 @@ else # T42c: the lock still bites on an entry that is not in the list probe=$(printf '%s\n' "$basedeny" | grep -vxF -f "$WORK/removed-ok" | head -1) jq --arg e "$probe" '.permissions.deny -= [$e]' "$S" > "$WORK/s-probe.json" + check T42c-probe-written "$(jq -e . "$WORK/s-probe.json" >/dev/null 2>&1 \ + && echo ok || echo bad)" ok check T42c-lock-bites \ "$(deny_lost "$WORK/s-probe.json" | grep -cxF -- "$probe")" 1 # T42d: the user's hand edit (the 6 dead entries out) is forgiven @@ -379,6 +385,9 @@ else "Write(~/.gitconfig)", "Write(~/.config/git/config)"]' "$S" \ > "$WORK/s-after.json" check T42d-edit-forgiven "$(deny_lost "$WORK/s-after.json")" "" + # T42f: a missing settings file is a failed check, not an empty loss list + check T42f-jq-failure-reported \ + "$(deny_lost "$WORK/absent.json" 2>/dev/null | grep -c 'pipeline failed')" 1 fi soft=$(jq -r '.autoMode.soft_deny[]' "$S") @@ -419,17 +428,22 @@ twins_missing() { # twins_missing -> missing twin rules done } check T62-twins-present "$(twins_missing "$S")" "" -T62F="$(mktemp)" +T62F="$WORK/s-twin.json" jq '.permissions.deny += ["Bash(cat */ghost.key)"]' "$S" > "$T62F" check T62b-flip-missing-twin "$(twins_missing "$T62F" | /usr/bin/grep -F ghost.key | sort | paste -sd' ' -)" "Bash(*/rtk read */ghost.key*) Bash(rtk read */ghost.key*)" -rm -f "$T62F" -# T63 phase 2 rules present (adding is free; a removal is the user's edit) +# T63 phase 2 rules present (adding is free; a removal is the user's edit): +# each row in the list where it sits today, so a deny row demoted to ask +# (or the reverse) fails missing="" -while IFS= read -r e; do - jq -e --arg e "$e" '(.permissions.ask + .permissions.deny) | index($e)' \ - "$S" >/dev/null || missing="$missing [$e]" -done <<'EOF' +rules_present() { # rules_present : stdin rows -> $missing + local e + while IFS= read -r e; do + jq -e --arg e "$e" --arg l "$1" '.permissions[$l] | index($e)' \ + "$S" >/dev/null || missing="$missing [$1:$e]" + done +} +rules_present ask <<'EOF' Bash(git merge *) Bash(bash *gitflow.sh finish*) Bash(sh *gitflow.sh finish*) @@ -446,6 +460,8 @@ Bash(/usr/bin/bash *) Bash(/usr/bin/sh *) Bash(/usr/bin/env bash*) Bash(/usr/bin/env sh*) +EOF +rules_present deny <<'EOF' Bash(awk *system(*) Bash(head * .env*) Bash(tail * .env*) diff --git a/lib/tests/session-digest.test.sh b/lib/tests/session-digest.test.sh index af322a0..9702bd5 100644 --- a/lib/tests/session-digest.test.sh +++ b/lib/tests/session-digest.test.sh @@ -199,18 +199,24 @@ printf '#!/usr/bin/env bash\nexit 2\n' > "$WORK/sd9/lib/memory.sh" out=$(run "$p" - "$S9"); rc=$? check SD9a-exit2-rc "$rc" 0 check SD9a-banner "$(printf '%s' "$out" | grep -c 'make doctor')" 1 -check SD9a-no-digest "$(printf '%s' "$out" | grep -c 'memory digest:')" 0 +check SD9a-unknown "$(printf '%s\n' "$out" | grep -c -x -- '- open: unknown (memory.sh index failed)')" 1 printf '#!/usr/bin/env bash\nprintf "| BLK-1 |\\n| BLK-2 | x\\n| BLK-3 | d | t | weird |\\ngarbage\\n"\n' \ > "$WORK/sd9/lib/memory.sh" out=$(run "$p" - "$S9"); rc=$? check SD9b-malformed-rc "$rc" 0 check SD9b-banner "$(printf '%s' "$out" | grep -c 'make doctor')" 1 -check SD9b-no-digest "$(printf '%s' "$out" | grep -c 'memory digest:')" 0 +check SD9b-none "$(printf '%s\n' "$out" | grep -c -x -- '- open: none')" 1 p=$(mkproj sd9d); mkdir "$p/.claude/memory/blockers.md" out=$(run "$p" -); rc=$? check SD9c-dir-rc "$rc" 0 check SD9c-banner "$(printf '%s' "$out" | grep -c 'make doctor')" 1 -check SD9c-no-digest "$(printf '%s' "$out" | grep -c 'memory digest:')" 0 +check SD9c-none "$(printf '%s\n' "$out" | grep -c -x -- '- open: none')" 1 + +# SD9d: a successful read with zero open rows says so +p=$(mkproj sd9e); blk "$p" 1 resolved "Done" +check SD9d-none "$(run "$p" - | digest | grep -c -x -- '- open: none')" 1 +blk "$p" 2 open "Now open" +check SD9d-not-none "$(run "$p" - | digest | grep -c -x -- '- open: none')" 0 # SD10: PATH without jq, piped resume -> no digest, banner intact bin="$WORK/nojq"; mkdir -p "$bin" @@ -246,7 +252,7 @@ t1=$(now_ms) check SD12-hang-bounded "$([ $((t1 - t0)) -lt 3500 ] && echo yes || echo no)" yes check SD12-rc "$rc" 0 check SD12-banner "$(printf '%s' "$out" | grep -c 'make doctor')" 1 -check SD12-no-digest "$(printf '%s' "$out" | grep -c 'memory digest:')" 0 +check SD12-unknown "$(printf '%s\n' "$out" | grep -c -x -- '- open: unknown (memory.sh index failed)')" 1 # SD16: a memory.sh whose `check` hangs costs under 3.5 s, exit 0 printf '#!/usr/bin/env bash\ncase "$*" in *check*) sleep 10 ;; esac\n' \ diff --git a/skills/prune-memory/SKILL.md b/skills/prune-memory/SKILL.md index 58ea5f3..7580496 100644 --- a/skills/prune-memory/SKILL.md +++ b/skills/prune-memory/SKILL.md @@ -31,8 +31,8 @@ Operates on `.claude/memory/` in the current project (CWD). Curates the - **Git is the backup.** Skill writes in-place. PRECHECK refuses to run if working tree dirty on registry files. - **Append-only friendly.** Marks entries with a `superseded by ` or - `deprecated` status written by `memory.sh status` instead of deleting. Body of old entry stays - for history. + `deprecated` status written by `memory.sh status` instead of deleting. + Body of old entry stays for history. - **IDs stable.** Never renumber. Merges create a new ID; sources keep their ID with superseded status. - **Caveman style enforced.** Per CLAUDE.md memory rule, all writes are diff --git a/skills/prune-memory/tests/run-deterministic.sh b/skills/prune-memory/tests/run-deterministic.sh index f97edb3..016ca62 100644 --- a/skills/prune-memory/tests/run-deterministic.sh +++ b/skills/prune-memory/tests/run-deterministic.sh @@ -11,7 +11,8 @@ set -uo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SKILL="${SKILL:-$HERE/../SKILL.md}" -SANDBOX="$(mktemp -d "${TMPDIR:-/tmp}/prune-red.XXXXXX")" +SANDBOX="$(mktemp -d "${TMPDIR:-/tmp}/prune-red.XXXXXX")" || exit 1 +[ -n "$SANDBOX" ] || exit 1 trap 'rm -rf "$SANDBOX"' EXIT fail=0