fix(project-rules): physical parent check, emit write guard, extract keeps both copies
_rules_out_path_ok resolves the first existing ancestor with cd -P and refuses an out that escapes the project; emit goes through it and checks mkdir and the write; an extract rewrite failure keeps tmp and out and names both. Callers (onboarder, scaffolder) stop on any rc 2 but exists.
This commit is contained in:
1 parent
4e71b1af64
commit
9d5530af89
4 files changed
+83
-24
No files matched your search
+4
-2
@@ -141,7 +141,8 @@ bash ~/.claude/lib/project-rules.sh -C <PROJECT_ROOT> emit ~/.claude/templates/p
|
||||
`build_cmd`, `testing` layout from `test_cmd`); never duplicate the
|
||||
always-on Build/Test sections.
|
||||
- rc 1 → no match, nothing written, skip silently.
|
||||
- rc 2 → the file exists: keep it, never overwrite.
|
||||
- rc 2 whose stderr line ends with ` exists` → keep the file, never
|
||||
overwrite; any other rc 2 → STOP, relay the lib's line.
|
||||
Then `bash ~/.claude/lib/project-rules.sh -C <PROJECT_ROOT> check` must print
|
||||
`N rule(s) ok`. The `!.claude/rules/` negation comes from the gitflow socle
|
||||
at /onboard STEP 2.6 (`gitflow init` reconciles it); PHASE 4 stays as is.
|
||||
@@ -252,7 +253,8 @@ rc ≠ 0 (should not happen after S0) → ROLLBACK: `git -C <PROJECT_ROOT>
|
||||
reset -q`, `git -C <PROJECT_ROOT> checkout -- CLAUDE.md` (plus
|
||||
`.gitignore` when S0 appended to it), delete every
|
||||
`.claude/rules/<name>.md` this run wrote (literal paths, one `rm` per
|
||||
row), then STOP with the row and the lib's message.
|
||||
row; a row that failed at the rewrite step also leaves a tmp file named
|
||||
on stderr: delete it too), then STOP with the row and the lib's message.
|
||||
|
||||
S2 — POINTER. If CLAUDE.md has no `## Path-scoped rules` section, append:
|
||||
```markdown
|
||||
|
||||
@@ -87,7 +87,9 @@ bash ~/.claude/lib/project-rules.sh -C <PROJECT_ROOT> emit ~/.claude/templates/p
|
||||
rc 0 → fill its stubs from the DESIGN (image, compose services, test
|
||||
layout, pipeline jobs), no placeholders left; rc 1 → nothing matched,
|
||||
skipped (a plain scaffold yields `docker.md` when Docker is relevant, and
|
||||
`testing.md` only when the stack scaffolds a test file). Then
|
||||
`testing.md` only when the stack scaffolds a test file); rc 2 whose stderr
|
||||
line ends with ` exists` → keep the file, any other rc 2 → STOP, relay the
|
||||
lib's line. Then
|
||||
`bash ~/.claude/lib/project-rules.sh -C <PROJECT_ROOT> check` prints
|
||||
`N rule(s) ok`. The `.gitignore` negation `!.claude/rules/` comes from the
|
||||
gitflow socle at STEP 5f (`gitflow init` reconciles it before the root
|
||||
|
||||
+38
-19
@@ -186,17 +186,33 @@ _rules_globs_all_match() { # <globs-json> → rc 0 valid and every glob matches
|
||||
done <<< "$globs"
|
||||
}
|
||||
|
||||
_rules_out_path_ok() { # <out> → rc 2 when absolute, has a .. part, or exists
|
||||
# shortcut: lexical only, a symlinked component is not resolved (no
|
||||
# portable realpath -m); upgrade = resolve the parent dir with cd -P
|
||||
case "$1" in
|
||||
/*) echo "project-rules: out must be relative: $1" >&2; return 2 ;;
|
||||
_rules_out_path_ok() { # <out> → rc 2 when absolute, has a .. part, exists,
|
||||
# or resolves outside cwd
|
||||
local out="$1" here parent phys
|
||||
case "$out" in
|
||||
/*) echo "project-rules: out must be relative: $out" >&2; return 2 ;;
|
||||
..|../*|*/..|*/../*)
|
||||
echo "project-rules: out must not contain ..: $1" >&2; return 2 ;;
|
||||
echo "project-rules: out must not contain ..: $out" >&2; return 2 ;;
|
||||
esac
|
||||
if [ -e "$1" ] || [ -L "$1" ]; then
|
||||
echo "project-rules: $1 exists" >&2; return 2
|
||||
if [ -e "$out" ] || [ -L "$out" ]; then
|
||||
echo "project-rules: $out exists" >&2; return 2
|
||||
fi
|
||||
here=$(pwd -P) && [ -n "$here" ] \
|
||||
|| { echo "project-rules: cannot resolve cwd" >&2; return 2; }
|
||||
parent=$(dirname "$out")
|
||||
while [ ! -d "$parent" ]; do parent=$(dirname "$parent"); done
|
||||
phys=$(CDPATH='' cd -P -- "./$parent" 2>/dev/null && pwd -P) \
|
||||
|| { echo "project-rules: cannot resolve $parent" >&2; return 2; }
|
||||
case "$phys" in
|
||||
"${here%/}"|"${here%/}"/*) ;;
|
||||
*) echo "project-rules: out must stay under the project: $out" >&2
|
||||
return 2 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
_rules_mkdir_parent() { # <out> → rc 2 when its directory cannot be created
|
||||
mkdir -p "$(dirname "$1")" 2>/dev/null \
|
||||
|| { echo "project-rules: cannot create $(dirname "$1")" >&2; return 2; }
|
||||
}
|
||||
|
||||
project_rules_extract() { # <file> <heading> <out.md> <globs-json>
|
||||
@@ -207,20 +223,22 @@ project_rules_extract() { # <file> <heading> <out.md> <globs-json>
|
||||
return 2; }
|
||||
_rules_out_path_ok "$out" || return 2
|
||||
_rules_globs_all_match "$globs" || return 2
|
||||
mkdir -p "$(dirname "$out")" \
|
||||
|| { echo "project-rules: cannot create $(dirname "$out")" >&2; return 2; }
|
||||
_rules_mkdir_parent "$out" || return 2
|
||||
tmp=$(mktemp) || { echo "project-rules: no temp file" >&2; return 2; }
|
||||
{ printf -- '---\npaths: %s\n---\n\n# %s\n' "$globs" "${hline#* }" \
|
||||
&& _rules_section_body "$src" "$hline"; } > "$out" \
|
||||
|| { rm -f "$tmp" "$out"
|
||||
echo "project-rules: $out not written, $src untouched" >&2
|
||||
return 2; }
|
||||
if ! { _rules_without_section "$src" "$hline" > "$tmp" \
|
||||
&& cat "$tmp" > "$src"; }; then
|
||||
rm -f "$tmp" "$out"
|
||||
echo "project-rules: $src not rewritten, $out removed" >&2
|
||||
return 2
|
||||
fi
|
||||
_rules_without_section "$src" "$hline" > "$tmp" \
|
||||
|| { rm -f "$tmp" "$out"
|
||||
echo "project-rules: $src untouched, $out removed" >&2
|
||||
return 2; }
|
||||
cat "$tmp" > "$src" || {
|
||||
echo "project-rules: $src rewrite failed: rewritten text at $tmp," \
|
||||
"section at $out. Complete with: cat $tmp > $src;" \
|
||||
"or abort: rm $out when $src is intact" >&2
|
||||
return 2; }
|
||||
rm -f "$tmp"
|
||||
echo "project-rules: moved '$hline' to $out"
|
||||
}
|
||||
@@ -233,10 +251,11 @@ project_rules_emit() { # <skeleton.md> <out.md> → the rule, matching globs on
|
||||
|| { echo "project-rules: $1: no valid paths: array" >&2; return 2; }
|
||||
kept=$(_rules_matching "$globs") \
|
||||
|| { echo "project-rules: $1: no glob matches, not emitted" >&2; return 1; }
|
||||
[ -e "$2" ] && { echo "project-rules: $2 exists" >&2; return 2; }
|
||||
mkdir -p "$(dirname "$2")"
|
||||
_rules_out_path_ok "$2" || return 2
|
||||
_rules_mkdir_parent "$2" || return 2
|
||||
{ printf -- '---\npaths: %s\n---\n' "$kept"
|
||||
awk 'body { print } NR > 1 && $0 == "---" { body = 1 }' "$1"; } > "$2"
|
||||
awk 'body { print } NR > 1 && $0 == "---" { body = 1 }' "$1"; } > "$2" \
|
||||
|| { rm -f "$2"; echo "project-rules: $2 not written" >&2; return 2; }
|
||||
echo "project-rules: emitted $2 paths: $kept"
|
||||
}
|
||||
|
||||
|
||||
@@ -165,11 +165,18 @@ TMPDIR=/nonexistent bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/beta.md '[
|
||||
check T9e-no-tmp-rc "$?" 2
|
||||
check T9f-no-tmp-nothing-written "$([ -e .claude/rules/beta.md ] && echo yes || echo no)" no
|
||||
check T9g-no-tmp-beta-still-there "$(grep -c '^## Beta' CLAUDE.md)" 1
|
||||
# a failed rewrite after the truncation keeps the rewritten text and the rule
|
||||
mkdir -p "$FIX/tmp"; cp CLAUDE.md "$FIX/src.before"
|
||||
chmod a-w CLAUDE.md
|
||||
bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/beta.md '["src/**"]' 2>/dev/null
|
||||
err=$(TMPDIR="$FIX/tmp" bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/beta.md '["src/**"]' 2>&1 >/dev/null)
|
||||
check T9h-unwritable-src-rc "$?" 2
|
||||
check T9i-unwritable-src-rule-removed "$([ -e .claude/rules/beta.md ] && echo yes || echo no)" no
|
||||
kept=$(printf '%s\n' "$err" | sed -n 's/.*rewritten text at \([^,]*\),.*/\1/p')
|
||||
check T9h2-unwritable-src-tmp-named "$(case "$kept" in "$FIX"/tmp/*) [ -e "$kept" ] && echo yes || echo no ;; *) echo no ;; esac)" yes
|
||||
check T9h3-unwritable-src-tmp-rewritten "$(grep -c '^## Beta' "$kept")" 0
|
||||
check T9i-unwritable-src-rule-kept "$([ -e .claude/rules/beta.md ] && echo yes || echo no)" yes
|
||||
check T9h4-unwritable-src-untouched "$(cmp -s CLAUDE.md "$FIX/src.before" && echo yes || echo no)" yes
|
||||
chmod u+w CLAUDE.md
|
||||
rm -f .claude/rules/beta.md "$FIX"/tmp/tmp.*
|
||||
check T9j-unwritable-src-beta-still-there "$(grep -c '^## Beta' CLAUDE.md)" 1
|
||||
|
||||
# T9k a mid-file H1 ends the section (foreign CLAUDE.md with several parts)
|
||||
@@ -229,6 +236,35 @@ check T9z-write-fails-beta-kept "$(grep -c '^## Beta' CLAUDE.md)" 1
|
||||
check T9z2-write-fails-no-rule "$([ -e .claude/rules/ro/beta.md ] && echo yes || echo no)" no
|
||||
chmod u+w .claude/rules/ro
|
||||
|
||||
# T9z3 a symlinked .claude/rules resolving outside the project is refused by
|
||||
# extract and emit; a symlinked -C dir stays accepted
|
||||
mkdir -p "$FIX/p-esc/.claude" "$FIX/out-esc" "$FIX/p-esc/src"
|
||||
ln -s "$FIX/out-esc" "$FIX/p-esc/.claude/rules"
|
||||
cp CLAUDE.md "$FIX/p-esc/CLAUDE.md"; echo x > "$FIX/p-esc/src/a.c"
|
||||
echo x > "$FIX/p-esc/Dockerfile"
|
||||
cd "$FIX/p-esc" || exit 1
|
||||
err=$(bash "$PR" extract CLAUDE.md 'Beta' .claude/rules/beta.md '["src/**"]' 2>&1 >/dev/null)
|
||||
check T9z3-escape-extract-rc "$?" 2
|
||||
check T9z4-escape-extract-reason "$(printf '%s\n' "$err" | grep -c 'must stay under the project')" 1
|
||||
err=$(bash "$PR" emit "$ROOT/templates/project-rules/docker.md" .claude/rules/docker.md 2>&1 >/dev/null)
|
||||
check T9z5-escape-emit-rc "$?" 2
|
||||
check T9z6-escape-emit-reason "$(printf '%s\n' "$err" | grep -c 'must stay under the project')" 1
|
||||
check T9z7-escape-target-empty "$(find "$FIX/out-esc" -mindepth 1 | wc -l | tr -d ' ')" 0
|
||||
cd "$FIX" || exit 1
|
||||
mkdir -p "$FIX/p-ok" && echo x > "$FIX/p-ok/Dockerfile" && ln -s "$FIX/p-ok" "$FIX/link-esc"
|
||||
bash "$PR" -C "$FIX/link-esc" emit "$ROOT/templates/project-rules/docker.md" .claude/rules/docker.md >/dev/null
|
||||
check T9z8-symlinked-C-emit-rc "$?" 0
|
||||
cd "$FIX/p-ok" || exit 1
|
||||
mkdir -p .claude/rules && touch .claude/rules/notadir
|
||||
err=$(bash "$PR" emit "$ROOT/templates/project-rules/docker.md" .claude/rules/notadir/x.md 2>&1 >/dev/null)
|
||||
check T9z9-emit-mkdir-fails-rc "$?" 2
|
||||
check T9z10-emit-mkdir-fails-reason "$(printf '%s\n' "$err" | grep -c 'cannot create')" 1
|
||||
err=$(bash "$PR" emit "$ROOT/templates/project-rules/docker.md" .claude/rules/x.md/ 2>&1 >/dev/null)
|
||||
check T9z11-emit-write-fails-rc "$?" 2
|
||||
check T9z12-emit-write-fails-reason "$(printf '%s\n' "$err" | grep -c 'not written')" 1
|
||||
check T9z13-emit-write-fails-no-file "$([ -e .claude/rules/x.md ] && echo yes || echo no)" no
|
||||
cd "$FIX" || exit 1
|
||||
|
||||
# T10 the hook appends one jq line, prints nothing, never fails
|
||||
HOOK="$ROOT/hooks/instructions-loaded.sh"
|
||||
LOG="$FIX/il.jsonl"
|
||||
|
||||
Reference in new issue
Block a user