@@ -98,7 +98,19 @@ def self.included(base)
9898 STYLE_SRC_ATTR
9999 ] . flatten . freeze
100100
101- ALL_DIRECTIVES = ( DIRECTIVES_1_0 + DIRECTIVES_2_0 + DIRECTIVES_3_0 ) . uniq . sort
101+ # Experimental directives - these vary greatly in support
102+ # See MDN for details.
103+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types
104+ TRUSTED_TYPES = :trusted_types
105+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/require-trusted-types-for
106+ REQUIRE_TRUSTED_TYPES_FOR = :require_trusted_types_for
107+
108+ DIRECTIVES_EXPERIMENTAL = [
109+ TRUSTED_TYPES ,
110+ REQUIRE_TRUSTED_TYPES_FOR ,
111+ ] . flatten . freeze
112+
113+ ALL_DIRECTIVES = ( DIRECTIVES_1_0 + DIRECTIVES_2_0 + DIRECTIVES_3_0 + DIRECTIVES_EXPERIMENTAL ) . uniq . sort
102114
103115 # Think of default-src and report-uri as the beginning and end respectively,
104116 # everything else is in between.
@@ -121,6 +133,7 @@ def self.included(base)
121133 OBJECT_SRC => :source_list ,
122134 PLUGIN_TYPES => :media_type_list ,
123135 REQUIRE_SRI_FOR => :require_sri_for_list ,
136+ REQUIRE_TRUSTED_TYPES_FOR => :require_trusted_types_for_list ,
124137 REPORT_URI => :source_list ,
125138 PREFETCH_SRC => :source_list ,
126139 SANDBOX => :sandbox_list ,
@@ -130,6 +143,7 @@ def self.included(base)
130143 STYLE_SRC => :source_list ,
131144 STYLE_SRC_ELEM => :source_list ,
132145 STYLE_SRC_ATTR => :source_list ,
146+ TRUSTED_TYPES => :source_list ,
133147 WORKER_SRC => :source_list ,
134148 UPGRADE_INSECURE_REQUESTS => :boolean ,
135149 } . freeze
@@ -175,6 +189,7 @@ def self.included(base)
175189 ] . freeze
176190
177191 REQUIRE_SRI_FOR_VALUES = Set . new ( %w( script style ) )
192+ REQUIRE_TRUSTED_TYPES_FOR_VALUES = Set . new ( %w( script ) )
178193
179194 module ClassMethods
180195 # Public: generate a header name, value array that is user-agent-aware.
@@ -270,7 +285,8 @@ def list_directive?(directive)
270285 source_list? ( directive ) ||
271286 sandbox_list? ( directive ) ||
272287 media_type_list? ( directive ) ||
273- require_sri_for_list? ( directive )
288+ require_sri_for_list? ( directive ) ||
289+ require_trusted_types_for_list? ( directive )
274290 end
275291
276292 # For each directive in additions that does not exist in the original config,
@@ -308,6 +324,10 @@ def require_sri_for_list?(directive)
308324 DIRECTIVE_VALUE_TYPES [ directive ] == :require_sri_for_list
309325 end
310326
327+ def require_trusted_types_for_list? ( directive )
328+ DIRECTIVE_VALUE_TYPES [ directive ] == :require_trusted_types_for_list
329+ end
330+
311331 # Private: Validates that the configuration has a valid type, or that it is a valid
312332 # source expression.
313333 def validate_directive! ( directive , value )
@@ -325,6 +345,8 @@ def validate_directive!(directive, value)
325345 validate_media_type_expression! ( directive , value )
326346 when :require_sri_for_list
327347 validate_require_sri_source_expression! ( directive , value )
348+ when :require_trusted_types_for_list
349+ validate_require_trusted_types_for_source_expression! ( directive , value )
328350 else
329351 raise ContentSecurityPolicyConfigError . new ( "Unknown directive #{ directive } " )
330352 end
@@ -369,6 +391,16 @@ def validate_require_sri_source_expression!(directive, require_sri_for_expressio
369391 end
370392 end
371393
394+ # Private: validates that a require trusted types for expression:
395+ # 1. is an array of strings
396+ # 2. is a subset of ["script"]
397+ def validate_require_trusted_types_for_source_expression! ( directive , require_trusted_types_for_expression )
398+ ensure_array_of_strings! ( directive , require_trusted_types_for_expression )
399+ unless require_trusted_types_for_expression . to_set . subset? ( REQUIRE_TRUSTED_TYPES_FOR_VALUES )
400+ raise ContentSecurityPolicyConfigError . new ( %(require-trusted-types-for for must be a subset of #{ REQUIRE_TRUSTED_TYPES_FOR_VALUES . to_a } but was #{ require_trusted_types_for_expression } ) )
401+ end
402+ end
403+
372404 # Private: validates that a source expression:
373405 # 1. is an array of strings
374406 # 2. does not contain any deprecated, now invalid values (inline, eval, self, none)
0 commit comments