Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(hooks): treat a shell glob's ** as recursive for capture exclusions

AkitaOnRails1bc90bbb3ce0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bbaa9d1b..8677ee7f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,9 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`include` globs names or could expand into an ignored path, and glob syntax
the matcher cannot evaluate makes it metadata-only. Gemini's
`list_directory` and the legacy `search_file_content` name are now search
- tools, dropped under an active policy like `grep_search` and `glob`. This
- applies to the native hook, the server, and the generated TypeScript
- integrations.
+ tools, dropped under an active policy like `grep_search` and `glob`. A shell
+ command whose `**` glob could recurse into an ignored directory (zsh, or bash
+ with `globstar`) is now dropped too. This applies to the native hook, the
+ server, and the generated TypeScript integrations. (GHSA-gcpc-88hm-3xqr)
- Fixed a web page open in the operator's browser being able to act on an
unauthenticated server (the loopback default, no token), such as claiming a
project's pending handoff or starting admin jobs. With auth off, the machine
diff --git a/crates/ai-memory-cli/src/commands/render_shared.rs b/crates/ai-memory-cli/src/commands/render_shared.rs
index 4b17ce98..d8e91d95 100644
--- a/crates/ai-memory-cli/src/commands/render_shared.rs
+++ b/crates/ai-memory-cli/src/commands/render_shared.rs
@@ -343,7 +343,7 @@ function captureMatchCommand(command: string | string[], config: CaptureConfig,
// drops when it could expand into an ignored path, and glob syntax the matcher
// cannot evaluate (classes, braces, extglobs, negation) fails closed.
function captureMatchGlobs(paths: string[], candidates: { path: string; windows: boolean }[], config: CaptureConfig): boolean | undefined { const budget = { work: 0 }; for (let i = 0; i < paths.length; i++) { if (/[\[\]{}()!]/.test(paths[i])) return undefined; const match = captureMatchCandidate(candidates[i], true, config, budget); if (match !== false) return match; } return false; }
-function captureMatchCandidate(candidate: { path: string; windows: boolean }, toolGlob: boolean, config: CaptureConfig, budget: { work: number }): boolean | undefined { const glob = /[*?]/.test(candidate.path); for (const pattern of config.patterns) { if (candidate.windows !== pattern.windows) continue; const insensitive = toolGlob || pattern.windows; const under = captureStartsWith(candidate.path, pattern.prefix, insensitive); if (!under && !glob) continue; if (under) { const directory = pattern.directory ? captureGlob(pattern.directory, candidate.path, insensitive, budget) : false; if (directory !== false) return directory; const match = captureGlob(pattern.path, candidate.path, insensitive, budget); if (match !== false) return match; } if (glob) { const reaches = toolGlob ? captureGlobstarReaches(candidate.path, pattern.prefix, insensitive, budget) : captureGlobReaches(candidate.path, pattern.prefix, insensitive, budget); if (reaches !== false) return reaches; } } return false; }
+function captureMatchCandidate(candidate: { path: string; windows: boolean }, toolGlob: boolean, config: CaptureConfig, budget: { work: number }): boolean | undefined { const glob = /[*?]/.test(candidate.path); for (const pattern of config.patterns) { if (candidate.windows !== pattern.windows) continue; const insensitive = toolGlob || pattern.windows; const under = captureStartsWith(candidate.path, pattern.prefix, insensitive); if (!under && !glob) continue; if (under) { const directory = pattern.directory ? captureGlob(pattern.directory, candidate.path, insensitive, budget) : false; if (directory !== false) return directory; const match = captureGlob(pattern.path, candidate.path, insensitive, budget); if (match !== false) return match; } if (glob) { const reaches = captureGlobstarReaches(candidate.path, pattern.prefix, insensitive, budget); if (reaches !== false) return reaches; } } return false; }
function captureGlobstarReaches(glob: string, prefix: string, insensitive: boolean, budget: { work: number }): boolean | undefined { const star = glob.indexOf("**"); if (star < 0) return captureGlobReaches(glob, prefix, insensitive, budget); const slash = glob.lastIndexOf("/", star); const head = slash < 0 ? "" : glob.slice(0, slash); const depth = head.split("/").filter(Boolean).length; let seen = 0; let offset = 0; for (const part of prefix.split("/")) { if (seen === depth) break; offset += part.length; if (part) seen++; if (seen < depth) offset++; } if (seen < depth) return captureGlobReaches(glob, prefix, insensitive, budget); return captureGlob(head, prefix.slice(0, offset), insensitive, budget); }
function captureTool(payload: Record<string, unknown>): { family: CaptureProtocol["tool_family"]; paths?: string[]; extraction: CaptureProtocol["extraction_state"]; callID?: string; command?: string | string[]; shell?: boolean; workdir?: string; globs?: boolean } { const name = typeof payload.tool === "string" ? payload.tool.toLowerCase() : ""; const args = payload.args as Record<string, unknown> | undefined; const call = ["tool_use_id","toolUseId","tool_call_id","toolCallId","call_id","callId","callID"].map((k) => payload[k]).find((v): v is string => typeof v === "string" && /^[A-Za-z0-9_.-]{1,128}$/.test(v)); if (["search","grep","glob","find","list","ls","list_files","read_dir","list_dir","grep_search","search_files","find_by_name","list_directory","search_file_content"].includes(name)) return { family: "search-list", extraction: "not-applicable", callID: call }; if (["bash","shell","shell_command","exec","execute","run_command","web_search","search_web","manage_task","manage_subagents","terminal","execute_bash","execute_cmd","powershell","run_shell_command"].includes(name)) { const workdir = ["workdir","dir_path","directory"].map((k) => args?.[k]).find((v) => v !== undefined); return { family: "non-file", extraction: "extracted", callID: call, command: captureShellCommand(args), shell: name !== "web_search", workdir: typeof workdir === "string" && workdir.trim() ? workdir : undefined }; } if (!["read","write","edit","apply_patch","notebookedit","notebook_edit","create_file","delete_file","rename_file","move_file","multi_edit","multiedit","replace","replace_all","read_many_files"].includes(name)) return { family: "unknown", extraction: "extracted", callID: call }; const direct = (o: any): string[] | undefined => { if (!o || typeof o !== "object") return undefined; const r: string[] = []; for (const k of ["file_path","filePath","path","absolute_path","AbsolutePath","notebook_path","TargetFile"]) if (k in o) { if (typeof o[k] !== "string") return undefined; r.push(o[k]); } if ("paths" in o) { if (!Array.isArray(o.paths) || o.paths.some((x: unknown) => typeof x !== "string")) return undefined; r.push(...o.paths); } return r.length && r.length <= CAPTURE_MAX_CANDIDATES ? r : undefined; }; let paths = direct(args); if (["multi_edit","multiedit","replace_all"].includes(name)) { const entries = args?.edits ?? args?.replacements; if (!Array.isArray(entries) || !entries.length || entries.length > CAPTURE_MAX_CANDIDATES) paths = undefined; else { paths = paths ?? []; for (const entry of entries) { const more = direct(entry); if (!more || paths.length + more.length > CAPTURE_MAX_CANDIDATES) { paths = undefined; break; } paths.push(...more); } } } if (name === "read_many_files" && args && "include" in args) { const include = args.include; if (!Array.isArray(include) || include.some((x: unknown) => typeof x !== "string") || (paths?.length ?? 0) + include.length > CAPTURE_MAX_CANDIDATES) paths = undefined; else paths = [...(paths ?? []), ...(include as string[])]; } if (!paths || !paths.length || paths.some((p) => !p.trim() || [...p].length > CAPTURE_MAX_PATH_CHARS)) return { family: "file", extraction: "missing-or-malformed", callID: call }; return { family: "file", paths, extraction: "extracted", callID: call, globs: name === "read_many_files" }; }
// An external lifecycle owner (`AI_MEMORY_CAPTURE_OWNER`, any value that is
diff --git a/crates/ai-memory-hooks/src/capture_policy.rs b/crates/ai-memory-hooks/src/capture_policy.rs
index 9a2f27b7..50ef3b27 100644
--- a/crates/ai-memory-hooks/src/capture_policy.rs
+++ b/crates/ai-memory-hooks/src/capture_policy.rs
@@ -724,11 +724,10 @@ impl CapturePolicy {
if *work > MAX_MATCH_WORK {
return Err(());
}
- let reaches = if tool_glob {
- globstar_reaches
- } else {
- glob_reaches
- };
+ // Treat `**` as recursive for shell globs too: bash leaves it a
+ // single segment, but zsh (and bash with `globstar`) recurse, and
+ // an over-eager drop is the safe side of a capture exclusion.
+ let reaches = globstar_reaches;
if (under_prefix
&& glob_match(
&pattern.path,
@@ -2383,6 +2382,24 @@ mod tests {
json!({"tool_name": "Bash", "tool_input": {"command": command}, "tool_use_id": "call-1"})
}
+ /// zsh, and bash with `globstar`, recurse on `**`, so a shell glob whose
+ /// `**` can descend into an ignored directory drops the event; a `**`
+ /// rooted elsewhere is still kept (control).
+ #[test]
+ fn recursive_shell_globs_cannot_reach_ignored_paths() {
+ let policy = shell_policy();
+ for command in ["cat **/*.md", "cat **/0001.md", "cat docs/**/0001.md"] {
+ let decision = policy.inspect(AgentKind::ClaudeCode, &bash(command), "/repo");
+ assert_eq!(
+ decision.protocol().disposition(),
+ CaptureDisposition::Drop,
+ "{command}"
+ );
+ }
+ let decision = policy.inspect(AgentKind::ClaudeCode, &bash("cat src/**/main.rs"), "/repo");
+ assert_eq!(decision.protocol().disposition(), CaptureDisposition::Keep);
+ }
+
/// The drop/keep tables live in the shared fixture so the generated
/// TypeScript matcher runs the very same vectors (#961).
#[test]
@@ -2751,7 +2768,7 @@ mod tests {
// Without `**` it is exactly `glob_reaches`.
assert!(!globstar_reaches("/repo/*.md", "/repo/docs/adr", false));
assert!(globstar_reaches("/repo/*/adr/x", "/repo/docs/adr", false));
- // A shell glob does not recurse: `glob_reaches` keeps that reading.
+ // `glob_reaches` alone does not recurse; matching uses `globstar_reaches`.
assert!(!glob_reaches("/repo/**/*.md", "/repo/docs/adr", false));
}
diff --git a/crates/ai-memory-hooks/tests/fixtures/capture-policy.json b/crates/ai-memory-hooks/tests/fixtures/capture-policy.json
index 8879855b..06be443b 100644
--- a/crates/ai-memory-hooks/tests/fixtures/capture-policy.json
+++ b/crates/ai-memory-hooks/tests/fixtures/capture-policy.json
@@ -85,6 +85,9 @@
{"command":"cat /{root}/docs/adr/x.md","disposition":"drop"},
{"command":"cat ../docs/adr/0001.md","cwd":"sub","disposition":"drop"},
{"command":"cat docs/adr/*.md","disposition":"drop"},
+ {"command":"cat docs/**/0001.md","disposition":"drop"},
+ {"command":"cat **/*.md","disposition":"drop"},
+ {"command":"cat src/**/main.rs","disposition":"keep"},
{"command":"cat docs/*/0001.md","disposition":"drop"},
{"command":"ls docs/*","disposition":"drop"},
{"command":"cat 'docs/adr/a b.md'","disposition":"drop"},