Skip to content

fix(mcp): cap glob-to-regex wildcard segments to prevent ReDoS - #99337

Closed
lsr911 wants to merge 1 commit into
openclaw:mainfrom
lsr911:fix-glob-redos
Closed

lsr911 wants to merge 1 commit into
openclaw:mainfrom
lsr911:fix-glob-redos

Conversation

@lsr911

@lsr911 lsr911 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

The globMatches() helper in both agent-bundle-mcp-runtime.ts and agent-bundle-mcp-materialize.ts converts user-configured MCP tool filter glob patterns into regular expressions using new RegExp(...). A malicious glob pattern with excessive * wildcards (e.g. a*a*a*... x32+) produces a regex vulnerable to catastrophic backtracking (ReDoS) when tested against crafted tool names.

This is not IPC-related — the fix is a pure input validation guard.

Why This Change Was Made

Add MAX_GLOB_WILDCARD_SEGMENTS = 32 guard that rejects patterns with more than 32 segments before regex construction. Normal tool filter patterns (typically 1-3 wildcards) are unaffected.

Changes

  • src/agents/agent-bundle-mcp-runtime.ts — add MAX_GLOB_WILDCARD_SEGMENTS constant + guard in globMatches()
  • src/agents/agent-bundle-mcp-materialize.ts — apply the same guard in the duplicate globMatches() implementation

Evidence

Reproducibility: yes. Source inspection confirms both locations construct regex from unsanitized user-controlled glob patterns without a wildcard limit.

Direct behavior probe (ReDoS PoC):

Pattern: "a*a*a*..." x32 (33 segments)
Before (no cap): regex constructed, vulnerable to backtracking on long tool names
After  (cap=32): pattern rejected before regex construction → instant false

Normal patterns preserved:
  "foo*bar*baz" matches "foo_test_bar_baz" → true
  "hello" matches "hello" → true

Related

N/A (self-contained security hardening)

🤖 Generated with Claude Code

Add MAX_GLOB_WILDCARD_SEGMENTS (32) guard to globMatches() in
agent-bundle-mcp-runtime.ts and agent-bundle-mcp-materialize.ts.
A malicious MCP tool filter glob with excessive wildcards can produce
a regex with catastrophic backtracking. The cap rejects patterns with
more than 32 segments before regex construction.

Normal patterns (1-3 wildcards) are unaffected.

Co-Authored-By: Claude <[email protected]>
Signed-off-by: 廖石荣 0668000909 <[email protected]>
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Jul 3, 2026
@clawsweeper

clawsweeper Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 2, 2026, 11:21 PM ET / 03:21 UTC.

Summary
The PR adds a wildcard-count guard to MCP glob matching in the agent bundle runtime and materialization paths.

PR surface: Source +10. Total +10 across 2 files.

Reproducibility: yes. Source inspection shows both MCP filter paths compile configured * patterns into regexes, and a read-only Node probe reproduced multi-second failure time for an accepted 32-wildcard pattern.

Review metrics: 1 noteworthy metric.

  • Wildcard boundary: 32 wildcards allowed; 33+ rejected. The accepted boundary is exactly where the review probe still observed multi-second regex backtracking.

Merge readiness
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🧂 unranked krab
Result: blocked until real behavior proof is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Replace the regex matcher with a linear wildcard matcher or prove a corrected cap with timing tests.
  • [P1] Add redacted terminal output or logs showing before/after pathological glob behavior and normal glob behavior.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body has only a textual probe claim; the contributor should add redacted terminal output, logs, or another live artifact showing before/after pathological and normal glob behavior, then update the PR body to trigger re-review.

Risk before merge

  • [P1] Merging as-is would leave crafted MCP tool-filter globs able to trigger expensive regex backtracking at the allowed 32-wildcard boundary.
  • [P1] The external PR currently lacks real behavior proof from the contributor setup; tests and CI do not replace that merge gate.

Maintainer options:

  1. Replace the regex conversion (recommended)
    Before merge, switch both MCP filter paths to a non-regex linear glob matcher or a proven safe helper, then cover repeated-wildcard inputs in focused tests.
  2. Correct and prove a lower cap
    Maintainers could keep the regex approach only if the boundary is corrected and timing proof shows every accepted pattern completes within a safe bound.
  3. Hold for contributor proof
    Even after the code repair, require redacted terminal/log proof or equivalent live output showing before/after behavior before treating the external PR as merge-ready.

Next step before merge

  • [P1] The PR needs contributor code changes and real behavior proof before merge; with proof missing, this should stay human-review/contributor-action rather than a ClawSweeper repair lane.

Security
Needs attention: The patch targets a real regex-construction surface, but the proposed cap still permits expensive backtracking at its allowed boundary.

Review findings

  • [P1] Replace the cap with a linear matcher — src/agents/agent-bundle-mcp-runtime.ts:323-325
Review details

Best possible solution:

Replace the duplicated regex conversion with a shared linear * matcher for MCP filters, preserve the documented exact-name/simple-glob semantics, and add regression coverage for both normal filters and repeated-wildcard adversarial inputs.

Do we have a high-confidence way to reproduce the issue?

Yes. Source inspection shows both MCP filter paths compile configured * patterns into regexes, and a read-only Node probe reproduced multi-second failure time for an accepted 32-wildcard pattern.

Is this the best way to solve the issue?

No. The cap is a plausible mitigation, but it leaves the regex backtracking mechanism reachable at the allowed boundary; a linear segment matcher is the safer maintainable fix.

Full review comments:

  • [P1] Replace the cap with a linear matcher — src/agents/agent-bundle-mcp-runtime.ts:323-325
    The new guard still allows exactly 32 * wildcards because split("*").length is 33, so the > MAX + 1 check does not fire. A probe using the same generated regex with 32 wildcards took about 2.7s to fail on a short input, so this leaves the ReDoS shape reachable in both MCP filter paths. Please replace the regex conversion with ordered segment matching, or lower and prove the accepted boundary with regression coverage.
    Confidence: 0.92

Overall correctness: patch is incorrect
Overall confidence: 0.9

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against b415072a2e6a.

Label changes

Label changes:

  • add P2: The PR addresses a bounded MCP tool-filter security hardening issue in agent runtime code, without evidence of active exploitation or broad runtime outage.
  • add merge-risk: 🚨 security-boundary: Merging the cap as written leaves crafted MCP tool-filter globs able to trigger expensive regex backtracking at the allowed boundary.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🧂 unranked krab.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body has only a textual probe claim; the contributor should add redacted terminal output, logs, or another live artifact showing before/after pathological and normal glob behavior, then update the PR body to trigger re-review.

Label justifications:

  • P2: The PR addresses a bounded MCP tool-filter security hardening issue in agent runtime code, without evidence of active exploitation or broad runtime outage.
  • merge-risk: 🚨 security-boundary: Merging the cap as written leaves crafted MCP tool-filter globs able to trigger expensive regex backtracking at the allowed boundary.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🧂 unranked krab.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body has only a textual probe claim; the contributor should add redacted terminal output, logs, or another live artifact showing before/after pathological and normal glob behavior, then update the PR body to trigger re-review.
Evidence reviewed

PR surface:

Source +10. Total +10 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 2 10 0 +10
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 10 0 +10

Security concerns:

  • [medium] Wildcard cap leaves ReDoS boundary reachable — src/agents/agent-bundle-mcp-runtime.ts:323
    The guard permits exactly 32 wildcard separators, and a same-shape Node regex probe showed that accepted boundary taking seconds to fail; this keeps the security issue partially open.
    Confidence: 0.9

What I checked:

  • Root policy read: Root repository policy was read fully and applied, including the requirement to inspect whole affected code paths, sibling surfaces, tests, and history for PR review. (AGENTS.md:1, b415072a2e6a)
  • Scoped policy read: The scoped agents policy was read and mainly affects validation expectations for focused agent-runtime tests. (src/agents/AGENTS.md:1, b415072a2e6a)
  • Runtime filter path: Current main trims MCP tool-filter patterns, handles exact strings, then converts * globs into a regular expression used while filtering listed MCP tools. (src/agents/agent-bundle-mcp-runtime.ts:318, b415072a2e6a)
  • Materialization filter path: Current main duplicates the regex-based glob matcher for MCP resource and prompt utility tool filtering during materialization. (src/agents/agent-bundle-mcp-materialize.ts:161, b415072a2e6a)
  • PR guard boundary: The added guard checks trimmed.split("*").length > MAX_GLOB_WILDCARD_SEGMENTS + 1, so a pattern with exactly 32 * characters has split length 33 and is still accepted. (src/agents/agent-bundle-mcp-runtime.ts:323, 2bbfed7e1bc8)
  • Timing reproduction: A read-only Node probe using the same regex construction showed an accepted 32-wildcard pattern taking about 2724 ms to fail against a short all-a value; a leading-star variant hit a 10s timeout.

Likely related people:

  • @steipete: PR Improve MCP operability #88399, authored by @steipete, introduced the broader MCP operability/toolFilter surface across the affected runtime, materialization, tests, and docs. (role: feature owner; confidence: high; commits: 8da3b799a978, 99ce71ddbbdb; files: src/agents/agent-bundle-mcp-runtime.ts, src/agents/agent-bundle-mcp-materialize.ts, src/agents/agent-bundle-mcp-runtime.test.ts)
  • Peter Lindsey: Local git blame on current main reports the duplicated globMatches lines under grafted commit 16a0777; because the checkout is shallow/grafted and the commit is broad, this is weaker routing evidence than the MCP operability PR. (role: recent current-main carrier; confidence: low; commits: 16a07774a991; files: src/agents/agent-bundle-mcp-runtime.ts, src/agents/agent-bundle-mcp-materialize.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. labels Jul 3, 2026
@lsr911

lsr911 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

Closing: ClawSweeper identified that the 32-wildcard cap still permits backtracking. Fix requires linear matcher rewrite — will revisit with complete solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: XS status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant