Disallow invalid trusted type policy names (#38886)

Actual fix is in the CSP crate.

Part of #36258

Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com>
This commit is contained in:
Tim van der Lippe 2025-08-28 19:49:33 +02:00 committed by GitHub
parent 908c392219
commit 6205c07114
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 18 additions and 29 deletions

2
Cargo.lock generated
View file

@ -1630,7 +1630,7 @@ dependencies = [
[[package]] [[package]]
name = "content-security-policy" name = "content-security-policy"
version = "0.5.4" version = "0.5.4"
source = "git+https://github.com/servo/rust-content-security-policy?branch=servo-csp#cf67beb96db9244ab6956a4da61dbe83384d5cd7" source = "git+https://github.com/servo/rust-content-security-policy?branch=servo-csp#fc927dfefb1fdc052fa4fa18c2ca3c3f6b87047b"
dependencies = [ dependencies = [
"base64 0.22.1", "base64 0.22.1",
"bitflags 2.9.3", "bitflags 2.9.3",

View file

@ -51,8 +51,8 @@ pub(crate) trait CspReporting {
fn is_trusted_type_policy_creation_allowed( fn is_trusted_type_policy_creation_allowed(
&self, &self,
global: &GlobalScope, global: &GlobalScope,
policy_name: String, policy_name: &str,
created_policy_names: Vec<String>, created_policy_names: &[&str],
) -> bool; ) -> bool;
fn does_sink_type_require_trusted_types( fn does_sink_type_require_trusted_types(
&self, &self,
@ -173,8 +173,8 @@ impl CspReporting for Option<CspList> {
fn is_trusted_type_policy_creation_allowed( fn is_trusted_type_policy_creation_allowed(
&self, &self,
global: &GlobalScope, global: &GlobalScope,
policy_name: String, policy_name: &str,
created_policy_names: Vec<String>, created_policy_names: &[&str],
) -> bool { ) -> bool {
let Some(csp_list) = self else { let Some(csp_list) = self else {
return true; return true;

View file

@ -71,19 +71,20 @@ impl TrustedTypePolicyFactory {
global: &GlobalScope, global: &GlobalScope,
can_gc: CanGc, can_gc: CanGc,
) -> Fallible<DomRoot<TrustedTypePolicy>> { ) -> Fallible<DomRoot<TrustedTypePolicy>> {
// Step 1: Let allowedByCSP be the result of executing Should Trusted Type policy creation be blocked by // Avoid double borrow on policy_names
// Content Security Policy? algorithm with global, policyName and factorys created policy names value. {
let allowed_by_csp = global // Step 1: Let allowedByCSP be the result of executing Should Trusted Type policy creation be blocked by
.get_csp_list() // Content Security Policy? algorithm with global, policyName and factorys created policy names value.
.is_trusted_type_policy_creation_allowed( let policy_names = self.policy_names.borrow();
global, let policy_names: Vec<&str> = policy_names.iter().map(String::as_ref).collect();
policy_name.clone(), let allowed_by_csp = global
self.policy_names.borrow().clone(), .get_csp_list()
); .is_trusted_type_policy_creation_allowed(global, &policy_name, &policy_names);
// Step 2: If allowedByCSP is "Blocked", throw a TypeError and abort further steps. // Step 2: If allowedByCSP is "Blocked", throw a TypeError and abort further steps.
if !allowed_by_csp { if !allowed_by_csp {
return Err(Error::Type("Not allowed by CSP".to_string())); return Err(Error::Type("Not allowed by CSP".to_string()));
}
} }
// Step 3: If policyName is default and the factorys default policy value is not null, throw a TypeError // Step 3: If policyName is default and the factorys default policy value is not null, throw a TypeError

View file

@ -1,17 +1,5 @@
[should-trusted-type-policy-creation-be-blocked-by-csp-002.html] [should-trusted-type-policy-creation-be-blocked-by-csp-002.html]
expected: TIMEOUT expected: TIMEOUT
[invalid tt-policy-name name "policy*name"]
expected: FAIL
[invalid tt-policy-name name "policy$name"]
expected: FAIL
[invalid tt-policy-name name "policy?name"]
expected: FAIL
[invalid tt-policy-name name "policy!name"]
expected: FAIL
[directive "trusted-type _TTP1_%09_TTP2_%0C_TTP3_%0D_TTP4_%20_TTP5_" (required-ascii-whitespace)] [directive "trusted-type _TTP1_%09_TTP2_%0C_TTP3_%0D_TTP4_%20_TTP5_" (required-ascii-whitespace)]
expected: TIMEOUT expected: TIMEOUT