Integrity-Policy header

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

HTTP Integrity-Policy 响应头允许网站管理员确保用户代理加载的所有(某种类型)资源都具有子资源完整性保障。

设置此头后,用户代理将阻止那些没有完整性元数据的指定请求目的地的请求,并且还将阻止以no-cors模式发出的请求。

如果此头包含与使用 Reporting-Endpoints 头声明的端点匹配的报告端点名称,则还可能发送违规报告。报告使用 Reporting API 生成,并且也可以通过使用 ReportingObserver 在执行完整性策略的页面中观察到。报告正文的格式由 IntegrityViolationReportBody 字典给出(此正文的 JSON 序列化形式通过 POST 请求发送到报告服务器端点)。

这有助于防范获取的子资源的内容篡改。

头类型 响应头
禁止请求头

语法

http
Integrity-Policy: blocked-destinations=(<destination>),sources=(<source>),endpoints=(<endpoint>)

头值被定义为具有以下键的结构化字段字典

blocked-destinations

必须包含有效完整性元数据的请求目的地列表。允许的值为:

script

脚本资源。

style

样式表资源。

sources 可选

必须包含完整性元数据的完整性源列表。允许的值为:

inline

完整性元数据源是内容中内联的,例如 完整性属性。这是默认值。

由于这是默认值也是唯一值,因此省略 sources 等同于指定 sources=(inline)

endpoints 可选

报告端点名称列表,指示报告将发送到何处。报告端点必须在 Reporting-Endpoints 头中定义。

示例

当脚本缺少完整性元数据时进行阻止和报告

此示例展示了一个文档,当任何 <script>(或 HTMLScriptElement)未指定 integrity 属性,或者当脚本资源以 no-cors 模式请求时,会进行阻止和报告。

请注意,Integrity-Policy 中使用的 integrity-endpointReporting-Endpoints 头中定义。

http
Reporting-Endpoints: integrity-endpoint="https://example.com/integrity", backup-integrity-endpoint="https://report-provider.example/integrity"
Integrity-Policy: blocked-destinations=(script), endpoints=(integrity-endpoint backup-integrity-endpoint)

报告负载可能如下所示。

json
{
  "type": "integrity-violation",
  "url": "https://example.com",
  "body": {
    "documentURL": "https://example.com",
    "blockedURL": "https://example.com/main.js",
    "destination": "script",
    "reportOnly": false
  }
}

规范

规范
子资源完整性
# integrity-policy-section

浏览器兼容性

另见