CSP:require-trusted-types-for
HTTP Content-Security-Policy (CSP) require-trusted-types-for 實驗性 指令指示使用者代理控制傳遞給 DOM XSS 接收函式(例如 Element.innerHTML 設定器)的資料。
使用此指令時,這些函式僅接受由受信任型別策略建立的不可偽造的型別化值,並拒絕字串。結合 trusted-types 指令(該指令用於保護受信任型別策略的建立),這允許作者定義規則來保護向 DOM 寫入值,從而將 DOM XSS 攻擊面縮小到 Web 應用程式程式碼庫的少量隔離部分,從而方便監控和程式碼審查。
語法
http
Content-Security-Policy: require-trusted-types-for 'script';
'指令碼'-
禁止使用帶有 DOM XSS 注入接收函式的字串,並要求使用由受信任型別策略建立的匹配型別。
示例
js
// Content-Security-Policy: require-trusted-types-for 'script'; trusted-types foo;
const attackerInput = '<svg onload="alert(/cross-site-scripting/)" />';
const el = document.createElement("div");
if (typeof trustedTypes !== "undefined") {
// Create a policy that can create TrustedHTML values
// after sanitizing the input strings with DOMPurify library.
const sanitizer = trustedTypes.createPolicy("foo", {
createHTML: (input) => DOMPurify.sanitize(input),
});
el.innerHTML = sanitizer.createHTML(attackerInput); // Puts the sanitized value into the DOM.
el.innerHTML = attackerInput; // Rejects a string value; throws a TypeError.
}
規範
| 規範 |
|---|
| 受信任型別 # require-trusted-types-for-csp-directive |
瀏覽器相容性
BCD 表格僅在啟用 JavaScript 的瀏覽器中載入。
另請參閱
內容安全策略- 跨站指令碼 (XSS)
- 受信任型別涵蓋的 DOM XSS 注入接收器
- 使用受信任型別防止基於 DOM 的跨站指令碼漏洞
- 使用 DOMPurify XSS 淨化器實現受信任型別
- 受信任型別的 polyfill