Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(auto-review): MCP 工具名改用字符串解析,消除 CodeQL 报告的正则回溯

Dashc6666d0778f4
diff --git a/packages/maker-shared/src/__tests__/interaction.test.ts b/packages/maker-shared/src/__tests__/interaction.test.ts
index 4ab9faedf1..3551841c33 100644
--- a/packages/maker-shared/src/__tests__/interaction.test.ts
+++ b/packages/maker-shared/src/__tests__/interaction.test.ts
@@ -308,6 +308,14 @@ describe('interaction shared model', () => {
expect(describePermissionAction('mcp:cindy_browser:call_tool', {
name: 'browser', args: { action: 'recipe', recipeId: 'post-reply', site: 'x.com', text: 'hello' },
}).preview).toBe('recipe {"recipeId":"post-reply","site":"x.com","text":"hello"}');
+ // MCP names parse by plain string scan: both separators work and hostile names stay linear (CodeQL ReDoS).
+ expect(describePermissionAction('mcp__cindy_orca__create_workers', { n: 1 })).toMatchObject({ kind: 'plugin', subject: 'cindy_orca', preview: 'create_workers {"n":1}' });
+ expect(describePermissionAction('mcp:my-server:do_it', {})).toMatchObject({ kind: 'plugin', subject: 'my-server', preview: 'do_it' });
+ expect(describePermissionAction('mcp__', {}).kind).toBe('tool');
+ const hostile = `mcp:9${'-9'.repeat(50_000)}`;
+ const started = Date.now();
+ expect(describePermissionAction(hostile, {}).kind).toBe('tool');
+ expect(Date.now() - started).toBeLessThan(500);
// argv arrays keep argument boundaries.
expect(describePermissionAction('shell', { command: ['printf', '%s', 'a b', "it's", ''] }).preview)
.toBe("printf %s 'a b' 'it'\\''s' ''");
diff --git a/packages/maker-shared/src/interaction.ts b/packages/maker-shared/src/interaction.ts
index 4a62e32fa9..db945ec797 100644
--- a/packages/maker-shared/src/interaction.ts
+++ b/packages/maker-shared/src/interaction.ts
@@ -914,6 +914,20 @@ function argsPreview(args: Record<string, unknown>): string {
return text.length > 500 ? `${text.slice(0, 500)}...` : text;
}
+/** `mcp__server__tool` or `mcp:server:tool`; plain string scan, no backtracking on model-controlled names. */
+function parseMcpToolName(toolName: string): { server: string; tool: string } | null {
+ const rest = toolName.startsWith('mcp__') ? toolName.slice(5) : toolName.startsWith('mcp:') ? toolName.slice(4) : null;
+ if (!rest) return null;
+ const underscore = rest.indexOf('__');
+ const colon = rest.indexOf(':');
+ const cut = underscore < 0 ? colon : colon < 0 ? underscore : Math.min(underscore, colon);
+ if (cut <= 0) return null;
+ const sepLength = rest.startsWith('__', cut) ? 2 : 1;
+ const server = rest.slice(0, cut);
+ const tool = rest.slice(cut + sepLength);
+ return tool ? { server, tool } : null;
+}
+
function shellQuote(arg: string): string {
return /^[A-Za-z0-9_\/.:=@%+,-]+$/.test(arg) ? arg : `'${arg.replaceAll("'", `'\\''`)}'`;
}
@@ -988,11 +1002,11 @@ export function describePermissionAction(toolName: string, input: Record<string,
const paths = filePaths(input, FILE_READ_TOOLS.has(name) && name !== 'read');
if (paths) return { kind: FILE_READ_TOOLS.has(name) ? 'file-read' : 'file-write', preview: paths };
}
- const mcp = /^mcp(?:__|:)([^_:]+(?:[_-][^_:]+)*?)(?:__|:)(.+)$/.exec(toolName);
+ const mcp = parseMcpToolName(toolName);
if (mcp) {
- const dispatched = mcp[2] === 'call_tool' && innerName;
+ const dispatched = mcp.tool === 'call_tool' && innerName;
const args = dispatched ? innerArgs : input;
- return { kind: 'plugin', subject: mcp[1]!, preview: [dispatched || mcp[2], argsPreview(args)].filter(Boolean).join(' ') };
+ return { kind: 'plugin', subject: mcp.server, preview: [dispatched || mcp.tool, argsPreview(args)].filter(Boolean).join(' ') };
}
return { kind: 'tool', preview: previewOf(input) };
}