Skip to content

Commit 2bbfed7

Browse files
lsr911claude
andcommitted
fix(mcp): cap glob-to-regex wildcard segments to prevent ReDoS
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]>
1 parent dce1b0a commit 2bbfed7

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎src/agents/agent-bundle-mcp-materialize.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ function optionalStringRecordArg(input: unknown, key: string): Record<string, st
154154
return entries.length > 0 ? Object.fromEntries(entries) : undefined;
155155
}
156156

157+
const MAX_GLOB_WILDCARD_SEGMENTS = 32;
158+
157159
function escapeRegex(value: string): string {
158160
return value.replace(/[\\^$+?.()|[\]{}]/g, "\\$&");
159161
}
@@ -163,6 +165,9 @@ function globMatches(pattern: string, value: string): boolean {
163165
if (!trimmed) {
164166
return false;
165167
}
168+
if (trimmed.split("*").length > MAX_GLOB_WILDCARD_SEGMENTS + 1) {
169+
return false;
170+
}
166171
if (!trimmed.includes("*")) {
167172
return trimmed === value;
168173
}

‎src/agents/agent-bundle-mcp-runtime.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,16 @@ function escapeRegex(value: string): string {
315315
return value.replace(/[\\^$+?.()|[\]{}]/g, "\\$&");
316316
}
317317

318+
const MAX_GLOB_WILDCARD_SEGMENTS = 32;
319+
318320
function globMatches(pattern: string, value: string): boolean {
319321
const trimmed = pattern.trim();
320322
if (!trimmed) {
321323
return false;
322324
}
325+
if (trimmed.split("*").length > MAX_GLOB_WILDCARD_SEGMENTS + 1) {
326+
return false;
327+
}
323328
if (!trimmed.includes("*")) {
324329
return trimmed === value;
325330
}

0 commit comments

Comments
 (0)