2017-08-14 32 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