繁体   English   中英

Electron IPC 与节点集成

[英]Electron IPC and nodeIntegration

因此,我遵循了许多指南来设置 Webpack、Electron 和 React 来制作桌面应用程序。 完成设置后,我开始工作,并了解到我需要来自主程序和渲染器的 IPC 机制才能进行通信。

import {ipcRenderer} from "electron"; 将此添加到我的 renderer.js 文件会导致错误Uncaught ReferenceError: require is not defined

把我的问题交给一些同事后,有人建议我应该在我的main.js文件中更改

webPreferences: {
    nodeIntegration: false,
}

webPreferences: {
    nodeIntegration: true,
}

我在谷歌上读到的任何地方都非常清楚地表明,如果安全是您关心的事情,那么这不是您应该做的事情。 但是,我能够遇到的每个电子 ipc 资源都使用了 ipcRenderer。

现在,互联网上的每个例子都存在巨大的安全漏洞,还是我在这里遗漏了一些关键部分?

我的问题如下。

  1. 是否可以在不启用 nodeIntegration 的情况下使用 ipcRenderer?
  2. 如果是,我该怎么做,为什么这么多资源会排除这些信息?
  3. 如果不是,我用什么?

如果我问错了问题,或者我错过了什么,或者我问这个问题的方式有任何其他明显的问题,请告诉我,否则提前致谢。

  1. 是否可以在不启用 nodeIntegration 的情况下使用 ipcRenderer?

这是可能的,但很繁琐。 它可以通过使用preload脚本来完成。

  1. 如果是,我该怎么做,为什么这么多资源会排除这些信息?

可以使用如下所示的preload脚本。 但是,不被认为是安全的 大多数现有文档没有显示最佳安全实践。

后面给出了一个更安全的例子。

// preload.js
const electron = require('electron');

process.once('loaded', () => {
  global.ipcRenderer = electron.ipcRenderer;
});
// main.js
const {app, BrowserWindow} = require('electron');

app.on('ready', () => {
  // Create the browser window.
  win = new BrowserWindow({
      backgroundColor: '#fff', // always set a bg color to enable font antialiasing!
      webPreferences: {
        preload: path.join(__dirname, './preload.js'),
        nodeIntegration: false,
        enableRemoteModule: false,
        // contextIsolation: true,
        // nativeWindowOpen: true,
        // sandbox: true,
      }
  });
  win.loadURL(`file://${path.join(__dirname, 'index.html')}`);

注意预加载脚本的路径必须是绝对路径,这在使用 webpack/babel 时也会变得复杂,因为输出文件可能是不同的路径。

  1. 如果不是,我用什么?

编辑正如@Yannic 指出的那样,Electron 现在支持另一个选项,称为contextBridge 这个新选项可以更简单地解决问题。 有关contextBridge信息,请查看电子文档: https : contextBridge

但是,即使使用contextBridge您也不应该尝试公开整个电子 API,而只是为您的应用程序设计的有限 API

如上所述,虽然可以使用如上所示的 ipcRenderer,但当前的电子安全建议还建议启用contextIsolation 这将使上述方法无法使用,因为您无法再向全局范围添加数据。

最安全的建议,AFAIK 是使用addEventListenerpostMessage代替,并使用预加载脚本作为渲染器和主脚本之间的桥梁。

// preload.js
const { ipcRenderer } = require('electron');

process.once('loaded', () => {
  window.addEventListener('message', event => {
    // do something with custom event
    const message = event.data;

    if (message.myTypeField === 'my-custom-message') {
      ipcRenderer.send('custom-message', message);
    }
  });
});
// main.js
const {app, ipcMain, BrowserWindow} = require('electron');

app.on('ready', () => {
  ipcMain.on('custom-message', (event, message) => {
    console.log('got an IPC message', e, message);
  });

  // Create the browser window.
  win = new BrowserWindow({
      backgroundColor: '#fff', // always set a bg color to enable font antialiasing!
      webPreferences: {
        preload: path.join(__dirname, './preload.js'),
        nodeIntegration: false,
        enableRemoteModule: false,
        contextIsolation: true,
        sandbox: true,
        // nativeWindowOpen: true,
      }
  });
  win.loadURL(`file://${path.join(__dirname, 'index.html')}`);
// renderer.js
window.postMessage({
  myTypeField: 'my-custom-message',
  someData: 123,
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM