内容安全策略 report-uri 未被识别

Content Security Policy report-uri is not being recognized

我正在 report-only 模式下设置内容安全策略。当我测试它时,Google Chrome 给出了这个错误:

内容安全策略'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-only模式交付,但未指定'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;
");

我在网站根目录中有一个文件夹:csp-violations-report-endpoint,里面有一个 index.php 文件来处理违规。

我不确定我做错了什么。我已阅读 MDN's suggestions for report-uri and used Google's example 以编写我的 report-uri 指令。

我应该尝试将 report-uri 指向根目录中的脚本吗?我应该尝试让它自己登录,还是需要一个解析器来处理它?我的脚本有问题吗? (如果有帮助,我可以包括在内)

编辑:我的网络浏览器可能会忽略 report-uri 指令(因为它已被弃用)并期待 report-to 指令,这就是为什么它不起作用但出现错误消息让我相信情况并非如此。

我可能完全偏离了基础,但是,如果您使用的代码与上图完全一样,那么您可能会发送一堆无效的 headers。 HTTP Headers 必须存在于一行中,而您的则不需要。试试这个:

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; "
);