2017-08-14 63 views
3

我以仅报告模式设置内容安全策略。当我测试它时,Google Chrome会发出此错误:内容安全策略报告 - uri未被识别

内容安全策略'default-src'self'; script-src'self''unsafe-inline'https://use.typekit.com https://js.hs-analytics.nethttps://google-analytics.comhttps://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';该政策将不起作用。请添加一个'report-uri'指令,或通过'Content-Security-Policy'标题发送策略。

这里是我的全部内容安全策略,我定义HTTP标头中的网站头PHP文件:

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-违反报告端点,里面有一个index.php文件来处理违规行为。

我不知道我在做什么错。我读过MDN's suggestions for report-uri并使用Google's example来编写我的report-uri指令。

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

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

回答

2

我可能完全没有基础,但是,如果您完全按照上图所示使用代码,那么您可能会发送一堆无效的标头。 HTTP标题必须存在于一行中,而您的标题不会。试试这个:

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

就是这样。我将其更改为连接字符串,并且标题现在正在工作。好极了! – Tiffany