CSP:report-to

Content-Security-Policyreport-to 指令指示瀏覽器應使用哪個端點名稱來報告 CSP 違規。

如果發生 CSP 違規,則會生成一個報告,其中包含一個序列化後的 CSPViolationReportBody 物件例項。此報告將傳送到與端點名稱對應的 URL,使用 Reporting API 中定義的通用機制。

伺服器必須在 Reporting-Endpoints HTTP 響應頭中單獨提供端點名稱與其對應 URL 之間的對映。

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

語法

http
Content-Security-Policy: …; report-to <endpoint_name>

<endpoint_name>Reporting-Endpoints HTTP 響應頭提供的端點名稱。它也可以是伺服器在 Report-To 已棄用 HTTP 響應頭中提供的組的名稱。

違規報告語法

CSP 違規報告是一個 JSON 序列化的 Report 物件例項,其 type 屬性的值為 "csp-violation",而 bodyCSPViolationReportBody 物件的序列化形式(請參閱各自物件的屬性定義)。報告透過帶有 application/reports+jsonContent-TypePOST 操作傳送到目標端點。

單個報告的 JSON 可能如下所示

json
{
  "age": 53531,
  "body": {
    "blockedURL": "inline",
    "columnNumber": 39,
    "disposition": "enforce",
    "documentURL": "https://example.com/csp-report",
    "effectiveDirective": "script-src-elem",
    "lineNumber": 121,
    "originalPolicy": "default-src 'self'; report-to csp-endpoint-name",
    "referrer": "https://www.google.com/",
    "sample": "console.log(\"lo\")",
    "sourceFile": "https://example.com/csp-report",
    "statusCode": 200
  },
  "type": "csp-violation",
  "url": "https://example.com/csp-report",
  "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
}

使用說明

report-to 指令旨在替換 report-uri,支援 report-to 的瀏覽器會忽略 report-uri 指令。但是,在 report-to 得到廣泛支援之前,您可以同時指定這兩個標頭,如所示

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

請注意,本主題中的其他示例未顯示 report-uri

示例

設定 CSP 違規報告端點

伺服器可以使用 HTTP 響應中的 Reporting-Endpoints 標頭定義端點名稱與 URL 之間的對映。可以使用任何名稱:這裡我們選擇了 name-of-endpoint

http
Reporting-Endpoints: name-of-endpoint="https://example.com/csp-reports"

伺服器可以使用 report-to 指令將此端點名稱設定為傳送 CSP 違規報告的目標。

http
Content-Security-Policy: default-src 'self'; report-to name-of-endpoint

規範

規範
內容安全策略級別 3
# directive-report-to

瀏覽器相容性

BCD 表格僅在啟用 JavaScript 的瀏覽器中載入。

另請參閱