此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Content-Security-Policy:report-to 指令

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Content-Security-Policyreport-to 指令用于指定浏览器在检测到 CSP(内容安全策略)违规时,应该将报告发送到的端点名称。

如果发生 CSP 违规,会生成一个包含序列化的 CSPViolationReportBody 对象实例的报告。该报告将通过报告 API 中定义的通用机制,发送到与端点名称对应的 URL。

服务器必须在 Reporting-Endpoints HTTP 响应标头中单独提供端点名称与其对应 URL 之间的映射关系。

CSP 版本 3
指令类型 报告指令
此指令在 <meta> 元素中不受支持。

语法

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

<endpoint_name> Reporting-Endpoints HTTP 响应标头提供的端点名称。它也可以是服务器在 Report-To 已弃用 HTTP 响应标头中提供的组名称。

违规报告语法

CSP 违规报告是一个经过 JSON 序列化的 Report 对象实例,其中 type 属性的值为 "csp-violation",而 bodyCSPViolationReportBody 对象的序列化形式(其属性定义可参见对应对象文档)。报告会通过 POST 请求发送到目标端点,其 Content-Typeapplication/reports+json

单个报告的 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

规范

Specification
Content Security Policy Level 3
# directive-report-to

浏览器兼容性

参见