SecurityPolicyViolationEvent:sample 屬性

Baseline 已廣泛支援

此功能已成熟,並可在多種裝置和瀏覽器版本上執行。自 2021 年 9 月起,所有瀏覽器均已支援此功能。

注意:此功能在 Web Workers 中可用。

SecurityPolicyViolationEvent 介面中的只讀屬性 sample 是一個字串,代表導致 內容安全策略 (CSP) 違規的資源的示例。

僅適用於 script-src*style-src* 違規,當相應的 Content-Security-Policy 指令包含 'report-sample' 關鍵字時。此外,只有當資源是內聯指令碼、事件處理程式或樣式時,此項才會被填充 — 導致違規的外部資源不會生成示例。

注意: 違規報告應被視為攻擊者控制的資料。此欄位的內容在儲存或渲染之前應經過清理。

一個字串,包含導致違規的資源的示例,通常是前 40 個字元,或者是一個空字串。

示例

js
document.addEventListener("securitypolicyviolation", (e) => {
  console.log(e.sample);
});

規範

規範
內容安全策略級別 3
# dom-securitypolicyviolationevent-sample

瀏覽器相容性

另見