mirror of
https://github.com/lexmount/moli.git
synced 2026-11-22 08:02:23 +00:00
fix(csp): validate Trusted Types directive name tokens
Discard non-ASCII directives independently before duplicate lookup and retain exact ASCII whitespace boundaries. Match literal Trusted Types allowlist entries only when they satisfy tt-policy-name, while preserving arbitrary policy names without CSP or under a wildcard. Add exhaustive ASCII token tests, directive parsing regressions, and enforce/report-only delivery coverage. Validated with fmt, full-workspace Clippy and all 17,735 nextest tests. The 219-case Trusted Types suite gains one passing case and five passing subtests in both CLI and CDP, with all other semantic results unchanged.
This commit is contained in:
@@ -620,6 +620,14 @@ fn policy_allows_trusted_type_policy_name(
|
||||
let Some(sources) = directive_source_list(policy, TRUSTED_TYPES) else {
|
||||
return true;
|
||||
};
|
||||
trusted_type_sources_allow_policy_name(&sources, policy_name, is_duplicate)
|
||||
}
|
||||
|
||||
fn trusted_type_sources_allow_policy_name(
|
||||
sources: &[&str],
|
||||
policy_name: &str,
|
||||
is_duplicate: bool,
|
||||
) -> bool {
|
||||
let name_is_allowed = sources.iter().any(|source| {
|
||||
let source = *source;
|
||||
source == "*"
|
||||
@@ -633,7 +641,7 @@ fn policy_allows_trusted_type_policy_name(
|
||||
let duplicate_is_allowed = !is_duplicate
|
||||
|| sources
|
||||
.iter()
|
||||
.any(|source| csp_keyword_eq(source.trim(), "allow-duplicates"));
|
||||
.any(|source| csp_keyword_eq(source, "allow-duplicates"));
|
||||
name_is_allowed && duplicate_is_allowed
|
||||
}
|
||||
|
||||
|
||||
@@ -2817,6 +2817,13 @@ fn trusted_types_csp_name_grammar_matches_only_valid_tokens() {
|
||||
false
|
||||
));
|
||||
}
|
||||
// Test the token matcher directly: delivery rejects this entire directive
|
||||
// before policy-name checks, so parsing it would test an absent directive.
|
||||
assert!(!trusted_type_sources_allow_policy_name(
|
||||
&["valid", "\u{000b}'allow-duplicates'"],
|
||||
"valid",
|
||||
true,
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -2836,6 +2843,16 @@ fn trusted_types_csp_name_grammar_does_not_restrict_unlisted_or_wildcard_names()
|
||||
false
|
||||
));
|
||||
}
|
||||
assert!(!policy_allows_trusted_type_policy_name(
|
||||
&ContentSecurityPolicy::from("trusted-types *"),
|
||||
name,
|
||||
true,
|
||||
));
|
||||
assert!(policy_allows_trusted_type_policy_name(
|
||||
&ContentSecurityPolicy::from("trusted-types * 'allow-duplicates'"),
|
||||
name,
|
||||
true,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -452,6 +452,80 @@ fn trusted_type_policy_creation_reports_name_and_duplicate_csp_violations() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trusted_types_csp_name_grammar_preserves_enforcement_and_report_only_delivery() {
|
||||
let policy = "trusted-types valid policy*name policy$name policy?name policy!name";
|
||||
let names = [
|
||||
"valid",
|
||||
"policy*name",
|
||||
"policy$name",
|
||||
"policy?name",
|
||||
"policy!name",
|
||||
"política",
|
||||
"",
|
||||
];
|
||||
for enforce in [false, true] {
|
||||
let mut vm = new_storage_test_vm("https://trusted-types-name-grammar.test/");
|
||||
if enforce {
|
||||
vm.set_response_content_security_policies(&[
|
||||
policy.to_owned(),
|
||||
"trusted-types ignored política".to_owned(),
|
||||
]);
|
||||
}
|
||||
vm.set_response_content_security_report_only_policies(&[
|
||||
policy.to_owned(),
|
||||
"trusted-types ignored política".to_owned(),
|
||||
]);
|
||||
let actual = vm.eval(r#"
|
||||
globalThis.policyNameGrammarReports = [];
|
||||
document.addEventListener('securitypolicyviolation', event => {
|
||||
policyNameGrammarReports.push([event.sample, event.originalPolicy, event.disposition]);
|
||||
});
|
||||
JSON.stringify(['valid', 'policy*name', 'policy$name', 'policy?name', 'policy!name', 'política', ''].map(name => {
|
||||
try { return trustedTypes.createPolicy(name).name; }
|
||||
catch (error) { return error instanceof TypeError ? 'TypeError' : error.name; }
|
||||
}));
|
||||
"#).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<serde_json::Value>(&actual).unwrap(),
|
||||
if enforce {
|
||||
serde_json::json!([
|
||||
"valid",
|
||||
"TypeError",
|
||||
"TypeError",
|
||||
"TypeError",
|
||||
"TypeError",
|
||||
"TypeError",
|
||||
"TypeError"
|
||||
])
|
||||
} else {
|
||||
serde_json::json!(names)
|
||||
},
|
||||
);
|
||||
assert_eq!(vm.eval("policyNameGrammarReports.length").unwrap(), "0");
|
||||
let expected = names[1..]
|
||||
.iter()
|
||||
.flat_map(|name| {
|
||||
let mut reports = Vec::new();
|
||||
if enforce {
|
||||
reports.push(serde_json::json!([name, policy, "enforce"]));
|
||||
}
|
||||
reports.push(serde_json::json!([name, policy, "report"]));
|
||||
reports
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
drain_pre_domcontentloaded_non_script_page_tasks_for_test(&mut vm),
|
||||
expected.len(),
|
||||
);
|
||||
let actual = vm.eval("JSON.stringify(policyNameGrammarReports)").unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<serde_json::Value>(&actual).unwrap(),
|
||||
serde_json::json!(expected),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trusted_type_policy_creation_reports_every_violated_policy() {
|
||||
let enforced = [
|
||||
|
||||
Reference in New Issue
Block a user