Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 电子安全的困惑_Javascript_Security_Electron - Fatal编程技术网

Javascript 电子安全的困惑

Javascript 电子安全的困惑,javascript,security,electron,Javascript,Security,Electron,好吧,我只是迷路了。我打开了一个electron start应用程序,并添加了一个简单的代码,即控制台登录按钮。该函数位于文件renderer.js中,并在my index.html中被调用。为什么我按下按钮 我收到这个消息 Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self'

好吧,我只是迷路了。我打开了一个electron start应用程序,并添加了一个简单的代码,即控制台登录按钮。该函数位于文件renderer.js中,并在my 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.

为什么不允许我内联执行某些操作?外部来源似乎更容易受到攻击。有人能帮我澄清一下吗?

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

什么是内容安全策略?

内容安全策略是现代浏览器用来增强文档(或网页)安全性的HTTP响应头的名称。内容安全策略头允许您限制资源(如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>

上述示例策略将阻止在浏览器中加载或执行以下操作:

<!-- 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>

如果没有您的代码,没有人能理解为什么会发生此错误。