feat: 升级至 v0.3.1 — 全量代码审计修复 + 安全增强

本次升级基于完整代码审查,修复 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
This commit is contained in:
thzxx
2026-07-13 22:36:58 +08:00
parent 4f5f570ac8
commit e4d81d8247
47 changed files with 2247 additions and 475 deletions
+19
View File
@@ -83,12 +83,21 @@ export function MemoryViewer(): React.JSX.Element {
// Agent 完成时自动刷新:监听 agentStatus 从 thinking/executing -> idle
const agentStatus = useAgentStore((s) => s.agentStatus);
const prevStatus = useRef(agentStatus);
// M-53 修复: 竞态保护 ref,防止多次 loadMemories 调用乱序完成导致旧数据覆盖
const loadReqIdRef = useRef(0);
// 审计补充修复: handleSearch 使用独立 ref,避免与 loadMemories 共用导致 searching 状态卡死
// 原问题:handleSearch 与 loadMemories 共用 loadReqIdRef,当 agent 完成触发 loadMemories 时
// 会 ++ref,使 handleSearch 的 finally 检查失败,setSearching(false) 不执行,UI 永久显示"搜索中..."
const searchReqIdRef = useRef(0);
const loadMemories = useCallback(async () => {
if (!window.metona?.memory?.listAll) return;
const reqId = ++loadReqIdRef.current;
try {
setError(null);
const res = await window.metona.memory.listAll();
// 竞态保护:若已被新请求取代或组件已卸载,放弃本次结果
if (loadReqIdRef.current !== reqId) return;
if (!res.success) {
setError(res.error ?? '加载记忆失败');
return;
@@ -100,6 +109,7 @@ export function MemoryViewer(): React.JSX.Element {
working: (data.working as MemoryItem[] | undefined) ?? [],
});
} catch (err) {
if (loadReqIdRef.current !== reqId) return;
setError((err as Error).message ?? '加载记忆失败');
}
}, []);
@@ -107,6 +117,8 @@ export function MemoryViewer(): React.JSX.Element {
// 初次挂载加载
useEffect(() => {
loadMemories();
// cleanup: 使当前请求失效(防止卸载后 setState)
return () => { loadReqIdRef.current++; };
}, [loadMemories]);
// Agent 完成自动刷新
@@ -123,13 +135,20 @@ export function MemoryViewer(): React.JSX.Element {
if (!q || !window.metona?.memory?.search) return;
setSearching(true);
setError(null);
// 审计补充修复: 使用独立 searchReqIdRef,不再与 loadMemories 共用
const reqId = ++searchReqIdRef.current;
try {
const results = await window.metona.memory.search(q, { topK: 20 });
// 竞态保护:若已被新搜索请求取代或组件已卸载,放弃本次结果
if (searchReqIdRef.current !== reqId) return;
setSearchResults(results);
} catch (err) {
if (searchReqIdRef.current !== reqId) return;
setError((err as Error).message ?? '搜索失败');
setSearchResults([]);
} finally {
// 审计补充修复: 无条件清理 searching 状态,避免被 loadMemories 取代时卡死
// searching 仅对当前搜索有意义,请求被取代后应停止 spinner
setSearching(false);
}
};