javascript - 谷歌分析内容安全政策

标签 javascript google-analytics http-headers content-security-policy

我有内容安全政策:

default-src 'none';
style-src 'self';
script-src 'self' https://www.google-analytics.com;
img-src 'self' https://www.google-analytics.com;
connect-src 'self';

在我的页面上,我已将内联 GA 代码放入异步脚本中:

<script src="/javascript/ga.js" async></script>

这会导致 CSP 错误:

Refused to load the script 'data:application/javascript;base64,KGZ1bmN0aW9uKCkgewoJLy8gaHR0cHM6Ly9kZXZl…07Cgl9OwoJZ2EucmVtb3ZlID0gbm9vcGZuOwoJd2luZG93W2dhTmFtZV0gPSBnYTsKfSkoKTs=' because it violates the following Content Security Policy directive: "script-src 'self' https://www.google-analytics.com".

有没有办法从 JS 文件提供这个脚本,如果没有,我需要如何更改 CSP?

最佳答案

Google Analytics 与 CSP 兼容。 base64 编码的 data: blob OP 看到的是由 uBlock Origin 扩展注入(inject)的。要验证,禁用它/尝试隐身。 IIRC,这是由于扩展中的“实验性/未中断”设置。

请抵制白名单的诱惑data:script-src .这将使该策略对 XSS 缓解完全无用,因为攻击者可以注入(inject) <script src="data:text/javascript,alert(1)"></script>。执行 Javascript。

关于javascript - 谷歌分析内容安全政策,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41395591/

相关文章:

php - 如何从 PHP 脚本发送 500 Internal Server Error 错误

javascript - 寻找下一张图片

javascript - 当 mapStateToProps 中收到另一个 prop 时更新 prop

ajax - 可以通过 AJAX 加载 Google Analytics 吗?

javascript - 使用 CasperJS 在页面上测试 Google Analytics JavaScript 调用?

http - 如何防止返回 304 的请求

javascript - 动态添加文本框和文本区域

javascript - SVG 在最大尺寸处拉伸(stretch)然后填充其余尺寸

android studio google play-services 8.3 问题

http - http 中的 md5 范围