?
本文檔使用
php中文網(wǎng)手冊(cè) 發(fā)布
棄用
該功能已從Web標(biāo)準(zhǔn)中刪除。盡管一些瀏覽器可能仍然支持它,但它正在被丟棄。避免使用它并盡可能更新現(xiàn)有的代碼;請(qǐng)參閱本頁(yè)底部的兼容性表格來(lái)指導(dǎo)您的決定。請(qǐng)注意,此功能可能隨時(shí)停止工作。
不推薦使用的HTTP Content-Security-Policy(CSP)report-uri指令指示用戶代理報(bào)告違反內(nèi)容安全策略的企圖。這些違規(guī)報(bào)告由通過(guò)HTTP POST請(qǐng)求發(fā)送到指定URI的JSON文檔組成。
盡管report-to指令旨在取代已棄用的report-uri指令,report-to但在大多數(shù)瀏覽器中仍不受支持。因此,對(duì)于當(dāng)前瀏覽器的同時(shí),還加入向前兼容的瀏覽器時(shí)獲得的兼容性report-to支持,您可以同時(shí)指定report-uri和report-to:
Content-Security-Policy: ...; report-uri https://endpoint.com; report-to groupname
在支持的瀏覽器中report-to,report-uri指令將被忽略。
該指令本身沒(méi)有任何影響,但僅與其他指令結(jié)合起來(lái)才有意義。
CSP版本 | 1 |
|---|---|
指令類型 | 報(bào)告指令 |
| 該指令在<meta>元素中不受支持。|
Content-Security-Policy: report-uri <uri>;
<uri>將報(bào)告發(fā)布到的URI。
請(qǐng)參閱Content-Security-Policy-Report-Only更多信息和示例。
Content-Security-Policy: default-src https:; report-uri /csp-violation-report-endpoint/
/csp-violation-report-endpoint/ 例如可以運(yùn)行一個(gè)如下所示的PHP,它記錄詳細(xì)說(shuō)明違規(guī)的JSON,并且如果該違規(guī)是第一個(gè)添加到日志文件的違規(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) | 評(píng)論 |
|---|---|---|
內(nèi)容安全策略級(jí)別3該規(guī)范中“report-uri”的定義。 | 編輯草稿 | 沒(méi)有變化。 |
內(nèi)容安全策略級(jí)別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 |