feat: v0.7.2 安全收口 · 断链接线 · 观测补洞 — 230 用例扩充与全量回归
P1 修复面收口: /clear 全链路根治(前端清空联动 DB messages+摘要游标+TRACE 快照, IPC 语义改"操作完成"; 流式中拒绝); web_browser open 补 SSRF 校验(Chromium 旁路关闭, 与 web_fetch/http_request 同源 validateSSRF); MCP 工具结果纳入注入扫描(mcp_* 前缀 按网络来源同级 full 模式, 收敛 resolveScanMode 单点); Trace 落库/入 store 双重瘦身 (tool_result base64/超长字段剥离, metadata 防 MB 级膨胀); 文本附件 512KB 闸门 (file.slice 首段读取+truncated 标志随消息持久化+主进程附件提示感知截断); 单实例锁(requestSingleInstanceLock + second-instance 聚焦已有窗口) P2 安全纵深: ConfirmationHook 多窗口化(确认请求/超时提示改全窗口广播, getAllWindows 空时回退 mainWindow, fail-closed 判定升级双通道); mcp_servers.headers 全链路接线(safeParseHeaders 容错解析+SSE/StreamableHTTP requestInit 注入+IPC 逐项 校验+设置页 JSON 输入, 远程 MCP 鉴权头可用) P3 断链接线: llm:listModels IPC(六家 adapter 动态模型发现首次接线, 配置完整性 前置校验); Ollama pullModel IPC+设置页下载卡片(进度/取消/能力徽标, v0.7.0 死代码 激活); 后台会话运行指示(sessionRunStates 图+Sidebar 状态点, 多会话并发可见); IR 卫生(移除 THINKING_START/END 死枚举, constraints 标注预留) P4 质量与文档: i18n 第二阶段(确认弹框/侧栏/状态栏/AgentMonitor/终止原因出层, 外观设置 zh-CN/en-US 切换, ui.locale 持久化, 渲染时求值规避异步注册); README/D1 文档对齐(http_request 风险等级/用例数/实现状态注记); 版本号 0.7.2 测试: 507 → 737 用例(+230, 11 个新文件)。覆盖补齐: context-builder/consolidator/ orchestrator/workspace.service/session-recorder/config-layering/secure-config/ network-proxy + IPC mcp/tasks/memory/app/data 域 + 渲染层 store 与流事件管线纯函数。 测试驱动修复: workspace.appendMemory 中文分区 \b 词边界失效(JS \b 不含 CJK), 固化条目恒追加文件末尾产生重复分区头 → (?=\n|$) 前瞻断言根治 回归: typecheck 双端 0 错误; ESLint 0/0; 系统 Node 687 通过 50 跳过; Electron ABI 全量 737/737 零跳过
This commit is contained in:
@@ -4,12 +4,22 @@
|
||||
* 超时拒绝、用户批准、批量审批、pending 管理、恢复询问接口
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { describe, it, expect, vi, beforeEach, afterEach, type Mock } from 'vitest';
|
||||
import type { BrowserWindow } from 'electron';
|
||||
import { ConfirmationHook } from '../confirmation-hook';
|
||||
import type { MetonaToolCall, MetonaToolDef } from '../../types';
|
||||
import { MetonaToolCategory, MetonaRiskLevel } from '../../types';
|
||||
|
||||
// v0.7.2 P2-7: ConfirmationHook 的请求分发升级为 BrowserWindow.getAllWindows()
|
||||
// 全窗口广播 —— 测试环境(node vitest)下 electron 的 BrowserWindow 为 undefined,
|
||||
// 统一 mock 为可控的窗口数组(默认空数组 → 广播回退到注入的 mainWindow,与旧行为一致)。
|
||||
const getAllWindowsMock = vi.fn((): BrowserWindow[] => []);
|
||||
vi.mock('electron', () => ({
|
||||
BrowserWindow: {
|
||||
getAllWindows: () => getAllWindowsMock(),
|
||||
},
|
||||
}));
|
||||
|
||||
/** 需要确认的高风险工具定义 */
|
||||
const HIGH_RISK_DEF: MetonaToolDef = {
|
||||
name: 'run_command',
|
||||
@@ -61,6 +71,98 @@ function makeMockWindow(): BrowserWindow {
|
||||
} as unknown as BrowserWindow;
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
// v0.7.2 P2-7: 每个用例前重置窗口数组 mock(默认无窗口 → 广播回退 mainWindow)
|
||||
getAllWindowsMock.mockReturnValue([]);
|
||||
});
|
||||
|
||||
describe('ConfirmationHook — 多窗口广播(v0.7.2 P2-7)', () => {
|
||||
/** 带send 追踪的窗口(新测试用;makeMockWindow 保持原签名兼容既有用例) */
|
||||
function makeTrackedWindow(destroyed = false): { win: BrowserWindow; send: Mock } {
|
||||
const send = vi.fn();
|
||||
const win = {
|
||||
isDestroyed: () => destroyed,
|
||||
webContents: { send },
|
||||
} as unknown as BrowserWindow;
|
||||
return { win, send };
|
||||
}
|
||||
|
||||
it('确认请求广播到所有存活窗口(而非仅 mainWindow)', async () => {
|
||||
const a = makeTrackedWindow();
|
||||
const b = makeTrackedWindow();
|
||||
const destroyed = makeTrackedWindow(true);
|
||||
getAllWindowsMock.mockReturnValue([a.win, destroyed.win, b.win]);
|
||||
|
||||
const hook = new ConfirmationHook(null, null);
|
||||
hook.setToolDefs([HIGH_RISK_DEF]);
|
||||
const p = hook.beforeExecute(makeToolCall(), 'sess');
|
||||
|
||||
// 所有存活窗口均收到确认请求(携带 expiresAt 倒计时契约)
|
||||
expect(a.send).toHaveBeenCalledWith(
|
||||
'tool:confirmationRequest',
|
||||
expect.objectContaining({ toolName: 'run_command', expiresAt: expect.any(Number) }),
|
||||
);
|
||||
expect(b.send).toHaveBeenCalledWith(
|
||||
'tool:confirmationRequest',
|
||||
expect.objectContaining({ toolName: 'run_command' }),
|
||||
);
|
||||
// 已销毁窗口不接收(防 send on destroyed webContents 抛错)
|
||||
expect(destroyed.send).not.toHaveBeenCalled();
|
||||
|
||||
hook.resolveConfirmation(hook.getPendingConfirmations()[0].toolCallId, true, false, false);
|
||||
expect((await p).blocked).toBe(false);
|
||||
});
|
||||
|
||||
it('getAllWindows 为空时回退到注入的 mainWindow(向后兼容)', async () => {
|
||||
const mainWin = makeMockWindow();
|
||||
getAllWindowsMock.mockReturnValue([]);
|
||||
const hook = new ConfirmationHook(mainWin, null);
|
||||
hook.setToolDefs([HIGH_RISK_DEF]);
|
||||
const p = hook.beforeExecute(makeToolCall(), 'sess');
|
||||
expect((mainWin.webContents as unknown as { send: Mock }).send).toHaveBeenCalledWith(
|
||||
'tool:confirmationRequest',
|
||||
expect.objectContaining({ toolName: 'run_command' }),
|
||||
);
|
||||
hook.resolveConfirmation(hook.getPendingConfirmations()[0].toolCallId, true, false, false);
|
||||
expect((await p).blocked).toBe(false);
|
||||
});
|
||||
|
||||
it('全部窗口不可达时 fail-closed 阻断(no main window available)', async () => {
|
||||
const destroyed = makeTrackedWindow(true);
|
||||
getAllWindowsMock.mockReturnValue([destroyed.win]);
|
||||
const hook = new ConfirmationHook(null, null);
|
||||
hook.setToolDefs([HIGH_RISK_DEF]);
|
||||
const result = await hook.beforeExecute(makeToolCall(), 'sess');
|
||||
expect(result.blocked).toBe(true);
|
||||
expect(result.reason).toContain('no main window available');
|
||||
});
|
||||
|
||||
it('超时提示同样广播到所有窗口(与确认请求同通道语义)', async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
const a = makeTrackedWindow();
|
||||
getAllWindowsMock.mockReturnValue([a.win]);
|
||||
const hook = new ConfirmationHook(null, null);
|
||||
hook.setToolDefs([HIGH_RISK_DEF]);
|
||||
hook.setConfirmationTimeout(30_000);
|
||||
|
||||
const p = hook.beforeExecute(makeToolCall(), 'sess');
|
||||
vi.advanceTimersByTime(31_000);
|
||||
await p;
|
||||
|
||||
expect(a.send).toHaveBeenCalledWith(
|
||||
'toast:show',
|
||||
expect.objectContaining({
|
||||
type: 'warning',
|
||||
message: expect.stringContaining('工具确认超时'),
|
||||
}),
|
||||
);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('ConfirmationHook — 免确认路径', () => {
|
||||
it('未注册工具定义时放行(由 ToolRegistry 处理未知工具错误)', async () => {
|
||||
const hook = new ConfirmationHook(null, null);
|
||||
|
||||
Reference in New Issue
Block a user