Content-Security-Policy: trusted-types 指令

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

HTTP Content-Security-Policy (CSP) trusted-types 指令指示使用者代理限制建立 Trusted Types 策略——這些函式用於構建不可偽造的、型別化的值,旨在傳遞給 DOM XSS 接收器以代替字串。

結合 require-trusted-types-for 指令,這允許作者定義規則,以保護寫入 DOM 的值,從而將 DOM XSS 攻擊面縮小到 Web 應用程式程式碼庫的小型、隔離的部分,便於它們的監控和程式碼審查。此指令聲明瞭一個允許建立自 Trusted Types API 中使用 trustedTypes.createPolicy 建立的 Trusted Type 策略名稱的白名單。

語法

http
Content-Security-Policy: trusted-types;
Content-Security-Policy: trusted-types 'none';
Content-Security-Policy: trusted-types <policyName>;
Content-Security-Policy: trusted-types <policyName> <policyName> 'allow-duplicates';
<policyName>

有效的策略名稱只能包含字母數字字元,或 -#=_/@.% 中的一個。策略名稱中的星號 (*) 指示使用者代理允許任何唯一的策略名稱 (allow-duplicates 可能會進一步放寬此限制)。

'none'

禁止建立任何 Trusted Type 策略(與未指定任何 <policyName> 相同)。

'allow-duplicates'

允許建立已使用過的名稱的策略。

示例

js
// Content-Security-Policy: trusted-types foo bar 'allow-duplicates';

if (typeof trustedTypes !== "undefined") {
  const policyFoo = trustedTypes.createPolicy("foo", {});
  const policyFoo2 = trustedTypes.createPolicy("foo", {});
  const policyBaz = trustedTypes.createPolicy("baz", {}); // Throws and dispatches a SecurityPolicyViolationEvent.
}

規範

規範
Trusted Types
# trusted-types-csp-directive

瀏覽器相容性

另見