IntegrityViolationReportBody

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

IntegrityViolationReportBody 字典是 Reporting API 的一個擴充套件,用於表示 Integrity Policy 違規報告的正文。

Integrity 違規報告可以傳送到 報告伺服器端點 或透過 ReportingObserver。它們的 type"integrity-violation",一個 url 指示包含違規的文件,以及一個 body 屬性,該屬性是一個與此字典匹配的物件。

例項屬性

blockedURL 只讀

一個字串,表示被強制執行的完整性策略所阻止的資源的 URL(或僅為 reportOnly 策略報告的資源)。

documentURL 只讀

一個字串,表示嘗試載入資源的文件的 URL。

destination 只讀

一個字串,指示被阻止資源的 Request.destination。當前只能是 "script"

reportOnly 只讀

一個布林值:如果策略已執行,則為 false;如果僅報告了違規,則為 true

描述

當文件嘗試載入不符合使用 Integrity-PolicyIntegrity-Policy-Report-Only HTTP 標頭設定的策略的 Subresource Integrity 保證的資源時,會報告完整性策略違規。

具體來說,當文件嘗試載入一個 <script> 資源(或策略中列出的其他 請求目標)且該資源沒有有效的完整性元資料,或者以 no-cors 模式發出請求時,會發送報告。

在違規文件中,可以使用 ReportingObserver 回撥(在 ReportingObserver() 建構函式中定義)來獲取違規報告,透過過濾 type"integrity-violation" 的報告物件來實現。

違規報告也可以作為 JSON 物件透過 POST 請求傳送到 endpoints,這些端點在 Integrity-PolicyIntegrity-Policy-Report-Only 標頭中指定。JSON 報告物件是對 ReportingObserver 返回的報告的序列化,因此也具有 type"integrity-violation",以及一個 body 屬性,該屬性是此物件的序列化。請注意,策略中設定的端點值必須與使用 Reporting-Endpoints 標頭設定的識別符號進行對映。

示例

使用 API 進行報告

此示例演示瞭如何使用 ReportingObserver 獲取完整性策略違規報告。

首先,我們使用 Integrity-Policy 設定頁面的完整性策略。下面的策略報告並阻止載入任何未指定 integrity 屬性的 <script> 元素或 HTMLScriptElement 物件,或者在 no-cors 模式下請求指令碼資源時。請注意,在本示例中,我們只關心透過 API 報告違規,因此我們省略了報告端點。

http
Integrity-Policy: blocked-destinations=(script)

接下來,我們假設我們的頁面包含以下元素來載入指令碼。因為我們想觸發一個違規,所以省略了用於檢查指令碼是否符合我們預期版本的 integrity 屬性。我們也可以省略 cross-origin 屬性,這樣請求就會以 no-cors 模式傳送。

html
<script
  src="https://example.com/example-framework.js"
  crossorigin="anonymous"></script>

注意: 符合策略的指令碼可能如下所示

html
<script
  src="https://example.com/example-framework.js"
  crossorigin="anonymous"></script>

為了在頁面內觀察違規,我們構建一個新的 ReportingObserver 物件來監聽 "integrity-violation" 型別的報告,傳遞一個會接收並記錄報告的回撥函式。這段程式碼需要在導致違規的指令碼之前,在同一頁面中載入。

js
const observer = new ReportingObserver(
  (reports, observer) => {
    reports.forEach((violation) => {
      console.log(violation);
      console.log(JSON.stringify(violation));
    });
  },
  {
    types: ["integrity-violation"],
    buffered: true,
  },
);

observer.observe();

上面,我們記錄了每個違規報告物件以及物件的 JSON 字串版本,這可能看起來與下面的物件相似。

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

將報告發送到報告端點

將網頁配置為將完整性策略違規報告發送到 報告伺服器端點 與前面的示例非常相似。

主要區別在於,我們需要使用 Reporting-Endpoints 響應標頭指定一個或多個要將報告發送到的報告端點,然後在設定策略時在 endpoints 欄位中引用它們。

您可以在下方看到,我們首先定義了兩個端點 — integrity-endpointbackup-integrity-endpoint — 然後在我們的策略中引用它們。

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)

我們可以透過從頁面載入一個不符合子資源完整性指南的外部指令碼來觸發違規。為了與前面的示例有所不同,這裡我們以 no-cors 模式傳送請求。

html
<script
  src="https://example.com/example-framework.js"></script>

然後,違規報告將作為 JSON 檔案傳送到指定的端點。從下面的示例中可以看到,type"integrity-violation"body 屬性是此 IntegrityViolationReportBody 物件的序列化。

在這種情況下,報告將與我們上一個示例中的 JSON 報告相同。

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

規範

規範
子資源完整性
# report-violations

瀏覽器相容性

另見