You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 7346806
Browse filesBrowse the repository at this point in the historyBrowse files
Deny Bash writes to memory files unless the command only reads
- Gate any Bash command naming a memories path, including a symlinked dir's target, not only deletes
- Match memory paths instead of the bare word, so read-only commands mentioning it pass
- Drop the shared-memories layout from tests and document the rule in CLAUDE.md
Copy file name to clipboardExpand all lines: CLAUDE.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ Two harness details are load-bearing rather than incidental:
40
40
41
41
**One dispatcher, several registrations.**`hooks/kb-gate.mts` is registered in `techpack.yaml` at most once per event and installed file, and branches on `hook_event_name`, then on `tool_name`. mcs keys a registration on its command string, so a second component on the same event and destination overwrites the first in `settings.json`; `hookEventExists` stays green and doctor only warns about the lost matcher. Tools on one event therefore share a matcher, and a registration that needs different hook settings installs the same source under another name. A contract test asserts the pairs are unique. Matchers are broad on purpose; which agent types count as "discovery" is decided in exactly one place, `GATED_AGENTS`, and which skills authorize memory writes, and for how long, in `MEMORY_SKILL_SCOPES`. `hooks/sync-memories.mts` is likewise registered twice, on SessionStart and UserPromptSubmit.
42
42
43
-
**Memory writes are gated by skill, per turn.** A file-tool write into `.claude/memories/` is denied unless a memory skill authorizes it. A Bash `rm`, `unlink`, `mv` or `git rm` whose command mentions `memories` is denied unless memory-audit ran, because only the audit deletes, and only after per-batch approval. That check exists for auto mode, where no permission prompt would stop the command. It matches the whole command rather than parsing arguments, since variables, globs and `cd` make parsing easy to evade. The scope is the turn for capture because a session that loaded the skill turns earlier edits from a stale copy and skips its checks. A typed skill command produces no `Skill` tool call, only prompt text, which is why the `UserPromptSubmit` branch scans the prompt as well. This gate ignores `KB_GATE_MODE` except `off`, so its own per-turn denial budget is what keeps a broken `Skill` record from blocking writes forever.
43
+
**Memory writes are gated by skill, per turn.** A file-tool write into `.claude/memories/` is denied unless a memory skill authorizes it. A Bash command counts when it names a memories path: `.claude/memories`, a relative `memories/`, or the directory a symlinked `.claude/memories` resolves to. Under that test, `rm`, `unlink`, `mv` or `git rm` is denied unless memory-audit ran, because only the audit deletes, and only after per-batch approval. Any other command is denied outright, whatever skill ran, unless every segment is an allowlisted read with no redirect, heredoc or command substitution: a script can write a file in more ways than a denylist could name, and memory edits belong to Edit/Write under a skill. These checks exist for auto mode, where no permission prompt would stop the command. They match the whole command rather than parsing arguments, since variables, globs and `cd` make parsing easy to evade; the per-turn budget releases a misread command. The path test never names how a team shares memories. The scope is the turn for capture because a session that loaded the skill turns earlier edits from a stale copy and skips its checks. A typed skill command produces no `Skill` tool call, only prompt text, which is why the `UserPromptSubmit` branch scans the prompt as well. This gate ignores `KB_GATE_MODE` except `off`, so its own per-turn denial budget is what keeps a broken `Skill` record from blocking writes forever.
44
44
45
45
**Both hooks fail open.** Unexpected filesystem or payload errors exit 0 without output. The gate never exits 2, never calls qmd, and logs every evaluation except when disabled or when an uninteresting sub-agent starts. Preserve the append-only query and denial files, monotonic turn counter, and separate per-state/per-turn denial budgets. Gate state is compatible with previous shell installs.
// An allowlist of reads, because a script can write a file in more ways than a denylist could name.
31
+
constREAD_COMMANDS=newSet(
32
+
"cat head tail less grep egrep rg ugrep ls tree wc stat file diff cmp jq cut tr sort uniq nl column echo printf pwd cd test [ basename dirname realpath readlink shasum md5 sed find".split(
? "Memory files are written only through the continuous-learning or memory-audit skill, and continuous-learning has not been invoked in this turn. Invoke Skill(continuous-learning) now and run its search and pre-write checks before retrying. Working from a copy loaded in an earlier turn is what skips them."
324
-
: "Memory files are deleted or moved only by the memory-audit skill, after the user approves the batch. Run Skill(memory-audit) and follow its approval step before retrying; to correct a memory instead, edit it through Skill(continuous-learning).",
381
+
: bashPhase==="memory_write"
382
+
? "Memory files are changed only with Edit or Write, through Skill(continuous-learning) or Skill(memory-audit); a Bash command that may write to .claude/memories/ is denied even when one of them is active. If this command only reads, use the Read tool, or a plain read command (cat, head, grep, ls, sed -n, find, jq, read-only git) with no redirect, heredoc or command substitution."
383
+
: "Memory files are deleted or moved only by the memory-audit skill, after the user approves the batch. Run Skill(memory-audit) and follow its approval step before retrying; to correct a memory instead, edit it through Skill(continuous-learning).",
0 commit comments