我正在仅报告模式下设置内容安全策略。当我测试它时,Google Chrome 出现以下错误:
内容安全策略'default-src'self'; script-src 'self' '不安全内联' https://use.typekit.com https://js.hs-analytics.net https://google-analytics.com https://ajax.googleapis.com ;字体-src https://use.typekit.com; style-src 'self' 'unsafe-inline' https://use.typekit.com;框架-src https://www.youtube.com;' 以仅报告模式交付,但未指定“report-uri”;该策略将无效。请添加“report-uri”指令,或通过“Content-Security-Policy” header 传递策略。
这是我的完整内容安全策略,我在网站的 header PHP 文件中定义了 HTTP header :
header("Content-Security-Policy-Report-Only: default-src 'self';
script-src 'self' 'unsafe-inline' https://use.typekit.com https://js.hs-analytics.net https://google-analytics.com https://ajax.googleapis.com;
font-src https://use.typekit.com;
style-src 'self' 'unsafe-inline' https://use.typekit.com;
frame-src https://www.youtube.com;
report-uri /csp-violations-report-endpoint;
");
我的 Web 根目录中有一个文件夹:csp-violations-report-endpoint,其中有一个 index.php 文件来处理违规行为。
我不确定我做错了什么。我读过MDN's suggestions for report-uri并使用Google's example编写我的report-uri指令。
我应该尝试将 report-uri
指向根目录中的脚本吗?我应该尝试让它自己登录,还是需要一个解析器来处理它?我的脚本可能有问题吗? (如果有帮助的话我可以添加)
编辑:我的网络浏览器可能会忽略 report-uri
指令(因为它已被弃用)并且需要 report-to
指令,并且这就是为什么它不起作用,但错误消息让我相信情况并非如此。
最佳答案
我可能完全没有根据,但是,如果您使用的代码与上图完全相同,那么您可能会发送一堆无效 header 。 HTTP header 必须存在于一行中,而您的则不然。试试这个:
header(
"Content-Security-Policy-Report-Only: default-src 'self'; " .
"script-src 'self' 'unsafe-inline' https://use.typekit.com https://js.hs-analytics.net https://google-analytics.com https://ajax.googleapis.com; " .
"font-src https://use.typekit.com; " .
"style-src 'self' 'unsafe-inline' https://use.typekit.com; " .
"frame-src https://www.youtube.com; " .
"report-uri /csp-violations-report-endpoint; "
);
关于security - 内容安全策略报告-uri 未被识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45676707/