@@ -98,7 +98,19 @@ def self.included(base)
98
98
STYLE_SRC_ATTR
99
99
] . flatten . freeze
100
100
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
102
114
103
115
# Think of default-src and report-uri as the beginning and end respectively,
104
116
# everything else is in between.
@@ -121,6 +133,7 @@ def self.included(base)
121
133
OBJECT_SRC => :source_list ,
122
134
PLUGIN_TYPES => :media_type_list ,
123
135
REQUIRE_SRI_FOR => :require_sri_for_list ,
136
+ REQUIRE_TRUSTED_TYPES_FOR => :require_trusted_types_for_list ,
124
137
REPORT_URI => :source_list ,
125
138
PREFETCH_SRC => :source_list ,
126
139
SANDBOX => :sandbox_list ,
@@ -130,6 +143,7 @@ def self.included(base)
130
143
STYLE_SRC => :source_list ,
131
144
STYLE_SRC_ELEM => :source_list ,
132
145
STYLE_SRC_ATTR => :source_list ,
146
+ TRUSTED_TYPES => :source_list ,
133
147
WORKER_SRC => :source_list ,
134
148
UPGRADE_INSECURE_REQUESTS => :boolean ,
135
149
} . freeze
@@ -175,6 +189,7 @@ def self.included(base)
175
189
] . freeze
176
190
177
191
REQUIRE_SRI_FOR_VALUES = Set . new ( %w( script style ) )
192
+ REQUIRE_TRUSTED_TYPES_FOR_VALUES = Set . new ( %w( script ) )
178
193
179
194
module ClassMethods
180
195
# Public: generate a header name, value array that is user-agent-aware.
@@ -270,7 +285,8 @@ def list_directive?(directive)
270
285
source_list? ( directive ) ||
271
286
sandbox_list? ( directive ) ||
272
287
media_type_list? ( directive ) ||
273
- require_sri_for_list? ( directive )
288
+ require_sri_for_list? ( directive ) ||
289
+ require_trusted_types_for_list? ( directive )
274
290
end
275
291
276
292
# For each directive in additions that does not exist in the original config,
@@ -308,6 +324,10 @@ def require_sri_for_list?(directive)
308
324
DIRECTIVE_VALUE_TYPES [ directive ] == :require_sri_for_list
309
325
end
310
326
327
+ def require_trusted_types_for_list? ( directive )
328
+ DIRECTIVE_VALUE_TYPES [ directive ] == :require_trusted_types_for_list
329
+ end
330
+
311
331
# Private: Validates that the configuration has a valid type, or that it is a valid
312
332
# source expression.
313
333
def validate_directive! ( directive , value )
@@ -325,6 +345,8 @@ def validate_directive!(directive, value)
325
345
validate_media_type_expression! ( directive , value )
326
346
when :require_sri_for_list
327
347
validate_require_sri_source_expression! ( directive , value )
348
+ when :require_trusted_types_for_list
349
+ validate_require_trusted_types_for_source_expression! ( directive , value )
328
350
else
329
351
raise ContentSecurityPolicyConfigError . new ( "Unknown directive #{ directive } " )
330
352
end
@@ -369,6 +391,16 @@ def validate_require_sri_source_expression!(directive, require_sri_for_expressio
369
391
end
370
392
end
371
393
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
+
372
404
# Private: validates that a source expression:
373
405
# 1. is an array of strings
374
406
# 2. does not contain any deprecated, now invalid values (inline, eval, self, none)
0 commit comments