使用 CSP 随机数加载 Google Analytics js 文件

Load Google Analytics js file with CSP nonce

我正在使用 CSP nonce 规则来加载我所有的外部 JS 脚本,也就是说,我没有触发 'unsafe-inline'。所以比较安全。

奇怪的是 Google Analytics 文件,浏览器阻止加载由 analytics.js 脚本本身.[=17= 加载的文件]

任何人都有加载 Google Analytics with CSP and without 'unsafe-inline', using nonces or hashes?

我敢打赌 script-src only works for your inline GA script, however subsequent /collect requests made by GA are image/gif requests. Try to use img-src 允许那些 /collect 调用:

Content-Security-Policy: img-src http://www.google-analytics.com
Content-Security-Policy: img-src https://www.google-analytics.com

选择符合您协议的那个。