javascript - Content-Security-Policy HTTP header 无法正常用于脚本源

标签 javascript http security browser http-headers

我正在关注 Google's strict CSP policy对于我的 CSP header ,我正确地添加了 nonce,但是我的脚本在浏览器中不断收到此错误:

Refused to load the script 'http://localhost:8080/client/dist/inline.6e0c61259742e86be1dd.bundle.js' because it violates the following Content Security Policy directive: "script-src nonce-XVlBzgbaiCMRAjWwhTHctcuAxhxKQFDaFpLSjFbcXoEFfRsWxP 'unsafe-inline' 'unsafe-eval' 'strict-dynamic' https: http:". 'strict-dynamic' is present, so host-based whitelisting is disabled.

如您所见,随机数与脚本中的随机数匹配:

<script type="text/javascript" src="/client/dist/inline.6e0c61259742e86be1dd.bundle.js" nonce="XVlBzgbaiCMRAjWwhTHctcuAxhxKQFDaFpLSjFbcXoEFfRsWxP"></script>

这是我为 script-src 使用的 CSP header 。 "%s"表示随机生成的随机数,它将在响应中设置 header 之前进行插值:

script-src nonce-%s 'unsafe-inline' 'unsafe-eval' 'strict-dynamic' https: http:;

我在这里做错了什么?

最佳答案

看起来您没有用单引号将 nonce 值括起来——而不是 nonce-%s 它应该是 'nonce-%s' 在您的政策。

由于您使用的是 Google 示例,因此您可能知道使策略安全所需的其他指令(例如 object-srcbase-uri),但是为了以防万一,请确保使用 CSP Evaluator 之类的工具完成后检查您的保单。

关于javascript - Content-Security-Policy HTTP header 无法正常用于脚本源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45056321/

相关文章:

java - 我可以使用 getResource 将文件存储在 Java 中的 ArrayList 中吗?

security - Write buffer overflow exploit——如何找出shellcode的地址?

security - 如何使用JAVA URL httpsconnection发送没有证书的https请求

javascript - 输入的插入符高度

javascript - 将 promise 的结果插入数组

javascript - 使用 AngularJS 登录 Spring Boot $http.post

java - 由于 java 中的 session ,两个 HTTP 请求(一个接一个)不起作用

Python BaseHTTPServer.HTTPServer - 启动和停止事件的回调

javascript - JSON Object javascript,如何获取值而不是键

mysql - 如何轻松安全地远程连接到postgres或mysql?