fix(serve): strip env field, add status cleanup and name validation (T2.8 #4514)

Security:
- Strip `env` from runtime-added MCP server configs (prevents
  NODE_OPTIONS/LD_PRELOAD injection via HTTP body)

Correctness:
- Add `removeMCPServerStatus(name)` in spawn-failure catch block
  (prevents stale CONNECTING entry in status registry)

Hardening:
- Add name validation (charset + length) to ACP ext-method handlers
  for both add and remove (matches HTTP route validation)
This commit is contained in:
doudouOUC 2026-05-28 23:57:12 +08:00
parent 1ba2c09961
commit 66dc4ce1c8
2 changed files with 14 additions and 0 deletions

View file

@ -2504,6 +2504,12 @@ class QwenAgent implements Agent {
'Invalid or missing name',
);
}
if (name.length > 256 || !/^[A-Za-z0-9_-]+$/.test(name)) {
throw RequestError.invalidParams(
undefined,
'Server name must be ≤256 chars, alphanumeric + underscore/hyphen',
);
}
if (!config || typeof config !== 'object' || Array.isArray(config)) {
throw RequestError.invalidParams(
undefined,
@ -2537,6 +2543,7 @@ class QwenAgent implements Agent {
includeTools: _inc,
excludeTools: _exc,
cwd: _cwd,
env: _env,
...safeConfig
} = config as Record<string, unknown>;
const result = await manager.addRuntimeMcpServer(
@ -2578,6 +2585,12 @@ class QwenAgent implements Agent {
'Invalid or missing name',
);
}
if (name.length > 256 || !/^[A-Za-z0-9_-]+$/.test(name)) {
throw RequestError.invalidParams(
undefined,
'Server name must be ≤256 chars, alphanumeric + underscore/hyphen',
);
}
if (
typeof originatorClientId !== 'string' ||
originatorClientId.length === 0

View file

@ -2825,6 +2825,7 @@ export class McpClientManager {
// Clean up any partial state (including tools from partial discover)
this.toolRegistry.removeMcpToolsByServer(name);
this.pooledConnections.delete(name);
removeMCPServerStatus(name);
const failedClient = this.clients.get(name);
if (failedClient) {
try {