本次升级基于完整代码审查,修复 Critical/High/Medium/Low 四级共 96 项问题, 并通过返工审计修复 10 项遗留问题,tsc 双端类型检查零错误。 Critical (10/10 完成): - C-4: command.ts 接入 shell-quote 进行 token-level 注入检测,替代原有正则匹配 可防御 r"m" -rf /、$'rm'、$(echo rm) 等字符串拼接绕过 High (11/11 完成): - 竞态保护、Promise.allSettled、AbortController 资源泄漏、IPC 参数校验等 Medium (55/55 完成): - 事务保护、敏感数据脱敏、枚举校验、MUI v9 Stack prop 迁移、 React 组件 cancelled 标志、类型收窄等 Low (20/20 完成): - 辅助方法提取(flushToolCallBuffer/scoreAndPushMemory/tryAddColumn 等) - nanoid 统一替代 Date.now()+Math.random() - confirm() 替换为 MUI Dialog、useMemo 缓存、魔法数字命名化等 返工审计修复 (10/10 完成): - L-11: LogsSettings 残留的原生 confirm()/alert() 全部替换为 MUI Dialog/Alert - M-53: MemoryViewer handleSearch 独立 ref,修复 searching 状态卡死 - M-42: 脱敏短值(length <= 4)泄露修复 - M-47: tasks:update 补全 title/description 类型校验 - L-9: ollama.adapter 非流式路径 nanoid 统一 - M-45: audit:query limit 策略与 memory:listAll 一致化 - SettingsModal handleConfirmRemove 补全 try/catch + loadServers cleanup - L-15: CommandPalette useMemo 补全 sessions 响应式依赖 - useAgentStream 事件类型补全 seq/timestamp 字段 新增依赖: shell-quote + @types/shell-quote 版本号: 0.3.0 -> 0.3.1
69 lines
2.5 KiB
TypeScript
69 lines
2.5 KiB
TypeScript
/**
|
|
* Pre-Tool Hooks — 工具执行前钩子
|
|
*
|
|
* @see docs/生产级通用 AI Agent 智能体桌面应用:完整设计与构建指南.html — 第五章
|
|
*/
|
|
|
|
import type { MetonaToolCall } from '../types';
|
|
import type { PolicyEngine } from '../sandbox/permissions';
|
|
|
|
export interface HookResult {
|
|
blocked: boolean;
|
|
reason?: string;
|
|
modifiedArgs?: Record<string, unknown>;
|
|
}
|
|
|
|
export interface PreToolHook {
|
|
beforeExecute(toolCall: MetonaToolCall, sessionId: string): Promise<HookResult>;
|
|
}
|
|
|
|
/** 权限校验钩子 — 集成 PolicyEngine */
|
|
export class PermissionCheckHook implements PreToolHook {
|
|
constructor(private policyEngine: PolicyEngine) {}
|
|
|
|
async beforeExecute(toolCall: MetonaToolCall, _sessionId: string): Promise<HookResult> {
|
|
const result = this.policyEngine.checkAuthorization(toolCall.name, toolCall.args);
|
|
if (!result.authorized) {
|
|
return { blocked: true, reason: result.reason };
|
|
}
|
|
// v0.3.0 修复: 授权成功后记录调用,使频率限制功能生效
|
|
// 在授权检查通过后立即记录,即使后续工具执行失败也计入频率
|
|
// 这样可以防止通过故意制造错误来绕过频率限制
|
|
this.policyEngine.recordCall(toolCall.name);
|
|
return { blocked: false };
|
|
}
|
|
}
|
|
|
|
/** 速率限制钩子 */
|
|
export class RateLimitHook implements PreToolHook {
|
|
private callCounts = new Map<string, { count: number; resetTime: number }>();
|
|
|
|
constructor(private maxCallsPerMinute: number = 20) {}
|
|
|
|
async beforeExecute(toolCall: MetonaToolCall, sessionId: string): Promise<HookResult> {
|
|
// 使用 sessionId:toolName 作为 key,实现会话隔离的 per-tool 速率限制
|
|
const key = `${sessionId}:${toolCall.name}`;
|
|
const now = Date.now();
|
|
|
|
// v0.3.0 修复: 定期清理过期 entry,避免 Map 随会话累积无限增长
|
|
if (this.callCounts.size > 1000) {
|
|
for (const [k, v] of this.callCounts) {
|
|
if (v.resetTime < now) this.callCounts.delete(k);
|
|
}
|
|
}
|
|
|
|
const entry = this.callCounts.get(key);
|
|
// L-2 修复: 使用 > 而非 >=,确保窗口到期时正确重置(边界条件)
|
|
// 当 resetTime === now 时应视为已到期,进入 else 分支重建 entry
|
|
if (entry && entry.resetTime > now) {
|
|
if (entry.count >= this.maxCallsPerMinute) {
|
|
return { blocked: true, reason: `Rate limit exceeded for tool "${toolCall.name}"` };
|
|
}
|
|
entry.count++;
|
|
} else {
|
|
this.callCounts.set(key, { count: 1, resetTime: now + 60_000 });
|
|
}
|
|
return { blocked: false };
|
|
}
|
|
}
|