CSP:report-uri
已棄用:此功能不再推薦使用。雖然一些瀏覽器可能仍然支援它,但它可能已從相關的 Web 標準中移除,可能正在被棄用,或者可能僅出於相容性目的而保留。避免使用它,如果可能,請更新現有程式碼;請參閱本頁面底部的相容性表格以指導您的決策。請注意,此功能可能隨時停止工作。
警告:report-to 指令旨在替換 report-uri,並且在支援 report-to 的瀏覽器中,report-uri 指令會被忽略。
但是,在 report-to 得到廣泛支援之前,您可以同時指定這兩個標頭,如所示
Content-Security-Policy: …; report-uri https://endpoint.example.com; report-to endpoint_name
已棄用的 HTTP Content-Security-Policy (CSP) report-uri 指令指示使用者代理報告違反內容安全策略的嘗試。這些違規報告由透過 HTTP POST 請求傳送到指定 URI 的JSON 文件組成。
該指令本身沒有任何效果,只有與其他指令結合使用才有意義。
語法
Content-Security-Policy: report-uri <uri>;
Content-Security-Policy: report-uri <uri> <uri>;
- <uri>
-
指示報告必須傳送到的 URI。
違規報告語法
報告 JSON 物件透過 HTTP POST 操作傳送,Content-Type 為 application/csp-report。
注意:違規報告應被視為攻擊者控制的資料。在儲存或呈現之前,應正確清理內容。對於提供的script-sample 屬性尤其如此。
報告 JSON 物件具有一個頂級屬性 "csp-report",其中包含一個具有以下屬性的物件
blocked-uri-
內容安全策略阻止載入的資源的 URI。如果阻止的 URI 與
document-uri來源不同,則阻止的 URI 將被截斷,僅包含方案、主機和埠。 disposition-
根據使用
Content-Security-Policy-Report-Only標頭還是Content-Security-Policy標頭,為"enforce"或"report"。 document-uri-
發生違規的文件的 URI。
effective-directive-
導致違規的指令的執行。某些瀏覽器可能會提供不同的值,例如 Chrome 提供
style-src-elem/style-src-attr,即使執行的指令是style-src。 original-policy-
由
Content-Security-PolicyHTTP 標頭指定的原始策略。 referrer已棄用 非標準-
發生違規的文件的來源。
script-sample-
導致違規的內聯指令碼、事件處理程式或樣式的前 40 個字元。來自外部檔案的違規不會包含在報告中。
這僅適用於
script-src*和style-src*違規,當相應的Content-Security-Policy指令包含'report-sample'關鍵字時。 status-code-
在其中例項化全域性物件的資源的 HTTP 狀態程式碼。
violated-directive已棄用-
導致違規的指令的執行。
violated-directive是effective-directive欄位的歷史名稱,包含相同的值。
示例
使用 Content-Security-Policy 的 CSP 違規報告
讓我們考慮一個位於 http://example.com/signup.html 的頁面。它使用以下策略,不允許除從 cdn.example.com 載入的樣式表之外的任何內容。
Content-Security-Policy: default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports
signup.html 的 HTML 如下所示
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>Sign Up</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
Here be content.
</body>
</html>
你能發現錯誤嗎?樣式表僅允許從 cdn.example.com 載入,但網站嘗試從其自己的來源 (http://example.com) 載入一個。能夠執行 CSP 的瀏覽器在訪問文件時會將以下違規報告作為 POST 請求傳送到 http://example.com/_/csp-reports
{
"csp-report": {
"blocked-uri": "http://example.com/css/style.css",
"disposition": "report",
"document-uri": "http://example.com/signup.html",
"effective-directive": "style-src-elem",
"original-policy": "default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports",
"referrer": "",
"status-code": 200,
"violated-directive": "style-src-elem"
}
}
如您所見,該報告在 blocked-uri 中包含違規資源的完整路徑。情況並非總是如此。例如,如果 signup.html 嘗試從 http://anothercdn.example.com/stylesheet.css 載入 CSS,瀏覽器 *不會* 包含完整路徑,而只會包含來源 (http://anothercdn.example.com),以防止洩漏有關跨來源資源的敏感資訊。CSP 規範對此行為進行了說明。
使用 Content-Security-Policy-Report-Only 的 CSP 違規報告
report-uri 指令也可以與Content-Security-Policy-Report-Only 響應標頭一起使用。此標頭允許瀏覽器在測試時報告違規,但不阻止違規。
HTTP 標頭將大致相同。
Content-Security-Policy-Report-Only: default-src 'none'; style-src cdn.example.com; report-to /_/csp-reports
該報告將與 disposition 為 "report" 且當然還有 "original-policy" 相同。
{
"csp-report": {
"blocked-uri": "http://example.com/css/style.css",
"disposition": "report",
"document-uri": "http://example.com/signup.html",
"effective-directive": "style-src-elem",
"original-policy": "default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports",
"referrer": "",
"status-code": 200,
"violated-directive": "style-src-elem"
}
}
CSP 違規日誌記錄
假設伺服器傳送包含以下 Content-Security-Policy 標頭的響應
Content-Security-Policy: default-src https:; report-uri /csp-violation-report-endpoint/
/csp-violation-report-endpoint/ 例如可以執行以下 PHP 指令碼,該指令碼記錄詳細說明違規的 JSON,並且如果違規是新增到日誌檔案中的第一個違規,則向管理員傳送電子郵件
<?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);
}
規範
| 規範 |
|---|
| 內容安全策略級別 3 # directive-report-uri |
瀏覽器相容性
BCD 表格僅在瀏覽器中載入