安全修复: - 开启 webSecurity(CORS 改为 webRequest 允许清单精确放行 Ollama 地址) - 新增 net-guard SSRF 防护:web_fetch/download_file/browser_open 拦截环回/内网/链路本地地址(DNS 解析后校验) - browser_open 协议白名单(仅 http/https,阻止 file:// 绕过路径安全层) - git 参数注入防护(用户可控参数禁止 - 开头;git add 强制 -- 分隔) - 身份文件保护:SOUL.md/AGENT.md/USER.md 工具只读(防提示注入持久化劫持) - 系统目录硬红线 + 工作空间/白名单不可豁免系统目录 - spawn_task 权限只降不升(封顶于用户设置 subAgentMaxPermission) - 子代理写类工具接入主 Agent 确认管线 + 完整路径沙箱 - toast 改 textContent、HTML 导出 escapeHtml(XSS 修复) - Agent 浏览器改用 memory: 内存分区(退出清空 cookie/storage) 数据层重构: - sql.js 写入改防抖批量落盘(300ms 合并快照 + temp 原子替换 + 退出刷盘) - Schema 迁移改 PRAGMA user_version 顺序迁移数组 - 消息/设置/轨迹批量写(单事务);SearXNG 配置 13 次写合并为 1 次 - 会话摘要查询(getSessionSummaries/searchSessions 单条 SQL)消除 N+1 - 导出改 getAllSessionsData 一次 IPC 取回全部行 Bug 修复: - edit_file 替换符污染($&/$1 被特殊解释导致文件写坏) - truncateToolResult 暴力截断拼接非法 JSON 必然崩溃 - diff 算法 100MB dp 数组 → 前缀/后缀裁剪 + LCS 限额 + 回退 - move_file 跨盘 rename 失败回退 copy+delete - Ctrl+K 快捷键冲突(双注册);全局错误处理器双注册 - ffmpeg stderr 无限累积 + 帧进度 O(n²) 正则 - 搜索可达性预检只取响应头(Range: bytes=0-0) - 备份导出逐字节 base64 拼接(O(n²))改 FileReader - MCP clientInfo 版本硬编码 5.0.0 改真实版本;tools/list 支持 nextCursor 分页 - 看门狗默认值统一为 30 分钟;download_file 超时跟随用户配置 架构改进: - 主进程工具分发注册表 tool-dispatch.ts(消除 switch 硬编码) - agent-engine 拆分 result-formatter.ts / tool-parsing.ts(纯函数) - 文本兜底解析白名单改从注册表派生(补齐 browser_*/diff/spawn_task/mcp_*) - diff 工具默认启用;MODE_TOOLS 单一事实来源(tools-modal 复用) - 记忆系统:条目缓存 + 访问统计(hits/last)持久化 + removeById 按 ID 删除 - 度量历史启动恢复 + Metrics 仪表盘接入 JSON/Prometheus 导出 - 子代理模型下拉框打开设置时刷新(此前从未填充) 死代码清理(约 1400 行): - 删除 context-indexer 整模块、agent-safety 震荡检测/性能报告/依赖图/记忆调优/归档取回 - 删除 context-manager 水印/跳过压缩/自适应窗口/趋势分析/预算分配等未接线函数 - 删除 sanitizeToolArgs(污染 write_file 内容,防注入职责移交主进程安全层) - infra-service 裁剪为全局错误处理器唯一定义 文档对齐: - 新增内置 AGENT.md(工作空间同名文件可覆盖) - README/帮助面板/DEVELOPMENT 移除失实描述(WAL/内部URL拦截/5层防御/并行白名单/Hook 数量) - 工具数量口径统一 33;安全机制表新增 SSRF/身份保护/子代理权限等 9 项 工程化: - Vitest + 34 个单元测试(myers-diff/calculator/net-guard/MEMORY.md 格式) - Gitea Actions CI(typecheck + test + build) - package.json 新增 typecheck/test 脚本
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { handleCalculator } from '../src/main/calculator.js';
|
||||
|
||||
describe('handleCalculator', () => {
|
||||
it('基础四则运算', () => {
|
||||
expect(handleCalculator({ expression: '1 + 2' }).result).toBe(3);
|
||||
expect(handleCalculator({ expression: '10 - 4' }).result).toBe(6);
|
||||
expect(handleCalculator({ expression: '6 * 7' }).result).toBe(42);
|
||||
expect(handleCalculator({ expression: '10 / 4' }).result).toBe(2.5);
|
||||
});
|
||||
|
||||
it('运算优先级与括号', () => {
|
||||
expect(handleCalculator({ expression: '2 + 3 * 4' }).result).toBe(14);
|
||||
expect(handleCalculator({ expression: '(2 + 3) * 4' }).result).toBe(20);
|
||||
expect(handleCalculator({ expression: '2 * (3 + (4 - 1))' }).result).toBe(12);
|
||||
});
|
||||
|
||||
it('幂运算与取模', () => {
|
||||
expect(handleCalculator({ expression: '2 ** 10' }).result).toBe(1024);
|
||||
expect(handleCalculator({ expression: '10 % 3' }).result).toBe(1);
|
||||
});
|
||||
|
||||
it('一元负号', () => {
|
||||
expect(handleCalculator({ expression: '-5 + 3' }).result).toBe(-2);
|
||||
expect(handleCalculator({ expression: '2 * -3' }).result).toBe(-6);
|
||||
});
|
||||
|
||||
it('小数与空白字符', () => {
|
||||
expect(handleCalculator({ expression: ' 1.5 * 2 ' }).result).toBe(3);
|
||||
});
|
||||
|
||||
it('除零报错', () => {
|
||||
const r = handleCalculator({ expression: '1 / 0' });
|
||||
expect(r.success).toBe(false);
|
||||
expect(r.error).toContain('除数不能为零');
|
||||
});
|
||||
|
||||
it('非法字符拒绝(无 eval,防注入)', () => {
|
||||
expect(handleCalculator({ expression: 'process.exit(1)' }).success).toBe(false);
|
||||
expect(handleCalculator({ expression: '1;require("fs")' }).success).toBe(false);
|
||||
expect(handleCalculator({ expression: 'alert(1)' }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('括号不闭合报错', () => {
|
||||
expect(handleCalculator({ expression: '(1 + 2' }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('空表达式与超长表达式拒绝', () => {
|
||||
expect(handleCalculator({ expression: '' }).success).toBe(false);
|
||||
expect(handleCalculator({ expression: '1'.repeat(501) }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('结果非有限数报错', () => {
|
||||
const r = handleCalculator({ expression: '2 ** 10000' });
|
||||
expect(r.success).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { parseMemoryMd, serializeMemoryMd } from '../src/renderer/services/memory-service.js';
|
||||
|
||||
const HEAD = '# METONA MEMORY\n\n> 条目内容紧跟元数据行,直到下一个 ## 或文件末尾\n\n';
|
||||
|
||||
describe('MEMORY.md 序列化/解析往返', () => {
|
||||
it('解析标准条目并保留访问统计', () => {
|
||||
const md = HEAD +
|
||||
'## fact | id: mem_20260101_001 | importance: 8 | tags: a, b | hits: 3 | last: 1700000000000\n内容甲\n\n';
|
||||
const entries = parseMemoryMd(md);
|
||||
expect(entries).toHaveLength(1);
|
||||
expect(entries[0].id).toBe('mem_20260101_001');
|
||||
expect(entries[0].importance).toBe(8);
|
||||
expect(entries[0].tags).toEqual(['a', 'b']);
|
||||
expect(entries[0].accessCount).toBe(3);
|
||||
expect(entries[0].lastAccessed).toBe(1700000000000);
|
||||
expect(entries[0].content).toBe('内容甲');
|
||||
});
|
||||
|
||||
it('旧格式(无 hits/last)兼容解析', () => {
|
||||
const md = HEAD + '## rule | id: mem_20260101_002 | importance: 10 | tags: x\n规则内容\n\n';
|
||||
const entries = parseMemoryMd(md);
|
||||
expect(entries).toHaveLength(1);
|
||||
expect(entries[0].accessCount ?? 0).toBe(0);
|
||||
});
|
||||
|
||||
it('序列化包含访问统计后缀并可通过主进程校验正则', () => {
|
||||
const entries = parseMemoryMd(HEAD + '## fact | id: mem_20260101_003 | importance: 5 | tags: t\nhello\n\n');
|
||||
entries[0].accessCount = 2;
|
||||
entries[0].lastAccessed = 1700000001000;
|
||||
const out = serializeMemoryMd(entries);
|
||||
expect(out).toMatch(/\| hits: 2 \| last: 1700000001000/);
|
||||
// 与主进程 validateMemoryContent 同构的条目头正则
|
||||
const headerLine = out.split('\n').find(l => l.startsWith('## '))!;
|
||||
expect(headerLine).toMatch(
|
||||
/^##\s+(fact|preference|rule)\s*\|\s*id:\s*mem_\d{8}_\d{3}\s*\|\s*importance:\s*\d{1,2}\s*\|\s*tags:\s+(.+?)(?:\s*\|\s*hits:\s*(\d+)\s*\|\s*last:\s*(\d+))?\s*$/i
|
||||
);
|
||||
});
|
||||
|
||||
it('零访问计数不写入后缀(保持旧格式简洁)', () => {
|
||||
const entries = parseMemoryMd(HEAD + '## fact | id: mem_20260101_004 | importance: 5 | tags: t\nhello\n\n');
|
||||
const out = serializeMemoryMd(entries);
|
||||
expect(out).not.toContain('hits:');
|
||||
});
|
||||
|
||||
it('往返保持内容一致', () => {
|
||||
const src = HEAD +
|
||||
'## preference | id: mem_20260101_005 | importance: 7 | tags: p1, p2\n偏好一\n\n' +
|
||||
'## rule | id: mem_20260101_006 | importance: 9 | tags: r1\n规则一\n\n';
|
||||
const round = serializeMemoryMd(parseMemoryMd(src));
|
||||
expect(parseMemoryMd(round).map(e => [e.id, e.content])).toEqual([
|
||||
['mem_20260101_005', '偏好一'],
|
||||
['mem_20260101_006', '规则一'],
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,81 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { diffLines, buildUnifiedHunks } from '../src/main/myers-diff.js';
|
||||
|
||||
describe('diffLines', () => {
|
||||
it('全同文件返回全 equal', () => {
|
||||
const ops = diffLines(['a', 'b', 'c'], ['a', 'b', 'c']);
|
||||
expect(ops.every(op => op.op === 'equal')).toBe(true);
|
||||
expect(ops).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('纯新增', () => {
|
||||
const ops = diffLines(['a'], ['a', 'x', 'y']);
|
||||
const inserts = ops.filter(op => op.op === 'insert');
|
||||
expect(inserts).toHaveLength(2);
|
||||
expect(ops.filter(op => op.op === 'equal')).toHaveLength(1);
|
||||
expect(ops.filter(op => op.op === 'delete')).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('纯删除', () => {
|
||||
const ops = diffLines(['a', 'x', 'y', 'b'], ['a', 'b']);
|
||||
expect(ops.filter(op => op.op === 'delete')).toHaveLength(2);
|
||||
expect(ops.filter(op => op.op === 'insert')).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('中部修改:前后缀裁剪 + LCS 精确差异', () => {
|
||||
const old = ['h1', 'h2', 'old1', 'old2', 't1', 't2'];
|
||||
const now = ['h1', 'h2', 'new1', 't1', 't2'];
|
||||
const ops = diffLines(old, now);
|
||||
expect(ops.filter(op => op.op === 'delete').map(op => old[op.oldIdx!])).toEqual(['old1', 'old2']);
|
||||
expect(ops.filter(op => op.op === 'insert').map(op => now[op.newIdx!])).toEqual(['new1']);
|
||||
// 前后缀 equal 保留
|
||||
expect(ops[0].op).toBe('equal');
|
||||
expect(ops[ops.length - 1].op).toBe('equal');
|
||||
});
|
||||
|
||||
it('空文件对比', () => {
|
||||
expect(diffLines([], ['a'])).toEqual([{ op: 'insert', newIdx: 0 }]);
|
||||
expect(diffLines(['a'], [])).toEqual([{ op: 'delete', oldIdx: 0 }]);
|
||||
expect(diffLines([], [])).toEqual([]);
|
||||
});
|
||||
|
||||
it('LCS 识别交叉公共子序列', () => {
|
||||
const old = ['a', 'b', 'c', 'd'];
|
||||
const now = ['b', 'd'];
|
||||
const ops = diffLines(old, now);
|
||||
expect(ops.filter(op => op.op === 'equal')).toHaveLength(2); // b、d 被识别为公共
|
||||
expect(ops.filter(op => op.op === 'delete')).toHaveLength(2); // a、c 删除
|
||||
expect(ops.filter(op => op.op === 'insert')).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildUnifiedHunks', () => {
|
||||
it('无差异返回空 hunks', () => {
|
||||
const ops = diffLines(['a'], ['a']);
|
||||
expect(buildUnifiedHunks(ops, ['a'], ['a'], 3).hunks).toEqual([]);
|
||||
expect(buildUnifiedHunks(ops, ['a'], ['a'], 3).additions).toBe(0);
|
||||
});
|
||||
|
||||
it('生成带 @@ 头的 unified diff hunk', () => {
|
||||
const old = ['l1', 'l2', 'l3', 'l4', 'l5', 'l6', 'l7'];
|
||||
const now = ['l1', 'l2', 'l3', 'CHANGED', 'l5', 'l6', 'l7'];
|
||||
const ops = diffLines(old, now);
|
||||
const { hunks, additions, deletions } = buildUnifiedHunks(ops, old, now, 3);
|
||||
expect(hunks).toHaveLength(1);
|
||||
expect(hunks[0]).toMatch(/^@@ -1,7 \+1,7 @@/);
|
||||
expect(hunks[0]).toContain('-l4');
|
||||
expect(hunks[0]).toContain('+CHANGED');
|
||||
expect(additions).toBe(1);
|
||||
expect(deletions).toBe(1);
|
||||
});
|
||||
|
||||
it('相距较远的多处修改生成多个 hunks', () => {
|
||||
const old = Array.from({ length: 30 }, (_, i) => `line${i}`);
|
||||
const now = [...old];
|
||||
now[2] = 'mod-a';
|
||||
now[25] = 'mod-b';
|
||||
const ops = diffLines(old, now);
|
||||
const { hunks } = buildUnifiedHunks(ops, old, now, 2);
|
||||
expect(hunks.length).toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { isPrivateIp, checkPublicHttpUrl } from '../src/main/net-guard.js';
|
||||
|
||||
describe('isPrivateIp', () => {
|
||||
it('环回与未指定地址', () => {
|
||||
expect(isPrivateIp('127.0.0.1')).toBe(true);
|
||||
expect(isPrivateIp('0.0.0.0')).toBe(true);
|
||||
expect(isPrivateIp('::1')).toBe(true);
|
||||
expect(isPrivateIp('::')).toBe(true);
|
||||
expect(isPrivateIp('::ffff:127.0.0.1')).toBe(true);
|
||||
});
|
||||
|
||||
it('私网 IPv4 段', () => {
|
||||
expect(isPrivateIp('10.0.0.1')).toBe(true);
|
||||
expect(isPrivateIp('10.255.255.255')).toBe(true);
|
||||
expect(isPrivateIp('172.16.0.1')).toBe(true);
|
||||
expect(isPrivateIp('172.31.255.255')).toBe(true);
|
||||
expect(isPrivateIp('192.168.1.1')).toBe(true);
|
||||
expect(isPrivateIp('169.254.169.254')).toBe(true); // 云元数据端点
|
||||
});
|
||||
|
||||
it('172 段边界:15/32 是公网,16-31 是私网', () => {
|
||||
expect(isPrivateIp('172.15.255.255')).toBe(false);
|
||||
expect(isPrivateIp('172.32.0.1')).toBe(false);
|
||||
});
|
||||
|
||||
it('公网地址放行', () => {
|
||||
expect(isPrivateIp('8.8.8.8')).toBe(false);
|
||||
expect(isPrivateIp('1.1.1.1')).toBe(false);
|
||||
expect(isPrivateIp('172.100.0.1')).toBe(false);
|
||||
});
|
||||
|
||||
it('IPv6 ULA 与链路本地', () => {
|
||||
expect(isPrivateIp('fd00::1')).toBe(true);
|
||||
expect(isPrivateIp('fc12::1')).toBe(true);
|
||||
expect(isPrivateIp('fe80::1')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkPublicHttpUrl', () => {
|
||||
it('拒绝非 http/https 协议(file:// 读取本地文件)', async () => {
|
||||
const r = await checkPublicHttpUrl('file:///C:/Windows/win.ini');
|
||||
expect(r.ok).toBe(false);
|
||||
expect(r.reason).toContain('协议');
|
||||
});
|
||||
|
||||
it('拒绝 localhost 与本地域名后缀', async () => {
|
||||
expect((await checkPublicHttpUrl('http://localhost:11434/api/tags')).ok).toBe(false);
|
||||
expect((await checkPublicHttpUrl('http://foo.internal/x')).ok).toBe(false);
|
||||
expect((await checkPublicHttpUrl('http://bar.local/x')).ok).toBe(false);
|
||||
});
|
||||
|
||||
it('拒绝字面量内网 IP', async () => {
|
||||
expect((await checkPublicHttpUrl('http://127.0.0.1:11434/')).ok).toBe(false);
|
||||
expect((await checkPublicHttpUrl('http://192.168.1.1/admin')).ok).toBe(false);
|
||||
expect((await checkPublicHttpUrl('http://169.254.169.254/latest/meta-data')).ok).toBe(false);
|
||||
});
|
||||
|
||||
it('公网域名放行', async () => {
|
||||
const r = await checkPublicHttpUrl('https://www.baidu.com/');
|
||||
expect(r.ok).toBe(true);
|
||||
});
|
||||
|
||||
it('无效 URL 拒绝', async () => {
|
||||
expect((await checkPublicHttpUrl('not a url')).ok).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user