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-
必須包含有效完整性元資料的請求目的地列表。允許的值為:
sources可選-
必須包含完整性元資料的完整性源列表。允許的值為:
endpoints可選-
報告端點名稱列表,指示報告將傳送到何處。報告端點必須在
Reporting-Endpoints頭中定義。
示例
當指令碼缺少完整性元資料時進行阻止和報告
此示例展示了一個文件,當任何 <script>(或 HTMLScriptElement)未指定 integrity 屬性,或者當指令碼資源以 no-cors 模式請求時,會進行阻止和報告。
請注意,Integrity-Policy 中使用的 integrity-endpoint 在 Reporting-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 |
瀏覽器相容性
載入中…