我正在尝试在我的网站上实施内容安全策略。但是当我实现 script-src 时,它给了我这个错误。
Refused to load the script 'https://www.googletagmanager.com/gtm.js?id=GTM-XXXXX' because it violates the following
我的内容安全策略是
Content-Security-Policy: script-src 'self' 'unsafe-eval' https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js https://www.google-analytics.com 'unsafe-eval' 'unsafe-inline' https://*.googletagmanager.com/gtm.js/ https://tagmanager.google.com/http://www.googleadservices.com/pagead/conversion_async.js https://*.google.com;
删除
https://*.googletagmanager.com/gtm.js/
中的尾部斜杠,因为 CSP 认为它是一个文件夹,但实际上它是一个文件。更可靠的是仅指定 https://*.googletagmanager.com
而不指定路径/文件名。
在
https://tagmanager.google.com/
和 http://www.googleadservices.com/pagead/conversion_async.js
之间插入一个空格,因为您指定的主机源不正确:https://tagmanager.google.com/http://www.googleadservices.com/pagead/conversion_async.js
。
注 1:主机源
http://www.googleadservices.com/pagead/conversion_async.js
被 https://*.google.com
覆盖(属于该模板)。
注 2:
script-src 'unsafe-inline'
不能防范 XSS。考虑不在 'unsafe-inline'
指令中使用 script-src
。
可以添加
script-src *.googletagmanager.com;
您的内容政策对 GTAG 进行了例外处理。