?
This document uses PHP Chinese website manual Release
棄用
該功能已從Web標準中刪除。盡管一些瀏覽器可能仍然支持它,但它正在被丟棄。避免使用它并盡可能更新現(xiàn)有的代碼;請參閱本頁底部的兼容性表格來指導您的決定。請注意,此功能可能隨時停止工作。
不推薦使用的HTTP Content-Security-Policy
(CSP)report-uri
指令指示用戶代理報告違反內容安全策略的企圖。這些違規(guī)報告由通過HTTP POST請求發(fā)送到指定URI的JSON文檔組成。
盡管report-to
指令旨在取代已棄用的report-uri
指令,report-to
但在大多數(shù)瀏覽器中仍不受支持。因此,對于當前瀏覽器的同時,還加入向前兼容的瀏覽器時獲得的兼容性report-to
支持,您可以同時指定report-uri
和report-to
:
Content-Security-Policy: ...; report-uri https://endpoint.com; report-to groupname
在支持的瀏覽器中report-to
,report-uri
指令將被忽略。
該指令本身沒有任何影響,但僅與其他指令結合起來才有意義。
CSP版本 | 1 |
---|---|
指令類型 | 報告指令 |
| 該指令在<meta>元素中不受支持。|
Content-Security-Policy: report-uri <uri>;
<uri>將報告發(fā)布到的URI。
請參閱Content-Security-Policy-Report-Only
更多信息和示例。
Content-Security-Policy: default-src https:; report-uri /csp-violation-report-endpoint/
/csp-violation-report-endpoint/
例如可以運行一個如下所示的PHP,它記錄詳細說明違規(guī)的JSON,并且如果該違規(guī)是第一個添加到日志文件的違規(guī),則向管理員發(fā)送一封電子郵件:
<?php// Start configure $log_file = dirname(__FILE__) . '/csp-violations.log'; $log_file_size_limit = 1000000; // bytes - once exceeded no further entries are added $email_address = 'admin@example.com'; $email_subject = 'Content-Security-Policy violation';// End configuration $current_domain = preg_replace('/www\./i', '', $_SERVER['SERVER_NAME']); $email_subject = $email_subject . ' on ' . $current_domain;http_response_code(204); // HTTP 204 No Content$json_data = file_get_contents('php://input'); // We pretty print the JSON before adding it to the log file if ($json_data = json_decode($json_data)) { $json_data = json_encode($json_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); if (!file_exists($log_file)) { // Send an email $message = "The following Content-Security-Policy violation occurred on " . $current_domain . ":\n\n" . $json_data . "\n\nFurther CPS violations will be logged to the following log file, but no further email notifications will be sent until this log file is deleted:\n\n" . $log_file; mail($email_address, $email_subject, $message, 'Content-Type: text/plain;charset=utf-8'); } else if (filesize($log_file) > $log_file_size_limit) { exit(0); } file_put_contents($log_file, $json_data, FILE_APPEND | LOCK_EX); } ?>
規(guī)范 | 狀態(tài) | 評論 |
---|---|---|
內容安全策略級別3該規(guī)范中“report-uri”的定義。 | 編輯草稿 | 沒有變化。 |
內容安全策略級別2該規(guī)范中的'report-uri'的定義。 | 建議 | 初始定義。 |
特征 | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
基本支持 | 25 | 14 | 23.0 | No | 15 | 7 |
特征 | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
基本支持 | (Yes) | (Yes) | ? | 23.0 | No | ? | 7.1 |