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

瀏覽器相容性

另見