mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 08:02:59 +00:00
fix(csp): discard invalid values without reviving duplicate directives
Ignore directives containing non-whitespace controls, DEL or non-ASCII characters. Reserve the first directive name even when its value is ignored, so later duplicates cannot replace it or bypass a valid default-src fallback. Cover header/meta parsing, independent directives, reporting, Trusted Types sinks and worker string/Wasm compilation. Keep source-token boundary checks separate from invalid directive handling.
This commit is contained in:
+24
-10
@@ -493,11 +493,13 @@ fn csp_invalid_directives_preserve_fallbacks_and_trusted_types_requirements() {
|
||||
assert!(policy_requires_trusted_types_for_script(
|
||||
&ContentSecurityPolicy::from(policy.as_str())
|
||||
));
|
||||
assert!(policy_allows_trusted_type_policy_name(
|
||||
&ContentSecurityPolicy::from(policy.as_str()),
|
||||
"unlisted",
|
||||
false
|
||||
));
|
||||
for duplicate in [false, true] {
|
||||
assert!(policy_allows_trusted_type_policy_name(
|
||||
&ContentSecurityPolicy::from(policy.as_str()),
|
||||
"unlisted",
|
||||
duplicate,
|
||||
));
|
||||
}
|
||||
|
||||
let policy = format!(
|
||||
"trusted-types allowed; require-trusted-types-for 'script'{invalid}; img-src 'none'"
|
||||
@@ -552,11 +554,13 @@ fn csp_invalid_first_values_do_not_activate_later_duplicate_directives() {
|
||||
);
|
||||
}
|
||||
let policy = format!("trusted-types *{invalid}; TRUSTED-TYPES allowed");
|
||||
assert!(policy_allows_trusted_type_policy_name(
|
||||
&ContentSecurityPolicy::from(policy.as_str()),
|
||||
"unlisted",
|
||||
false
|
||||
));
|
||||
for duplicate in [false, true] {
|
||||
assert!(policy_allows_trusted_type_policy_name(
|
||||
&ContentSecurityPolicy::from(policy.as_str()),
|
||||
"unlisted",
|
||||
duplicate,
|
||||
));
|
||||
}
|
||||
let policy = format!("trusted-types allowed; TRUSTED-TYPES *{invalid}");
|
||||
assert!(policy_allows_trusted_type_policy_name(
|
||||
&ContentSecurityPolicy::from(policy.as_str()),
|
||||
@@ -568,6 +572,11 @@ fn csp_invalid_first_values_do_not_activate_later_duplicate_directives() {
|
||||
"unlisted",
|
||||
false
|
||||
));
|
||||
assert!(!policy_allows_trusted_type_policy_name(
|
||||
&ContentSecurityPolicy::from(policy.as_str()),
|
||||
"allowed",
|
||||
true,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2826,6 +2835,11 @@ fn trusted_types_csp_name_grammar_matches_only_valid_tokens() {
|
||||
"valid",
|
||||
true,
|
||||
));
|
||||
assert!(!policy_allows_trusted_type_policy_name(
|
||||
&ContentSecurityPolicy::from("trusted-types valid x'allow-duplicates'"),
|
||||
"valid",
|
||||
true,
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -56,6 +56,30 @@ fn trusted_types_csp_policy_creation_matches_only_valid_name_tokens() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trusted_types_invalid_name_directives_preserve_sink_requirements() {
|
||||
for invalid in ['\0', '\u{000b}', '\u{001f}', '\u{007f}', '\u{00a0}'] {
|
||||
let mut vm = new_storage_test_vm("https://trusted-types-invalid-directive.test/");
|
||||
let policy = format!("trusted-types *{invalid}; require-trusted-types-for 'script'");
|
||||
vm.set_response_content_security_policies(std::slice::from_ref(&policy));
|
||||
let result = vm
|
||||
.eval(
|
||||
r#"(() => {
|
||||
const first = trustedTypes.createPolicy('unlisted', {createHTML: value => value});
|
||||
const second = trustedTypes.createPolicy('unlisted', {createHTML: value => value});
|
||||
const element = document.createElement('div');
|
||||
let errorName = 'none';
|
||||
try { element.innerHTML = '<i>untrusted</i>'; }
|
||||
catch (error) { errorName = error.name; }
|
||||
element.innerHTML = second.createHTML('<b>trusted</b>');
|
||||
return [first !== second, errorName, element.innerHTML].join('|');
|
||||
})()"#,
|
||||
)
|
||||
.expect("discarding an invalid name directive must retain valid sink requirements");
|
||||
assert_eq!(result, "true|TypeError|<b>trusted</b>", "{policy:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trusted_types_webidl_surface_checks_descriptors_arguments_and_brands() {
|
||||
let mut vm = new_storage_test_vm("https://trusted-types-webidl.test/");
|
||||
|
||||
@@ -61,8 +61,16 @@ async fn worker_wasm_csp_covers_sync_async_and_streaming_compilation() {
|
||||
"require-trusted-types-for 'script'; script-src 'trusted-types-eval'",
|
||||
false,
|
||||
),
|
||||
("script-src \u{000b}'wasm-unsafe-eval'", false),
|
||||
("script-src 'unsafe-eval'\u{000b}", false),
|
||||
("script-src \u{000b}'wasm-unsafe-eval'", true),
|
||||
("script-src 'unsafe-eval'\u{000b}", true),
|
||||
(
|
||||
"default-src 'none'; script-src \u{000b}'wasm-unsafe-eval'",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"default-src 'none'; script-src 'unsafe-eval'\u{000b}",
|
||||
false,
|
||||
),
|
||||
] {
|
||||
let result = wasm_csp_result(
|
||||
wasm_csp_options(if allowed { 0 } else { 5 })
|
||||
|
||||
Reference in New Issue
Block a user