安全·续1

// 不推荐const mainWindow = new BrowserWindow({  webPreferences: {    allowRunningInsecureContent: true  }})
// 推荐const mainWindow = new BrowserWindow({})

8) 不要开启实验室特性

Electron的默认值就是建议值。

Electron 的熟练用户可以通过 experimentalFeatures 属性来启用 Chromium 实验性功能。

为什么?

Experimental features are, as the name suggests, experimental and have not been enabled for all Chromium users. Furthermore, their impact on Electron as a whole has likely not been tested.

尽管存在合理的使用场景,但是除非你知道你自己在干什么,否则你不应该开启这个属性。

怎么做?

// 不推荐const mainWindow = new BrowserWindow({  webPreferences: {    experimentalFeatures: true  }})
// 推荐const mainWindow = new BrowserWindow({})

9) 不要使用enableBlinkFeatures

Electron的默认值就是建议值。

Blink是Chromium里的渲染引擎名称。 就像experimentalFeatures一样,enableBlinkFeatures属性将使开发者启用被默认禁用的特性。

为什么?

通常来说,某个特性默认不被开启肯定有其合理的原因。 针对特定特性的合理使用场景是存在的。 作为开发者,你应该非常明白你为何要开启它,有什么后果,以及对你应用安全性的影响。 在任何情况下都不应该推测性的开启特性。

怎么做?

// Badconst mainWindow = new BrowserWindow({  webPreferences: {    enableBlinkFeatures: 'ExecCommandInJavaScript'  }})
// 推荐const mainWindow = new BrowserWindow()

10) 不要使用allowpopups

Electron的默认值就是建议值。

如果您正在使用 <​webview> ,您可能需要页面和脚本加载进您的 <​webview> 标签以打开新窗口。 开启allowpopups属性将使得BrowserWindows可以通过window.open()方法创建。 否则, <​webview> 标签内不允许创建新窗口。

为什么?

如果你不需要弹窗,最好使用默认值以关闭新BrowserWindows的创建。 以下是最低的权限要求原则:若非必要,不要再网站中创建新窗口。

怎么做?

<​!-- Bad --><​webview allowpopups src="page.html"><​/webview><​!-- Good --><​webview src="page.html"><​/webview>

11) 创建WebView前确认其选项

通过渲染进程创建的WebView是不开启Node.js集成的,且也不能由自身开启。 但是,WebView可以通过其webPreferences属性创建一个独立的渲染进程。

It is a good idea to control the creation of new <​webview> tags from the main process and to verify that their webPreferences do not disable security features.

为什么?

Since <​webview> live in the DOM, they can be created by a script running on your website even if Node.js integration is otherwise disabled.

Electron 可以让开发者关闭各种控制渲染进程的安全特性。 通常情况下,开发者并不需要关闭他们中的任何一种 - 因此你不应该允许创建不同配置的<​webview>标签

怎么做?

在 <​webview>标签生效前,Electron将产生一个will-attach-webview事件到webContents中。 利用这个事件来阻止可能含有不安全选项的 webViews 创建。

app.on('web-contents-created', (event, contents) => {  contents.on('will-attach-webview', (event, webPreferences, params) => {    // Strip away preload scripts if unused or verify their location is legitimate    delete webPreferences.preload    delete webPreferences.preloadURL    // Disable Node.js integration    webPreferences.nodeIntegration = false    // Verify URL being loaded    if (!params.src.startsWith('https://example.com/')) {      event.preventDefault()    }  })})

Again, this list merely minimizes the risk, it does not remove it. If your goal is to display a website, a browser will be a more secure option.

12) Disable or limit navigation

If your app has no need to navigate or only needs to navigate to known pages, it is a good idea to limit navigation outright to that known scope, disallowing any other kinds of navigation.

为什么?

Navigation is a common attack vector. If an attacker can convince your app to navigate away from its current page, they can possibly force your app to open web sites on the Internet. Even if your webContents are configured to be more secure (like having nodeIntegration disabled or contextIsolation enabled), getting your app to open a random web site will make the work of exploiting your app a lot easier.

A common attack pattern is that the attacker convinces your app’s users to interact with the app in such a way that it navigates to one of the attacker’s pages. This is usually done via links, plugins, or other user-generated content.

怎么做?

If your app has no need for navigation, you can call event.preventDefault() in a will-navigate handler. If you know which pages your app might navigate to, check the URL in the event handler and only let navigation occur if it matches the URLs you’re expecting.

We recommend that you use Node’s parser for URLs. Simple string comparisons can sometimes be fooled - a startsWith('https://example.com') test would let https://example.com.attacker.com through.

const URL = require('url').URLapp.on('web-contents-created', (event, contents) => {  contents.on('will-navigate', (event, navigationUrl) => {    const parsedUrl = new URL(navigationUrl)    if (parsedUrl.origin !== 'https://example.com') {      event.preventDefault()    }  })})

13) 禁用或限制新窗口的创建

If you have a known set of windows, it’s a good idea to limit the creation of additional windows in your app.

为什么?

Much like navigation, the creation of new webContents is a common attack vector. Attackers attempt to convince your app to create new windows, frames, or other renderer processes with more privileges than they had before; or with pages opened that they couldn’t open before.

If you have no need to create windows in addition to the ones you know you’ll need to create, disabling the creation buys you a little bit of extra security at no cost. This is commonly the case for apps that open one BrowserWindow and do not need to open an arbitrary number of additional windows at runtime.

怎么做?

webContents will emit the new-window event before creating new windows. That event will be passed, amongst other parameters, the url the window was requested to open and the options used to create it. We recommend that you use the event to scrutinize the creation of windows, limiting it to only what you need.

const { shell } = require('electron')app.on('web-contents-created', (event, contents) => {  contents.on('new-window', async (event, navigationUrl) => {    // In this example, we'll ask the operating system    // to open this event's url in the default browser.    event.preventDefault()    await shell.openExternal(navigationUrl)  })})

14) Do not use openExternal with untrusted content

Shell’s openExternal allows opening a given protocol URI with the desktop’s native utilities. On macOS, for instance, this function is similar to the open terminal command utility and will open the specific application based on the URI and filetype association.

为什么?

Improper use of openExternal can be leveraged to compromise the user’s host. When openExternal is used with untrusted content, it can be leveraged to execute arbitrary commands.

怎么做?

//  Badconst { shell } = require('electron')shell.openExternal(USER_CONTROLLED_DATA_HERE)
//  Goodconst { shell } = require('electron')shell.openExternal('https://example.com/index.html')

15) Disable the remote module

The remote module provides a way for the renderer processes to access APIs normally only available in the main process. Using it, a renderer can invoke methods of a main process object without explicitly sending inter-process messages. If your desktop application does not run untrusted content, this can be a useful way to have your renderer processes access and work with modules that are only available to the main process, such as GUI-related modules (dialogs, menus, etc.).

However, if your app can run untrusted content and even if you sandbox your renderer processes accordingly, the remote module makes it easy for malicious code to escape the sandbox and have access to system resources via the higher privileges of the main process. Therefore, it should be disabled in such circumstances.

为什么?

remote uses an internal IPC channel to communicate with the main process. “Prototype pollution” attacks can grant malicious code access to the internal IPC channel, which can then be used to escape the sandbox by mimicking remote IPC messages and getting access to main process modules running with higher privileges.

Additionally, it’s possible for preload scripts to accidentally leak modules to a sandboxed renderer. Leaking remote arms malicious code with a multitude of main process modules with which to perform an attack.