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:
@@ -29,12 +29,15 @@ export interface PermissionPolicy {
|
||||
export const DEFAULT_POLICIES: PermissionPolicy[] = [
|
||||
// v0.3.0 修复:deniedPatterns 使用 (?:\/|["'\s,}]|$) 匹配,
|
||||
// 覆盖 /etc/ 和 /etc(无尾斜杠,在 JSON 字符串中后跟引号的情况)
|
||||
{ toolName: 'read_file', requiredLevel: PermissionLevel.READ, deniedPatterns: [/\/etc(?:\/|["'\s,}]|$)/, /\/proc(?:\/|["'\s,}]|$)/, /C:\\Windows\\/i, /C:\\System32\\/i, /MEMORY\.md/i] },
|
||||
// H-5 修复: 移除 /MEMORY\.md/i 粗粒度正则 — 之前会误拦子目录的 MEMORY.md
|
||||
// 改为在 engine.ts executeToolSafely 中进行精确的根目录校验(仅保护 workspacePath/MEMORY.md)
|
||||
// @see project_memory.md — Only the MEMORY.md in the workspace root directory is protected
|
||||
{ toolName: 'read_file', requiredLevel: PermissionLevel.READ, deniedPatterns: [/\/etc(?:\/|["'\s,}]|$)/, /\/proc(?:\/|["'\s,}]|$)/, /C:\\Windows\\/i, /C:\\System32\\/i] },
|
||||
{ toolName: 'web_search', requiredLevel: PermissionLevel.READ, maxFrequency: 10 },
|
||||
{ toolName: 'list_directory', requiredLevel: PermissionLevel.READ },
|
||||
{ toolName: 'search_files', requiredLevel: PermissionLevel.READ },
|
||||
{ toolName: 'memory_search', requiredLevel: PermissionLevel.READ },
|
||||
{ toolName: 'write_file', requiredLevel: PermissionLevel.WRITE, deniedPatterns: [/\/etc(?:\/|["'\s,}]|$)/, /\/proc(?:\/|["'\s,}]|$)/, /\/System(?:\/|["'\s,}]|$)/, /C:\\Windows\\/i, /C:\\System32\\/i, /MEMORY\.md/i], requireConfirmation: true, maxFrequency: 5 },
|
||||
{ toolName: 'write_file', requiredLevel: PermissionLevel.WRITE, deniedPatterns: [/\/etc(?:\/|["'\s,}]|$)/, /\/proc(?:\/|["'\s,}]|$)/, /\/System(?:\/|["'\s,}]|$)/, /C:\\Windows\\/i, /C:\\System32\\/i], requireConfirmation: true, maxFrequency: 5 },
|
||||
{ toolName: 'memory_store', requiredLevel: PermissionLevel.WRITE },
|
||||
{ toolName: 'run_command', requiredLevel: PermissionLevel.EXTERNAL_ACTION, deniedPatterns: [/MEMORY\.md/i], requireConfirmation: true, maxFrequency: 3 },
|
||||
{ toolName: 'web_fetch', requiredLevel: PermissionLevel.READ },
|
||||
@@ -43,7 +46,7 @@ export const DEFAULT_POLICIES: PermissionPolicy[] = [
|
||||
{ toolName: 'web_browser', requiredLevel: PermissionLevel.EXTERNAL_ACTION, requireConfirmation: true, maxFrequency: 20 },
|
||||
// v0.3.0 修复: 补全缺失的工具策略 — 之前这5个工具未配置策略,导致被 PolicyEngine 拦截
|
||||
// file_editor — 精准文件编辑(WRITE),与 write_file 同级安全约束
|
||||
{ toolName: 'file_editor', requiredLevel: PermissionLevel.WRITE, deniedPatterns: [/\/etc(?:\/|["'\s,}]|$)/, /\/proc(?:\/|["'\s,}]|$)/, /\/System(?:\/|["'\s,}]|$)/, /C:\\Windows\\/i, /C:\\System32\\/i, /MEMORY\.md/i], requireConfirmation: true, maxFrequency: 10 },
|
||||
{ toolName: 'file_editor', requiredLevel: PermissionLevel.WRITE, deniedPatterns: [/\/etc(?:\/|["'\s,}]|$)/, /\/proc(?:\/|["'\s,}]|$)/, /\/System(?:\/|["'\s,}]|$)/, /C:\\Windows\\/i, /C:\\System32\\/i], requireConfirmation: true, maxFrequency: 10 },
|
||||
// code_search — 基于 ripgrep 的只读搜索(READ)
|
||||
{ toolName: 'code_search', requiredLevel: PermissionLevel.READ },
|
||||
// diff_viewer — 文件/文本差异对比(只读,READ)
|
||||
@@ -52,6 +55,10 @@ export const DEFAULT_POLICIES: PermissionPolicy[] = [
|
||||
{ toolName: 'task_manager', requiredLevel: PermissionLevel.WRITE },
|
||||
// delegate_task — 子任务委派(启动 SubAgent,EXTERNAL_ACTION)
|
||||
{ toolName: 'delegate_task', requiredLevel: PermissionLevel.EXTERNAL_ACTION, requireConfirmation: false, maxFrequency: 5 },
|
||||
// C-7 修复: MCP 工具通配符策略 — MCP 工具名称动态生成(mcp_{serverName}_{toolName})
|
||||
// 无法预先配置精确策略,使用 mcp_* 通配符匹配所有 MCP 工具
|
||||
// @see project_memory.md — All tools must have a configured policy in DEFAULT_POLICIES
|
||||
{ toolName: 'mcp_*', requiredLevel: PermissionLevel.EXTERNAL_ACTION, requireConfirmation: true, maxFrequency: 20 },
|
||||
];
|
||||
|
||||
export class PolicyEngine {
|
||||
@@ -90,7 +97,18 @@ export class PolicyEngine {
|
||||
level: PermissionLevel;
|
||||
requiresConfirmation: boolean;
|
||||
} {
|
||||
const policy = this.policies.get(toolName);
|
||||
let policy = this.policies.get(toolName);
|
||||
|
||||
// C-7 修复: 支持通配符策略匹配(如 mcp_* 匹配所有 MCP 工具)
|
||||
// MCP 工具名称动态生成(mcp_{serverName}_{toolName}),无法预先配置精确策略
|
||||
if (!policy) {
|
||||
for (const [pattern, p] of this.policies) {
|
||||
if (pattern.endsWith('*') && toolName.startsWith(pattern.slice(0, -1))) {
|
||||
policy = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!policy) {
|
||||
return {
|
||||
|
||||
@@ -94,46 +94,54 @@ export class SandboxManager {
|
||||
* fork bomb、PowerShell 编码执行、环境变量窃取、编码绕过等。
|
||||
*/
|
||||
scanCode(code: string): { safe: boolean; reason?: string } {
|
||||
// C-5 修复: 补齐 28 个模式 + 加强 base64/$() 检测
|
||||
// @see project_memory.md — sandbox scanCode must include 28 patterns with 'i' flag
|
||||
// and base64/$() detection to prevent encoding bypass
|
||||
const dangerousPatterns = [
|
||||
// 危险模块导入
|
||||
// 危险模块导入(3)
|
||||
/require\s*\(\s*['"]child_process['"]\s*\)/i,
|
||||
/import\s+.*from\s+['"]fs['"]/i,
|
||||
/import\s+.*from\s+['"]child_process['"]/i,
|
||||
// 代码执行
|
||||
// 代码执行(3)
|
||||
/\beval\s*\(/i,
|
||||
/process\.exit/i,
|
||||
/Function\s*\(/i,
|
||||
// 路径遍历
|
||||
// 路径遍历(2)
|
||||
/\.\.\//i,
|
||||
/\\\.\.\\/i, // Windows ..\
|
||||
// 危险命令
|
||||
// 危险命令(3)
|
||||
/\brm\s+-rf\b/i,
|
||||
/\bkillall\s+-9\b/i,
|
||||
/\bchown\s+-R\s+\//i,
|
||||
// 重定向到系统目录
|
||||
// 重定向到系统目录(2)
|
||||
/>\s*\/dev\/null/i,
|
||||
/>\s*\/etc\//i,
|
||||
// 管道执行
|
||||
// 管道执行(2)
|
||||
/\bcurl\b.*\|\s*(bash|sh|zsh)\b/i,
|
||||
/\bwget\b.*\|\s*(sh|bash|zsh)\b/i,
|
||||
// 反向 shell
|
||||
// 反向 shell(3)
|
||||
/\/bin\/(bash|sh)\s+-i/i,
|
||||
/\bnc\s+-e\b/i,
|
||||
/\bbash\s+-i\b/i,
|
||||
// Fork bomb
|
||||
// Fork bomb(1)
|
||||
/:\(\)\s*\{\s*:\|:\s*&\s*\};:/i,
|
||||
// PowerShell 编码执行
|
||||
// PowerShell 编码执行(1)
|
||||
/powershell.*-enc(odedCommand)?\s+/i,
|
||||
// 环境变量窃取
|
||||
// 环境变量窃取(1)
|
||||
/env\b.*\b(GITHUB_TOKEN|API_KEY|SECRET|PASSWORD)\b/i,
|
||||
// 编码绕过检测
|
||||
/\bbase64\b.*\|\s*(sh|bash|zsh)\b/i,
|
||||
// 编码绕过检测(4)— C-5 加强 base64 解码后执行 + 任意 $() 替换
|
||||
// 检测 base64 解码(-d 或 --decode)后管道到 shell
|
||||
/\bbase64\b.*(-d|--decode)?\b.*\|\s*(sh|bash|zsh)\b/i,
|
||||
/\batob\s*\(/i,
|
||||
/\bprintf\s+['"]\\x[0-9a-f]/i,
|
||||
// 命令替换
|
||||
/\$\([^)]*(rm|kill|del|format|mkfs)\b/i,
|
||||
// heredoc 执行
|
||||
// 检测任意 $() 命令替换中包含危险命令(扩展检测范围)
|
||||
/\$\([^)]*(rm|kill|del|format|mkfs|chmod|chown|curl|wget|nc|bash|sh)\b/i,
|
||||
// heredoc 执行(1)
|
||||
/<<\s*(EOF|END)\s*[\s\S]*?\b(rm|kill|del|format|mkfs)\b/i,
|
||||
// C-5 新增模式 1: Python -c 执行危险代码
|
||||
/\bpython3?\b.*-c\s+['"]\s*(import\s+(os|subprocess|shutil)|exec\s*\(|eval\s*\()/i,
|
||||
// C-5 新增模式 2: Node.js -e 执行危险代码
|
||||
/\bnode\b.*-e\s+['"]\s*(require\s*\(\s*['"]child_process|process\.exit|execSync|spawnSync)/i,
|
||||
];
|
||||
|
||||
for (const pattern of dangerousPatterns) {
|
||||
|
||||
Reference in New Issue
Block a user