我正在使用 CSP nonce
规则来加载我所有的外部 JS 脚本,也就是说,我不会触发 'unsafe-inline'
。因此它更安全。
奇怪的是,对于 Google Analytics 文件,浏览器阻止加载由 analytics.js
脚本本身加载的文件。
任何人都有使用 CSP 和不使用 'unsafe-inline'
加载 Google Analytics 的经验,使用 nonces or hashes ?
最佳答案
我敢打赌 script-src仅适用于您的内联 GA 脚本,但是 GA 发出的后续 /collect
请求是 image/gif
请求。尝试使用 img-src允许那些 /collect
调用:
Content-Security-Policy: img-src http://www.google-analytics.com
Content-Security-Policy: img-src https://www.google-analytics.com
选择与您的协议(protocol)匹配的那个。
关于google-analytics - 使用 CSP nonce 加载 Google Analytics js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50089061/