【问题标题】:Electron Confusion about Security电子对安全的困惑
【发布时间】:2021-05-12 08:02:42
【问题描述】:

好吧,我只是迷路了。我打开了一个电子启动应用程序并添加了一个简单的代码,以控制台登录按钮按下。该函数位于文件 renderer.js 中,并在我的 index.html 中被调用。为什么当我按下按钮 我收到这条消息....

    Refused to execute inline event handler because it violates the following Content
 Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a
 hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

为什么不允许我内联执行某些内容?外部来源似乎更容易受到攻击。谁能帮我解决这个问题?

【问题讨论】:

  • 没有你的代码,没有人能理解为什么会出现这个错误。

标签: javascript security electron


【解决方案1】:

内容安全策略用于避免浏览器中基于 XSS 的攻击。在电子中,后台运行在 chrome 引擎中,因此代码实际上是在 chromium 浏览器中运行。该浏览器容易出现浏览器存在的所有安全问题有

什么是内容安全策略?

Content-Security-Policy 是现代浏览器用来增强文档(或网页)安全性的 HTTP 响应标头的名称。 Content-Security-Policy 标头允许您限制资源(例如 JavaScript、CSS 或浏览器加载的几乎任何内容)的方式。

您可以在 HTML 顶部添加以下代码以避免内容安全问题。这个新标头将允许内联代码执行

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

这个元标记会做什么?

允许

使用上述 CSP 策略,允许在浏览器中加载和执行以下内容:

<!-- allowed by 'self' -->
<script src="/js/some-file.js"></script>
<!-- allowed by https://js.example.com -->
<script src="https://js.example.com/file.js"></script>

方块

上面的示例策略将阻止以下内容在浏览器中加载或执行:

<script src="https://attacker.example.com/file.js"></script>

【讨论】:

    猜你喜欢
    • 2018-07-25
    • 1970-01-01
    • 2021-07-30
    • 2012-12-22
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多