Content-Security-Policy: report-uri 指令

已棄用:此特性不再推薦。雖然某些瀏覽器可能仍然支援它,但它可能已經從相關的網路標準中刪除,可能正在刪除過程中,或者可能僅為相容性目的而保留。請避免使用它,如果可能,請更新現有程式碼;請參閱本頁底部的相容性表格以指導您的決策。請注意,此特性可能隨時停止工作。

警告: report-to 指令旨在取代 report-uri,在支援 report-to 的瀏覽器中,report-uri 指令將被忽略。

然而,在 report-to 獲得廣泛支援之前,你可以同時指定這兩個頭,如下所示

http
Content-Security-Policy: …; report-uri https://endpoint.example.com; report-to endpoint_name

已廢棄的 HTTP Content-Security-Policy (CSP) report-uri 指令指示使用者代理報告嘗試違反內容安全策略的行為。這些違規報告由透過 HTTP POST 請求傳送到指定 URI 的 JSON 文件組成。

該指令本身沒有效果,只有與其他指令結合使用時才具有意義。

CSP 版本 1
指令型別 報告指令
此指令不支援在 <meta> 元素中使用。

語法

http
Content-Security-Policy: report-uri <uri>;
Content-Security-Policy: report-uri <uri> <uri>;
<uri>

指示報告發送位置的 URI。

違規報告語法

報告 JSON 物件透過 HTTP POST 操作傳送,Content-Typeapplication/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-Policy HTTP 頭指定的原始策略。

referrer 已廢棄 非標準

發生違規的文件的引用者。

script-sample

導致違規的內聯指令碼、事件處理程式或樣式的開頭 40 個字元。源自外部檔案的違規不包含在報告中。

這僅適用於 script-src*style-src* 違規,當相應的 Content-Security-Policy 指令包含 'report-sample' 關鍵字時。

status-code

例項化全域性物件的資源的 HTTP 狀態碼。

violated-directive 已廢棄

導致違規的指令。violated-directiveeffective-directive 欄位的歷史名稱,包含相同的值。

示例

帶有 Content-Security-Policy 的 CSP 違規報告

讓我們考慮一個位於 http://example.com/signup.html 的頁面。它使用以下策略,除了從 cdn.example.com 載入的樣式表外,禁止所有內容。

http
Content-Security-Policy: default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports

signup.html 的 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 的瀏覽器在訪問文件時會向 http://example.com/_/csp-reports 傳送以下違規報告作為 POST 請求

json
{
  "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 頭將大致相同。

http
Content-Security-Policy-Report-Only: default-src 'none'; style-src cdn.example.com; report-to /_/csp-reports

報告將相同,除了 disposition "report" 以及當然的 "original-policy"

json
{
  "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 頭的響應

http
Content-Security-Policy: default-src https:; report-uri /csp-violation-report-endpoint/

/csp-violation-report-endpoint/ 例如可以執行一個 PHP 指令碼,如下所示,該指令碼記錄詳細說明違規的 JSON,如果違規是第一個新增到日誌檔案的,則向管理員傳送一封電子郵件

php
<?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

瀏覽器相容性

另見