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 |
浏览器兼容性
加载中…