【问题标题】:Security warning in the console of BrowserWindow (electron ^9.2.0)BrowserWindow 控制台中的安全警告(电子^9.2.0)
【发布时间】:2020-12-05 04:38:02
【问题描述】:

我是 electron 的新手,我在我打开的每个 BrowserWindow 的控制台(DevTools)中都得到了这个:

webFrame.executeJavaScript was called without worldSafeExecuteJavaScript enabled. This is considered unsafe. worldSafeExecuteJavaScript will be enabled by default in Electron 12.

我还收到了另一个安全警告,那就是:

Electron Security Warning (Insecure Content-Security-Policy) This renderer process has either no Content Security
    Policy set or a policy with "unsafe-eval" enabled. This exposes users of
    this app to unnecessary security risks.

我不知道我到底做错了什么……!!

This is my console window

This is my package.json

【问题讨论】:

    标签: javascript electron


    【解决方案1】:

    webFrame.executeJavaScript 和 contextIsolation

    将以下设置添加到 main.js 中的 BrowserWindow

    webPreferences { worldSafeExecuteJavaScript: true, contextIsolation: true }
    

    参考见:

    不安全的内容安全策略

    如果您在本地加载,请将以下内容添加到您的 index.html 和任何其他 html 页面的头部

    <meta http-equiv="Content-Security-Policy" content="script-src 'self'">
    

    参考见:

    【讨论】:

    • 只有将 contextIsolation 设置为 true 时,警告才会消失。
    【解决方案2】:

    首先添加一个 CSP 策略,如下所示:

        <meta http-equiv="Content-Security-Policy" content="default-src 'self'">
    

    这可能会破坏一些脚本,但最好这样做。你也可以启用 unsafe-inline

    1. 查看 electron 的安全建议

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 2015-11-26
      • 2017-01-19
      • 1970-01-01
      相关资源
      最近更新 更多