name: electron-ipc-security-audit description: 分析Electron IPC实现中的安全漏洞,包括contextIsolation、nodeIntegration、preload脚本和通道验证 allowed-tools: Read, Grep, Glob, Bash tags: [electron, 安全, ipc, 审计, 桌面应用]
electron-ipc-security-audit
分析Electron IPC实现中的安全漏洞。此技能执行进程间通信模式的全面安全审计,检查contextIsolation问题、nodeIntegration风险、preload脚本安全性和IPC通道验证。
能力
- 审计IPC通道实现中的安全漏洞
- 检查contextIsolation和nodeIntegration配置
- 分析preload脚本中的不安全模式
- 验证IPC消息处理和清理
- 检测原型污染风险
- 检查远程代码执行漏洞
- 审查内容安全策略头
- 识别通过contextBridge暴露的API
输入模式
{
"type": "object",
"properties": {
"projectPath": {
"type": "string",
"description": "Electron项目根目录路径"
},
"auditScope": {
"type": "array",
"items": {
"enum": ["ipc-channels", "preload-scripts", "main-process", "renderer-security", "csp", "all"]
},
"default": ["all"]
},
"severity": {
"enum": ["all", "critical", "high", "medium"],
"default": "all",
"description": "报告的最低严重级别"
},
"includeRecommendations": {
"type": "boolean",
"default": true
}
},
"required": ["projectPath"]
}
输出模式
{
"type": "object",
"properties": {
"success": { "type": "boolean" },
"summary": {
"type": "object",
"properties": {
"totalIssues": { "type": "number" },
"critical": { "type": "number" },
"high": { "type": "number" },
"medium": { "type": "number" },
"low": { "type": "number" }
}
},
"findings": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "string" },
"severity": { "enum": ["critical", "high", "medium", "low"] },
"category": { "type": "string" },
"title": { "type": "string" },
"description": { "type": "string" },
"file": { "type": "string" },
"line": { "type": "number" },
"recommendation": { "type": "string" },
"codeExample": { "type": "string" }
}
}
},
"securityScore": {
"type": "number",
"description": "安全评分0-100"
}
},
"required": ["success", "findings"]
}
安全检查
关键检查
- nodeIntegration启用: 检查BrowserWindow中的
nodeIntegration: true - contextIsolation禁用: 检查
contextIsolation: false - 沙箱禁用: 检查
sandbox: false - 直接ipcRenderer暴露: 检查未通过contextBridge暴露ipcRenderer
- 远程模块使用: 检查已弃用的远程模块
- eval/Function执行: 检查IPC处理器中的动态代码执行
高严重性检查
- 无限制IPC通道: 检查
ipcMain.on('*')模式 - 缺少输入验证: 检查未清理的IPC参数
- webSecurity禁用: 检查
webSecurity: false - 不安全协议注册: 检查自定义协议处理器
- 缺少CSP头: 检查内容安全策略
中严重性检查
- 过度宽松的文件访问: 检查广泛的文件系统访问
- 不安全的web偏好设置: 检查已弃用的选项
- 缺少通道白名单: 检查preload脚本暴露
- 导航到不受信任的URL: 检查导航处理器
使用说明
- 扫描项目结构: 识别主进程、preload和渲染器文件
- 检查BrowserWindow配置: 审计webPreferences设置
- 分析IPC实现: 审查ipcMain/ipcRenderer使用
- 审查preload脚本: 检查contextBridge API暴露
- 验证CSP头: 确保正确的内容安全策略
- 生成报告: 编译包含严重性和建议的发现
漏洞模式
关键: 直接ipcRenderer暴露
// 错误: 直接暴露ipcRenderer
contextBridge.exposeInMainWorld('electron', {
ipcRenderer: ipcRenderer // 关键漏洞
});
// 正确: 仅暴露特定通道
contextBridge.exposeInMainWorld('electron', {
send: (channel, data) => {
const validChannels = ['file:read', 'file:write'];
if (validChannels.includes(channel)) {
ipcRenderer.send(channel, data);
}
}
});
关键: 缺少上下文隔离
// 错误: 上下文隔离禁用
new BrowserWindow({
webPreferences: {
contextIsolation: false, // 关键
preload: path.join(__dirname, 'preload.js')
}
});
// 正确: 上下文隔离启用
new BrowserWindow({
webPreferences: {
contextIsolation: true,
sandbox: true,
preload: path.join(__dirname, 'preload.js')
}
});
高: 无限制IPC处理器
// 错误: 执行任意命令
ipcMain.handle('execute', async (event, cmd) => {
return exec(cmd); // 高风险
});
// 正确: 仅允许白名单命令
const ALLOWED_COMMANDS = ['list-files', 'get-info'];
ipcMain.handle('execute', async (event, cmd, args) => {
if (!ALLOWED_COMMANDS.includes(cmd)) {
throw new Error('Command not allowed');
}
return executeWhitelistedCommand(cmd, args);
});
最佳实践
- 始终启用contextIsolation: 防止原型污染
- 使用沙箱模式: 限制渲染器进程能力
- 白名单IPC通道: 仅暴露必要的通道
- 验证所有IPC输入: 永远不要信任渲染器输入
- 避免动态代码执行: IPC处理器中不使用eval/Function
- 实现CSP头: 限制脚本源
- 使用invoke/handle模式: 对于请求-响应优先于send/on
相关技能
electron-main-preload-generator- 生成安全样板代码electron-builder-config- 构建配置desktop-security-auditoragent - 全面安全审查
相关代理
electron-architect- 架构指导desktop-security-auditor- 安全专业知识