Electron 安全检查清单
按 BrowserWindow 权限、preload/contextBridge、IPC sender/input validation、remote content、navigation/window.open、CSP、permissions、protocol、Electron version/fuses 等逐项审查桌面应用安全边界,并要求每一项有可观察证据。
[!info] related notes
- 所属 MOC: Electron MOC
- Window: Electron BrowserWindow
- Context: Electron contextIsolation
- Sandbox: Electron 进程沙箱
- Preload: Electron preload 脚本
- IPC: Electron IPC 模式
Electron 安全检查清单
使用原则
这不是“把几个布尔值设成推荐值”就结束。
每个窗口都要能回答:
- 加载什么内容;
- 谁能执行脚本;
- renderer 被攻陷后能调用什么;
- privileged action 最终由谁验证;
- 导航到其他 origin 后权限是否还存在。
1. BrowserWindow 权限
逐窗口检查:
- nodeIntegration 是否保持 false;
- contextIsolation 是否为 true;
- sandbox 是否为 true;
- 是否真的需要 webviewTag;
- preload 是否只有必要窗口配置;
- session/partition 是否符合隔离需求。
如果任何一项放宽:
写清楚业务原因和补偿措施。
当前 Electron 默认本身已经倾向安全:
- nodeIntegration=false;
- contextIsolation=true;
- sandbox=true。
不要为了 legacy code 主动打穿默认边界。
2. Preload / contextBridge
检查:
- 是否只暴露明确命名业务 API;
- 是否暴露 ipcRenderer / fs / shell / process;
- 是否存在 invoke(channel) / send(channel) 万能入口;
- callback 是否隐藏原始 IPC event;
- API 是否返回最小、可序列化数据。
目标:
renderer compromise
→ 只能使用少量明确 capability
而不是:
renderer compromise
→ 任意 Electron IPC / Node ability
3. IPC Sender Validation
所有高权限 IPC handler 都应验证 sender。
检查:
- event.senderFrame / origin;
- 是否允许 iframe/child window;
- navigation 后 origin 是否改变;
- 是否仅靠 channel name 认为可信。
Electron 官方明确建议 validate sender of all IPC messages。
4. IPC Input Validation
TypeScript 不能替代 runtime validation。
检查:
- payload type;
- enum/range;
- path traversal;
- symlink;
- URL scheme/host;
- file ownership;
- current application state;
- authorization。
尤其是:
- filesystem write;
- shell/openExternal;
- updater;
- credential;
- native command;
必须有更严格 validation。
5. Remote Content
如果窗口加载远程内容:
- 只使用 HTTPS/WSS 等安全协议;
- 禁止 Node integration;
- contextIsolation 开启;
- sandbox 开启;
- preload API 更窄;
- permission handler 明确;
- navigation/new window 严格控制;
- CSP 更严格。
Electron 本质上不是“给任意第三方网页一个桌面壳”。
6. CSP
检查页面是否有 Content-Security-Policy。
优先:
script-src ‘self’
并尽量避免:
- unsafe-eval;
- unsafe-inline;
- 任意远程 script origin。
CSP 不是防止所有 XSS 的替代品,但它是重要纵深防御。
7. Navigation
检查:
- will-navigate;
- redirect;
- deep link;
- external URL;
- iframe;
- protocol handler。
页面不应该在保留 privileged preload 的情况下任意导航到未知 origin。
8. New Windows
对 window.open / target=_blank:
- 默认 deny;
- 明确 allowlist;
- 普通 web link 优先在系统浏览器打开;
- 不给新窗口继承高权限 preload。
9. shell.openExternal
永远不要把不可信字符串直接传入 shell.openExternal。
至少校验:
- scheme;
- host;
- expected origin;
- 是否允许 file/custom scheme。
否则可能产生危险 protocol invocation。
10. webSecurity / insecure content
检查没有:
- webSecurity:false;
- allowRunningInsecureContent:true;
- 为兼容跨域而永久关闭浏览器安全模型。
开发环境 CORS 问题应该修 proxy/server policy,而不是关 webSecurity。
11. Permission Requests
加载 remote content 的 session 应设置 permission request handler。
明确决定:
- camera;
- microphone;
- notifications;
- clipboard;
- geolocation;
- display capture 等。
不要让网页默认获得不需要的权限。
12. Custom Protocol 优先于 file://
Electron 当前安全指南建议考虑自定义 protocol,而不是完全依赖 file://。
原因包括:
- 更清晰的 origin/security semantics;
- 更容易做 CSP;
- 更可控 resource mapping。
如果继续 file://,要明确其特殊 origin/path 行为。
13. Electron Version
Electron 打包了:
- Chromium;
- Node.js;
- Electron runtime。
因此过旧 Electron 会同时积累多个 runtime 漏洞。
检查:
- 当前 major;
- security support;
- breaking changes;
- upgrade policy。
不要长期冻结在旧版。
14. Dependencies
检查:
- NPM dependency CVE;
- native module;
- abandoned package;
- postinstall;
- supply-chain risk。
Electron 的本地权限意味着 dependency compromise 后果可能比普通网页严重。
15. Fuses
检查是否可以通过 Electron Fuses 进一步关闭生产不需要的能力,例如与:
- Node options;
- run-as-node;
- archive integrity;
相关的运行时行为。
具体 fuse 取值应根据当前 Electron 版本和发布策略验证,不要盲抄模板。
16. Dev / Prod Boundary
开发环境:
- localhost;
- HMR;
- DevTools;
- source maps;
不应自动意味着生产也保留相同暴露面。
检查 production build 是否:
- 去掉调试入口;
- 使用正确 CSP;
- 使用签名/公证;
- 固定 update source;
- 不信任 dev server。
17. 代码签名与自动更新
安全检查还应覆盖:
- app code signing;
- macOS notarization;
- updater signature;
- update feed TLS;
- update channel authorization。
否则安全的 renderer 仍可能被供应链更新路径破坏。
无提示执行检查
对一个真实 Electron 项目,不看正文完成:
- 枚举所有 BrowserWindow/WebContentsView;
- 记录每个窗口的 nodeIntegration/contextIsolation/sandbox/preload;
- 列出 preload 暴露 API;
- 列出所有 ipcMain.handle/on,并检查 sender/input validation;
- 列出 navigation/window.open/openExternal 入口;
- 检查 CSP 和 permission handlers;
- 检查当前 Electron 版本、fuses、code signing/updater;
- 输出一张“能力 → 信任来源 → validation → privileged action”的审计表。