【问题标题】:Should I use Context Isolation with my Electron App我应该在我的电子应用程序中使用上下文隔离吗
【发布时间】:2020-12-28 17:44:22
【问题描述】:

我和我的朋友几乎完成了our project。它基本上是用于 Spotify 的正在播放/迷你播放器应用程序。当我检查渲染进程控制台时,我又收到了一个想要清除的警告。这是关于worldSafeExecuteJavaScript 是真实的以及它是如何不安全的。我仔细看了看,结果发现我也需要打开contextIsolation。我进行了更多研究,发现我不能在渲染过程中使用require。我尝试查找文档,但我很困惑。从长远来看,我们希望我们的应用程序安全,特别是因为我还在上大学,而我的朋友正在研究它可能会因为他的学校增加而忙碌;而且因为 contextIsolation 在 Electron 12 中默认为真。

在我们的 javascript 渲染过程中,我们只需要 jQuery 和 ipcRenderer。我们如何合并contextBridge(我认为这就是它的名称),将 jQuery 和 ipcRenderer 导入我们的 javascript 以进行渲染过程?

【问题讨论】:

    标签: javascript electron


    【解决方案1】:

    我通过使用contextBridge 让我的应用程序与contextIsolation 一起工作。这是我的 main.js webPreferences 和预加载脚本:

        webPreferences: {
          worldSafeExecuteJavaScript: true,
          contextIsolation: true,
          preload: path.join(__dirname, "preload.js")
        },
    

    这里是preload.js 来导入ipcRenderer

    const { contextBridge, ipcRenderer } = require('electron')
    
    contextBridge.exposeInMainWorld(
        'ipcRenderer',
        {
    /*
    Important note: This will get it working, 
    but I'm going to make multiple methods for 
    each time I'm using this to heighten security.
    https://www.electronjs.org/docs/tutorial/context-isolation#security-considerations
    */
          send: (channel, arg) => ipcRenderer.send(channel, arg),
          on: (event, data) => ipcRenderer.on(event, data)
        }
    )
    

    To import jQuery,我只是用index.html导入的:

      <head>
        <meta charset="UTF-8">
        <title>Gemini</title>
        <meta http-equiv="Content-Security-Policy" content="script-src 'self'">
        <link rel="stylesheet" href="style.css">
        <link rel="stylesheet" href="macos.css">
        <script type="text/javascript" src="node_modules/jquery/dist/jquery.min.js"></script>
        <link rel="stylesheet" href="node_modules/@fortawesome/fontawesome-free/css/all.css">
      </head>
    

    我不确定通过 HTML 导入 jQuery 是否更安全。

    【讨论】:

      猜你喜欢
      • 2021-08-20
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多