feat: v0.7.2 安全收口 · 断链接线 · 观测补洞 — 230 用例扩充与全量回归
CI / 类型检查 + Lint + 单元测试 (push) Failing after 5m43s
CI / 全量测试 (Electron ABI) (push) Failing after 5m25s
CI / 产物编译验证 (push) Successful in 10m1s

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:
2026-08-30 00:09:25 +08:00
parent 36e4b28d93
commit 26169b7be4
55 changed files with 5673 additions and 434 deletions
@@ -0,0 +1,138 @@
/**
* Network Proxy 测试(v0.7.2 覆盖补齐 —— 此前零测试)
*
* 锁定 session 级代理应用契约(v0.6.4 P4-5 的回归防线):
* 1. 配置 proxyUrl → Chromium 双分区(default + agent-browser+ undici ProxyAgent
* 2. 环境变量回退(HTTPS_PROXY/HTTP_PROXY
* 3. 双空 → 显式直连(direct mode + 直连 Agent
* 4. 失败语义:任一通道失败仅 WARN 不阻断主流程
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
const sessionMocks = vi.hoisted(() => ({
defaultSetProxy: vi.fn(async (..._args: unknown[]) => undefined),
partitionSetProxy: vi.fn(async (..._args: unknown[]) => undefined),
throwOnDefaultSetProxy: false,
}));
vi.mock('electron', () => ({
session: {
defaultSession: {
setProxy: (...args: unknown[]) => {
if (sessionMocks.throwOnDefaultSetProxy) {
return Promise.reject(new Error('setProxy exploded'));
}
return sessionMocks.defaultSetProxy(...args);
},
},
fromPartition: () => ({
setProxy: (...args: unknown[]) => sessionMocks.partitionSetProxy(...args),
}),
},
}));
vi.mock('electron-log', () => ({
default: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
}));
const undiciMocks = vi.hoisted(() => ({
setGlobalDispatcher: vi.fn(),
proxyAgentCalls: [] as string[],
agentCalls: 0,
}));
vi.mock('undici', () => ({
Agent: class {
constructor() {
undiciMocks.agentCalls++;
}
},
ProxyAgent: class {
constructor(opts: { uri: string }) {
undiciMocks.proxyAgentCalls.push(opts.uri);
}
},
setGlobalDispatcher: (...args: unknown[]) => undiciMocks.setGlobalDispatcher(...args),
}));
import { applySessionProxy } from '../network-proxy';
beforeEach(() => {
sessionMocks.defaultSetProxy.mockClear();
sessionMocks.partitionSetProxy.mockClear();
sessionMocks.throwOnDefaultSetProxy = false;
undiciMocks.setGlobalDispatcher.mockClear();
undiciMocks.proxyAgentCalls.length = 0;
undiciMocks.agentCalls = 0;
delete process.env['HTTPS_PROXY'];
delete process.env['HTTP_PROXY'];
});
afterEach(() => {
delete process.env['HTTPS_PROXY'];
delete process.env['HTTP_PROXY'];
});
describe('applySessionProxy — 双通道应用', () => {
it('配置代理 → default + agent-browser 分区均设置 proxyRulesundici 走 ProxyAgent', async () => {
await applySessionProxy('http://127.0.0.1:7890');
const expected = { proxyRules: 'http://127.0.0.1:7890' };
expect(sessionMocks.defaultSetProxy).toHaveBeenCalledWith(expected);
expect(sessionMocks.partitionSetProxy).toHaveBeenCalledWith(expected);
expect(undiciMocks.proxyAgentCalls).toEqual(['http://127.0.0.1:7890']);
expect(undiciMocks.setGlobalDispatcher).toHaveBeenCalledTimes(1);
});
it('代理地址两侧空白被 trim', async () => {
await applySessionProxy(' http://proxy.local:8080 ');
expect(sessionMocks.defaultSetProxy).toHaveBeenCalledWith({
proxyRules: 'http://proxy.local:8080',
});
});
});
describe('applySessionProxy — 环境变量回退链', () => {
it('未配置 proxyUrl 时回退 HTTPS_PROXY 环境变量', async () => {
process.env['HTTPS_PROXY'] = 'http://env-proxy:3128';
await applySessionProxy(null);
expect(sessionMocks.defaultSetProxy).toHaveBeenCalledWith({
proxyRules: 'http://env-proxy:3128',
});
});
it('HTTPS_PROXY 缺失时回退 HTTP_PROXY', async () => {
process.env['HTTP_PROXY'] = 'http://env-http:3128';
await applySessionProxy(undefined);
expect(sessionMocks.defaultSetProxy).toHaveBeenCalledWith({
proxyRules: 'http://env-http:3128',
});
});
it('双空 → 显式直连(direct mode + 直连 Agent', async () => {
await applySessionProxy(null);
expect(sessionMocks.defaultSetProxy).toHaveBeenCalledWith({ mode: 'direct' });
expect(undiciMocks.agentCalls).toBe(1); // 直连 Agent
expect(undiciMocks.proxyAgentCalls).toHaveLength(0);
});
it('配置空串视为未配置(回退环境变量)', async () => {
process.env['HTTPS_PROXY'] = 'http://env-fallback:1';
await applySessionProxy(' ');
expect(sessionMocks.defaultSetProxy).toHaveBeenCalledWith({
proxyRules: 'http://env-fallback:1',
});
});
});
describe('applySessionProxy — 失败语义(绝不阻断主流程)', () => {
it('Chromium 通道失败 → 仅 WARNundici 通道照常设置', async () => {
sessionMocks.throwOnDefaultSetProxy = true;
await expect(applySessionProxy('http://proxy:1')).resolves.toBeUndefined();
expect(undiciMocks.proxyAgentCalls).toEqual(['http://proxy:1']);
});
});
@@ -0,0 +1,116 @@
/**
* Secure Config 测试(v0.7.2 覆盖补齐 —— 此前零测试)
*
* 锁定敏感配置加密存储契约(P0-1 的回归防线):
* 1. 敏感 key 判定模式(apikey/token/secret/password/auth_key
* 2. safeStorage 可用 → 加密前缀格式 + 解密还原
* 3. safeStorage 不可用 → 明文降级(可用性优先)+ WARN
* 4. 解密失败(跨机器/重装)→ 返回空串(引导重录而非崩溃)
* 5. 加密幂等(已加密值不二次加密)与非字符串透传
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
// 可控的 safeStorage 桩:可逆 fake 加密(enc: 前缀),可用性开关可编程
const mockState = vi.hoisted(() => ({
encryptionAvailable: true,
failDecrypt: false,
}));
vi.mock('electron', () => ({
safeStorage: {
isEncryptionAvailable: () => mockState.encryptionAvailable,
encryptString: (value: string) => Buffer.from(`enc:${value}`, 'utf-8'),
decryptString: (buffer: Buffer) => {
const raw = buffer.toString('utf-8');
if (mockState.failDecrypt || !raw.startsWith('enc:')) {
throw new Error('decryption failed');
}
return raw.slice(4);
},
},
}));
vi.mock('electron-log', () => ({
default: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
}));
import {
isSensitiveConfigKey,
isEncryptedValue,
encryptConfigValue,
decryptConfigValue,
} from '../secure-config';
beforeEach(() => {
mockState.encryptionAvailable = true;
mockState.failDecrypt = false;
});
describe('isSensitiveConfigKey — 敏感 key 判定', () => {
it.each([
['llm.apiKey', true],
['llm.api_key', true],
['searxng.auth_key', true],
['llm.fallbackApiKey', true],
['proxy.token', true],
['db.secret', true],
['GITHUB_PASSWORD', true],
['ui.theme', false],
['llm.model', false],
['agent.maxIterations', false],
['network.proxyUrl', false],
])('%s → %j', (key, expected) => {
expect(isSensitiveConfigKey(key)).toBe(expected);
});
});
describe('encrypt/decrypt — 加密回环', () => {
it('加密值带版本化前缀(metona-enc:v1:),解密还原原文', () => {
const encrypted = encryptConfigValue('sk-my-secret-key');
expect(typeof encrypted).toBe('string');
expect(isEncryptedValue(encrypted)).toBe(true);
expect(String(encrypted)).toMatch(/^metona-enc:v1:/);
const decrypted = decryptConfigValue(encrypted);
expect(decrypted).toBe('sk-my-secret-key');
});
it('非字符串值原样透传(number/boolean/null 不加密)', () => {
expect(encryptConfigValue(42)).toBe(42);
expect(encryptConfigValue(true)).toBe(true);
expect(encryptConfigValue(null)).toBe(null);
expect(decryptConfigValue(42)).toBe(42);
});
it('空字符串不加密(避免无意义前缀包裹)', () => {
expect(encryptConfigValue('')).toBe('');
});
it('已加密值幂等 —— 二次加密不再包裹前缀', () => {
const once = encryptConfigValue('sk-key') as string;
const twice = encryptConfigValue(once);
expect(twice).toBe(once);
expect(decryptConfigValue(twice)).toBe('sk-key');
});
it('非加密格式的值解密时原样返回(历史明文平滑兼容)', () => {
expect(decryptConfigValue('plain-old-key')).toBe('plain-old-key');
});
});
describe('加密降级与失败语义', () => {
it('safeStorage 不可用 → 明文存储(可用性优先 + WARN)', () => {
mockState.encryptionAvailable = false;
const value = encryptConfigValue('sk-plaintext-fallback');
expect(value).toBe('sk-plaintext-fallback');
expect(isEncryptedValue(value)).toBe(false);
});
it('加密过程抛错 → 回退明文存储(不阻断配置保存)', () => {
// decryptString 抛错不影响 encrypt;此处验证 decrypt 失败语义
mockState.failDecrypt = true;
const encrypted = encryptConfigValue('sk-x') as string;
expect(decryptConfigValue(encrypted)).toBe(''); // 失败 → 空串(createAdapter 判定未配置,引导重录)
});
});