Skip to content

fix(mcp): bound unbounded regex cache in name_referenced_in - #6296

Merged
bug-ops merged 1 commit into
mainfrom
fix/6255-unbounded-regex-cache-mcp
Jul 14, 2026
Merged

fix(mcp): bound unbounded regex cache in name_referenced_in#6296
bug-ops merged 1 commit into
mainfrom
fix/6255-unbounded-regex-cache-mcp

Conversation

@bug-ops

@bug-ops bug-ops commented Jul 14, 2026

Copy link
Copy Markdown
Owner

Summary

  • name_referenced_in in crates/zeph-mcp/src/sanitize.rs memoized a compiled Regex per distinct lowercased MCP tool name in two process-lifetime static caches with no eviction; since tool names are attacker-influenced (untrusted MCP server input), a malicious or reconnecting server rotating its advertised tool names could grow both caches without bound, leaking memory over the lifetime of a long-running daemon/gateway/serve process.
  • Replaced both HashMap<String, Regex> caches with lru::LruCache<String, Regex> capped at 256 entries (NAME_REGEX_CACHE_CAPACITY), using get_or_insert as a drop-in replacement for entry().or_insert_with() with the same single-lock-acquisition semantics — no new attack surface, no TOCTOU.
  • lru was already a workspace dependency (0.18.0, used identically in crates/zeph-memory/src/graph/ontology.rs), so this adds lru.workspace = true to crates/zeph-mcp/Cargo.toml with no new crate/version.
  • Added a regression test (name_referenced_in_cache_bounded_survives_churn) that churns well past the cap on both cache branches and confirms a long-evicted entry still matches correctly on recompile.

Closes #6255

Test plan

  • cargo +nightly fmt --check
  • cargo clippy --profile ci --workspace --all-targets --features "desktop,ide,server,chat,pdf,scheduler,testing" -- -D warnings
  • cargo nextest run --config-file .github/nextest.toml --workspace --features "desktop,ide,server,chat,pdf,scheduler" --lib --bins (13694 passed, 0 failed, 34 skipped)
  • RUSTFLAGS="-D warnings" RUSTDOCFLAGS="--deny rustdoc::broken_intra_doc_links" cargo doc --no-deps --workspace --features "desktop,ide,server,chat,pdf,scheduler"
  • gitleaks protect --staged --no-banner --redact
  • New regression test exercises eviction churn on both cache branches (hyphenated + plain word-boundary), confirming correctness is preserved past the LRU cap

@github-actions github-actions Bot added documentation Improvements or additions to documentation rust Rust code changes dependencies Dependency updates bug Something isn't working size/M Medium PR (51-200 lines) labels Jul 14, 2026
@bug-ops
bug-ops enabled auto-merge (squash) July 14, 2026 19:28
name_referenced_in memoized a compiled Regex per distinct lowercased
MCP tool name in two process-lifetime static HashMap caches with no
eviction. Tool names are attacker-influenced (untrusted MCP server
input), so a malicious or reconnecting server rotating its advertised
tool names could grow both caches without bound, leaking memory over
the lifetime of a long-running daemon/gateway/serve process.

Replace both caches with lru::LruCache capped at 256 entries, using
get_or_insert as a drop-in replacement for entry().or_insert_with()
with the same single-lock-acquisition semantics. Add a regression test
that churns well past the cap and confirms a long-evicted entry still
matches correctly on recompile.

Closes #6255
@bug-ops
bug-ops force-pushed the fix/6255-unbounded-regex-cache-mcp branch from 9226f8c to c63eab5 Compare July 14, 2026 19:36
@bug-ops
bug-ops merged commit f614fa8 into main Jul 14, 2026
43 checks passed
@bug-ops
bug-ops deleted the fix/6255-unbounded-regex-cache-mcp branch July 14, 2026 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working dependencies Dependency updates documentation Improvements or additions to documentation rust Rust code changes size/M Medium PR (51-200 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unbounded regex cache in zeph-mcp name_referenced_in keyed by external MCP tool names

1 participant