安全·续2
Disabling the remote module eliminates these attack vectors. Enabling context isolation also prevents the “prototype pollution” attacks from succeeding.
怎么做?
// Bad if the renderer can run untrusted contentconst mainWindow = new BrowserWindow({})
// Goodconst mainWindow = new BrowserWindow({ webPreferences: { enableRemoteModule: false }})
<!-- Bad if the renderer can run untrusted content --><webview src="page.html"></webview><!-- Good --><webview enableremotemodule="false" src="page.html"></webview>
16) Filter the remote module
If you cannot disable the remote module, you should filter the globals, Node, and Electron modules (so-called built-ins) accessible via remote that your application does not require. This can be done by blocking certain modules entirely and by replacing others with proxies that expose only the functionality that your app needs.
为什么?
Due to the system access privileges of the main process, functionality provided by the main process modules may be dangerous in the hands of malicious code running in a compromised renderer process. By limiting the set of accessible modules to the minimum that your app needs and filtering out the others, you reduce the toolset that malicious code can use to attack the system.
Note that the safest option is to fully disable the remote module. If you choose to filter access rather than completely disable the module, you must be very careful to ensure that no escalation of privilege is possible through the modules you allow past the filter.
怎么做?
const readOnlyFsProxy = require(/* ... */) // exposes only file read functionalityconst allowedModules = new Set(['crypto'])const proxiedModules = new Map(['fs', readOnlyFsProxy])const allowedElectronModules = new Set(['shell'])const allowedGlobals = new Set()app.on('remote-require', (event, webContents, moduleName) => { if (proxiedModules.has(moduleName)) { event.returnValue = proxiedModules.get(moduleName) } if (!allowedModules.has(moduleName)) { event.preventDefault() }})app.on('remote-get-builtin', (event, webContents, moduleName) => { if (!allowedElectronModules.has(moduleName)) { event.preventDefault() }})app.on('remote-get-global', (event, webContents, globalName) => { if (!allowedGlobals.has(globalName)) { event.preventDefault() }})app.on('remote-get-current-window', (event, webContents) => { event.preventDefault()})app.on('remote-get-current-web-contents', (event, webContents) => { event.preventDefault()})
17) 使用当前版本的 Electron
You should strive for always using the latest available version of Electron. Whenever a new major version is released, you should attempt to update your app as quickly as possible.
为什么?
An application built with an older version of Electron, Chromium, and Node.js is an easier target than an application that is using more recent versions of those components. Generally speaking, security issues and exploits for older versions of Chromium and Node.js are more widely available.
Both Chromium and Node.js are impressive feats of engineering built by thousands of talented developers. Given their popularity, their security is carefully tested and analyzed by equally skilled security researchers. Many of those researchers disclose vulnerabilities responsibly, which generally means that researchers will give Chromium and Node.js some time to fix issues before publishing them. Your application will be more secure if it is running a recent version of Electron (and thus, Chromium and Node.js) for which potential security issues are not as widely known.
