javascript - 在 Electron 中设置 CSP 元标记时 "Content Security Policy of your site blocks the use of ' eval ' in JavaScript"警告

标签 javascript electron google-chrome-devtools content-security-policy

我正在创建一个 Electron 应用程序,并且根据 Electron security tutorial我添加了一个 CSP 元标记。运行应用程序时,此问题出现在 devtools 中。

Content Security Policy of your site blocks the use of 'eval' in JavaScript

The Content Security Policy (CSP) prevents the evaluation of arbitrary strings as JavaScript to make it more difficult for an attacker to inject unauthorized code on your site.

To solve this issue, avoid using eval(), new Function(), setTimeout([string], ...) and setInterval([string], ...) for evaluating strings.


没有 eval我自己的代码中存在调用或其他字符串评估情况。这个问题没有提供任何关于是什么代码导致它的线索,我尝试使用 'report-sample'值对输出没有影响。在 Chrome 中打开 HTML 文件时不会出现此问题。
我可以用一个非常基本的应用程序重新创建警告。
main.js
const path = require("path");
const { app, BrowserWindow } = require("electron");

const createWindow = () => {
  let mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      contextIsolation: true,
      nodeIntegration: false,
    }
  });

  mainWindow.loadURL(`file://${path.join(__dirname, "/index.html")}`);
};

app.on("ready", createWindow);
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="Content-Security-Policy" content="default-src 'self'">
  <title>Document</title>
</head>
<body>
  <h1>CSP Issue Test</h1>
</body>
</html>
我想了解为什么会出现此问题并解决它,而不仅仅是抑制警告。

最佳答案

基于 Electron Github repo 的 issues , "此日志消息目前预计在开发中,如果您运行打包的应用程序,它将不会出现。"这是根据 Electron 贡献者之一的说法。
这个问题现在已经关闭,但它仍然活跃,一些用户说它有点令人困惑(我同意)。基于此,我认为我们只是在开发过程中忽略了它。当根据贡献者打包应用程序时,它应该会消失,尽管我自己没有对此进行测试。

关于javascript - 在 Electron 中设置 CSP 元标记时 "Content Security Policy of your site blocks the use of ' eval ' in JavaScript"警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65445255/

相关文章:

javascript - 重定向到数据 : 时 chrome.webRequest 跨源不允许方案

javascript - 根据隐藏的输入类型在按钮单击时生成警报

javascript - jquery - .each() 的每次迭代增加超时间隔

javascript - 输入字段显示 NaN

electron - Electron 自动更新程序未安装在OSx中

express - 如何分发使用 Electron、Node.js 和 Python 构建的桌面应用程序

javascript - 为什么有这么多依赖项?

javascript - 在 Electron Node 中创建 websocket 服务器以供外部客户端请求

google-chrome-devtools - 在 Chrome DevTools 的 Sources 中移动 'debugger' 面板?

google-chrome-devtools - Chrome 是否仍支持 SPDY?