安全
安全性,原生能力和你的责任
Web开发人员通常享有浏览器强大的网络安全特性,而自己的代码风险相对较小。 Our websites are granted limited powers in a sandbox, and we trust that our users enjoy a browser built by a large team of engineers that is able to quickly respond to newly discovered security threats.
当使用 Electron 时,很重要的一点是要理解 Electron 不是一个 Web 浏览器。 它允许您使用熟悉的 Web 技术构建功能丰富的桌面应用程序,但是您的代码具有更强大的功能。 JavaScript 可以访问文件系统,用户 shell 等。 这允许您构建更高质量的本机应用程序,但是内在的安全风险会随着授予您的代码的额外权力而增加。
考虑到这一点,请注意,展示任意来自不受信任源的内容都将会带来严重的安全风险,而这种风险Electron也没打算处理。 事实上,最流行的 Electron 应用程序(Atom,Slack,Visual Studio Code 等) 主要显示本地内容(即使有远程内容也是无 Node 的、受信任的、安全的内容) - 如果您的应用程序要运行在线的源代码,那么您需要确保源代码不是恶意的。
报告安全问题
有关如何正确上报 Electron 漏洞的信息,参阅 SECURITY.md
Chromium 安全问题和升级
Electron keeps up to date with alternating Chromium releases. For more information, see the Electron Release Cadence blog post.
安全是所有人的共同责任
It is important to remember that the security of your Electron application is the result of the overall security of the framework foundation (Chromium, Node.js), Electron itself, all NPM dependencies and your code. 因此,你有责任遵循下列安全守则:
- 使用最新版的 Electron 框架搭建你的程序。你最终发行的产品中会包含 Electron、Chromium 共享库和 Node.js 的组件。 这些组件存在的安全问题也可能影响你的程序安全性。 你可以通过更新Electron到最新版本来确保像是nodeIntegration绕过攻击一类的严重漏洞已经被修复因而不会影响到你的程序。 请参阅“使用当前版本的Electron”以获取更多信息。
- 评估你的依赖项目NPM提供了五百万可重用的软件包,而你应当承担起选择可信任的第三方库。 如果你使用了受已知漏洞的过时的库,或是依赖于维护的很糟糕的代码,你的程序安全就可能面临威胁。
- 遵循安全编码实践你的代码是你的程序安全的第一道防线。 一般的网络漏洞,例如跨站脚本攻击(Cross-Site Scripting, XSS),对Electron将造成更大的影响,因此非常建议你遵循安全软件开发最佳实践并进行安全性测试。
隔离不信任的内容
每当你从不被信任的来源(如一个远程服务器)获取代码并在本地执行,其中就存在安全性问题。 例如在默认的 BrowserWindow中显示一个远程网站. 如果攻击者以某种方式设法改变所述内容 (通过直接攻击源或者通过在应用和实际目的地之间进行攻击) ,他们将能够在用户的机器上执行本地代码。
:warning:无论如何,在启用Node.js集成的情况下,你都不该加载并执行远程代码。 相反,只使用本地文件(和您的应用打包在一起)来执行Node.js代码 To display remote content, use the <webview> tag or BrowserView, make sure to disable the nodeIntegration and enable contextIsolation.
Electron 安全警告
从Electron 2.0版本开始,开发者将会在开发者控制台看到打印的警告和建议。 这些警告仅在可执行文件名为 Electron 时才会为开发者显示。
你可以通过在process.env 或 window对象上配置ELECTRON_ENABLE_SECURITY_WARNINGS 或ELECTRON_DISABLE_SECURITY_WARNINGS来强制开启或关闭这些警告。
清单:安全建议
为加强程序安全性,你至少应当遵循下列规则:
-
只加载安全的内容
-
禁止在所有渲染器中使用Node.js集成显示远程内容
-
做所有显示远程内容的渲染器中启用上下文隔离。
-
在所有加载远程内容的会话中使用 ses.setPermissionRequestHandler().
-
不要禁用 webSecurity
-
定义一个Content-Security-Policy并设置限制规则(如:script-src 'self')
-
不要设置 allowRunningInsecureContent 为 true.
-
不要开启实验性功能
-
不要使用enableBlinkFeatures
-
<webview>:不要使用 allowpopups
-
<webview>:验证选项与参数
-
禁用或限制网页跳转
-
禁用或限制新窗口创建
-
不要对不可信的内容使用 openExternal
-
禁用 remote 模块
-
限制 remote 模块
-
使用当前版本的 Electron
To automate the detection of misconfigurations and insecure patterns, it is possible to use electronegativity. For additional details on potential weaknesses and implementation bugs when developing applications using Electron, please refer to this guide for developers and auditors
1) 仅加载安全内容
任何不属于你的应用的资源都应该使用像HTTPS这样的安全协议来加载。 换言之, 不要使用不安全的协议 (如 HTTP)。 同理,我们建议使用WSS,避免使用WS,建议使用FTPS ,避免使用FTP,等等诸如此类的协议。
为什么?
HTTPS 有三个主要好处:
1) 它对远程服务器进行身份验证, 确保您的应用程序连接到正确的主机而不是模仿器。 2) 确保数据完整性, 断言数据在应用程序和主机之间传输时未被修改。 3) 它对用户和目标主机之间的通信进行加密, 从而更难窃听应用程序和主机之间发送的信息。
怎么做?
// 不推荐browserWindow.loadURL ('http://example.com')// 推荐 browserWindow.loadURL ('https://example.com')
<!-- Bad --><script crossorigin src="http://example.com/react.js"></script><link rel="stylesheet" href="http://example.com/style.css"><!-- Good --><script crossorigin src="https://example.com/react.js"></script><link rel="stylesheet" href="https://example.com/style.css">
2) Do not enable Node.js Integration for Remote Content
This recommendation is the default behavior in Electron since 5.0.0.
It is paramount that you do not enable Node.js integration in any renderer (BrowserWindow, BrowserView, or <webview>) that loads remote content. 其目的是限制您授予远程内容的权限, 从而使攻击者在您的网站上执行 JavaScript 时更难伤害您的用户。
在此之后,你可以为指定的主机授予附加权限。 For example, if you are opening a BrowserWindow pointed at https://example.com/, you can give that website exactly the abilities it needs, but no more.
为什么?
如果攻击者跳过渲染进程并在用户电脑上执行恶意代码,那么这种跨站脚本(XSS) 攻击的危害是非常大的。 跨站脚本攻击很常见,通常情况下,威力仅限于执行代码的网站。 禁用Node.js集成有助于防止XSS攻击升级为“远程代码执行” (RCE) 攻击。
怎么做?
// Badconst mainWindow = new BrowserWindow({ webPreferences: { nodeIntegration: true, nodeIntegrationInWorker: true }})mainWindow.loadURL('https://example.com')
// Goodconst mainWindow = new BrowserWindow({ webPreferences: { preload: path.join(app.getAppPath(), 'preload.js') }})mainWindow.loadURL('https://example.com')
<!-- Bad --><webview nodeIntegration src="page.html"></webview><!-- Good --><webview src="page.html"></webview>
当禁用Node.js集成时,你依然可以暴露API给你的站点以使用Node.js的模块功能或特性。 预加载脚本依然可以使用require等Node.js特性, 以使开发者可以暴露自定义API给远程加载内容。
在下面的预加载脚本例子中,后加载的网站内容可以使用window.readConfig()方法,但不能使用Node.js特性。
const { readFileSync } = require('fs')window.readConfig = function () { const data = readFileSync('./config.json') return data}
3) 为远程内容开启上下文隔离
上下文隔离是Electron的一个特性,它允许开发者在预加载脚本里运行代码,里面包含Electron API和专用的JavaScript上下文。 实际上,这意味全局对象如 Array.prototype.push 或 JSON.parse等无法被渲染进程里的运行脚本修改。
Electron使用了和Chromium相同的Content Scripts技术来开启这个行为。
Even when you use nodeIntegration: false to enforce strong isolation and prevent the use of Node primitives, contextIsolation must also be used.
Why & How?
For more information on what contextIsolation is and how to enable it please see our dedicated Context Isolation document.
4) 处理来自远程内容的会话许可请求
当你使用Chromes时,也许见过这种许可请求:每当网站尝试使用某个特性时,就会弹出让用户手动确认(如网站通知)
此API基于Chromium permissions API,并已实现对应的许可类型。
为什么?
默认情况下,Electron将自动批准所有的许可请求,除非开发者手动配置一个自定义处理函数。 尽管默认如此,有安全意识的开发者可能希望默认反着来。
怎么做?
const { session } = require('electron')session .fromPartition('some-partition') .setPermissionRequestHandler((webContents, permission, callback) => { const url = webContents.getURL() if (permission === 'notifications') { // Approves the permissions request callback(true) } // Verify URL if (!url.startsWith('https://example.com/')) { // Denies the permissions request return callback(false) } })
5) 不要禁用WebSecurity
Electron的默认值就是建议值。
在渲染进程(BrowserWindow、BrowserView 和 <webview>)中禁用 webSecurity 将导致至关重要的安全性功能被关闭。
不要在生产环境中禁用webSecurity。
为什么?
禁用 webSecurity 将会禁止同源策略并且将 allowRunningInsecureContent 属性置 true。 换句话说,这将使得来自其他站点的非安全代码被执行。
怎么做?
// 不推荐const mainWindow = new BrowserWindow({ webPreferences: { webSecurity: false }})
// 推荐const mainWindow = new BrowserWindow()
<!-- Bad --><webview disablewebsecurity src="page.html"></webview><!-- Good --><webview src="page.html"></webview>
6) 定义一个内容安全策略
内容安全策略(CSP) 是应对跨站脚本攻击和数据注入攻击的又一层保护措施。 我们建议任何载入到Electron的站点都要开启。
为什么?
CSP允许Electron通过服务端内容对指定页面的资源加载进行约束与控制。 如果你定义https://example.com这个源,所属这个源的脚本都允许被加载,反之https://evil.attacker.com不会被允许加载运行。 对于提升你的应用安全性,设置CSP是个很方便的办法。
下面的CSP设置使得Electron只能执行自身站点和来自apis.example.com的脚本。
// 不推荐Content-Security-Policy: '*'// 推荐Content-Security-Policy: script-src 'self' https://apis.example.com
CSP HTTP头
Electron 会处理 Content-Security-Policy HTTP 标头,它可以在 webRequest.onHeadersReceived 中进行设置:
const { session } = require('electron')session.defaultSession.webRequest.onHeadersReceived((details, callback) => { callback({ responseHeaders: { ...details.responseHeaders, 'Content-Security-Policy': ['default-src \'none\''] } })})
CSP元标签
CSP的首选传递机制是HTTP报头,但是在使用file://协议加载资源时,不可能使用此方法。 It can be useful in some cases, such as using the file:// protocol, to set a policy on a page directly in the markup using a <meta> tag:
<meta http-equiv="Content-Security-Policy" content="default-src 'none'">
7) 不要设置allowRunningInsecureContent为true
Electron的默认值就是建议值。
默认情况下,Electron不允许网站在HTTPS中加载或执行非安全源(HTTP) 中的脚本代码、CSS或插件。 将allowRunningInsecureContent属性设为true将禁用这种保护。
当网站的初始内容通过HTTPS加载并尝试在子请求中加载HTTP的资源时,这被称为”混合内容”。
为什么?
通过HTTPS加载会将该资源进行加密传输,以保证其真实性和完整性。 参看只显示安全内容这节以获得更多信息。
