Content-Security-Policy: report-to 指令
Content-Security-Policy 的 report-to 指令指示瀏覽器應用於報告 CSP 違規的端點名稱。
如果發生 CSP 違規,將生成一份報告,其中包含一個序列化的 CSPViolationReportBody 物件例項。該報告將使用 報告 API 中定義的通用機制傳送到與端點名稱對應的 URL。
伺服器必須在 Reporting-Endpoints HTTP 響應頭中單獨提供端點名稱及其相應 URL 之間的對映。
語法
Content-Security-Policy: …; report-to <endpoint_name>
<endpoint_name> 是由 Reporting-Endpoints HTTP 響應頭提供的端點名稱。它也可以是伺服器在 Report-To 已廢棄 HTTP 響應頭中提供的一個組的名稱。
違規報告語法
CSP 違規報告是一個 JSON 序列化的 Report 物件例項,其 type 屬性的值為 "csp-violation",並且 body 是一個 CSPViolationReportBody 物件的序列化形式(有關其屬性定義,請參見相應的物件)。報告透過 POST 操作傳送到目標端點,Content-Type 為 application/reports+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 得到廣泛支援之前,您可以同時指定這兩個指令,如下所示:
Content-Security-Policy: …; report-uri https://endpoint.example.com; report-to endpoint_name
請注意,本主題中的其他示例並未顯示 report-uri。
示例
設定 CSP 違規報告端點
伺服器可以使用 HTTP 響應中的 Reporting-Endpoints 頭來定義端點名稱和 URL 之間的對映。可以使用任何名稱:這裡我們選擇了 name-of-endpoint。
Reporting-Endpoints: name-of-endpoint="https://example.com/csp-reports"
伺服器可以將此端點名稱設定為傳送 CSP 違規報告的目標,使用 report-to 指令。
Content-Security-Policy: default-src 'self'; report-to name-of-endpoint
規範
| 規範 |
|---|
| 內容安全策略級別 3 # 指令-report-to |
瀏覽器相容性
載入中…