Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(csp): discard invalid values without reviving duplicate directives
ldm05dc79f3363c5
diff --git a/moli-renderer-v8/src/content_security_policy.rs b/moli-renderer-v8/src/content_security_policy.rsindex d5e3b554f..c1785706d 100644--- a/moli-renderer-v8/src/content_security_policy.rs+++ b/moli-renderer-v8/src/content_security_policy.rs@@ -1,4 +1,4 @@-use std::collections::BTreeMap;+use std::collections::{BTreeMap, HashSet};use crate::context_bootstrap::{initialize_event_object, mark_event_trusted};use crate::network::ResourceRequestClient;@@ -1191,18 +1191,28 @@ fn effective_source_list_with_directive(}fn parsed_directives(policy: &str) -> Vec<(&str, Vec<&str>)> {+ let mut names = HashSet::new();policy.split(';').filter_map(|directive| {- // CSP discards the entire non-ASCII directive, independently of- // other directives in the same policy and before duplicate lookup.- if !directive.is_ascii() {+ // Split on exactly CSP's ASCII whitespace. Do not trim controls+ // into a valid directive name or source expression.+ let mut parts = directive.split_ascii_whitespace();+ let name = parts.next()?;+ // The first occurrence takes the name even if its value is invalid.+ // Later duplicates must not override it or suppress a fallback.+ if !names.insert(name.to_ascii_lowercase()) {return None;}- let mut parts = directive.split_ascii_whitespace();- // split_ascii_whitespace already trims exactly the CSP whitespace- // set. A subsequent str::trim would incorrectly erase vertical tabs.- Some((parts.next()?, parts.collect()))+ // Non-whitespace controls, DEL and non-ASCII characters invalidate+ // the entire directive, without affecting the rest of the policy.+ if !directive+ .bytes()+ .all(|byte| byte != b',' && (byte.is_ascii_whitespace() || byte.is_ascii_graphic()))+ {+ return None;+ }+ Some((name, parts.collect()))}).collect()}@@ -2269,15 +2279,160 @@ mod tests {);assert_eq!(collect(b"img-src *\x0b, img-src 'none'"),- ["img-src *\u{000b}", "img-src 'none'"]+ ["img-src 'none'"]);let policies = collect(b"img-src *\x0b");- assert!(!content_security_policy_allows_url(+ assert!(policies.is_empty());+ assert!(content_security_policy_allows_url(&policies,&protected_url(),&request_url("https://cdn.test/asset"),ContentSecurityPolicyResourceKind::DocumentImage,));+ assert!(!content_security_policy_allows_url(+ &collect(b"img-src *\x0b, img-src 'none'"),+ &protected_url(),+ &request_url("https://cdn.test/asset"),+ ContentSecurityPolicyResourceKind::DocumentImage,+ ));+ }+ }++ #[test]+ fn csp_single_directive_values_reject_commas() {+ let directives = parsed_directives("img-src https://cdn.test/a,b; script-src 'none'");+ assert!(directive_source_list(&directives, IMG_SRC).is_none());+ assert_eq!(+ directive_source_list(&directives, SCRIPT_SRC),+ Some(["'none'"].as_slice())+ );+ }++ #[test]+ fn csp_invalid_directive_characters_do_not_block_resources_or_discard_other_directives() {+ for invalid in ('\0'..='\u{0008}')+ .chain(['\u{000b}'])+ .chain('\u{000e}'..='\u{001f}')+ .chain(['\u{007f}', '\u{0080}', '\u{00a0}', '\u{feff}', '中'])+ {+ for value in [+ format!("{invalid}'none'"),+ format!("'no{invalid}ne'"),+ format!("'none'{invalid}"),+ ] {+ let policy = format!("img-src {value}; script-src 'none'; report-uri /blocked");+ let policies = [policy.clone()];+ for disposition in [+ ContentSecurityPolicyDisposition::Enforce,+ ContentSecurityPolicyDisposition::Report,+ ] {+ let violation = |kind| {+ content_security_policy_url_violation_with_redirect_status_disposition_and_reporting_endpoints(+ &policies,+ &protected_url(),+ &request_url("https://cdn.test/asset"),+ kind,+ ContentSecurityPolicyRedirectStatus::NoRedirect,+ disposition,+ &ContentSecurityPolicyReportingEndpoints::default(),+ )+ };+ assert!(+ violation(ContentSecurityPolicyResourceKind::DocumentImage).is_none(),+ "{policy:?}",+ );+ let blocked =+ violation(ContentSecurityPolicyResourceKind::DocumentScriptElement)+ .expect("the valid script-src directive must still block");+ assert_eq!(blocked.effective_directive, SCRIPT_SRC_ELEM);+ assert_eq!(blocked.disposition, disposition);+ assert_eq!(blocked.original_policy, policy);+ assert_eq!(blocked.report_uri_endpoints, ["https://app.test/blocked"]);+ }+ }+ }+ }++ #[test]+ fn csp_invalid_directives_preserve_fallbacks_and_trusted_types_requirements() {+ for invalid in ['\0', '\u{000b}', '\u{001f}', '\u{007f}', '\u{00a0}'] {+ let policy = format!(+ "default-src 'self'; img-src *{invalid}; \+ script-src 'unsafe-eval'{invalid}; trusted-types *{invalid}; \+ require-trusted-types-for 'script'"+ );+ assert!(!allowed(+ &policy,+ ContentSecurityPolicyResourceKind::DocumentImage,+ "https://cdn.test/asset",+ ));+ assert!(allowed(+ &policy,+ ContentSecurityPolicyResourceKind::DocumentImage,+ "https://app.test/asset",+ ));+ assert!(+ content_security_policy_non_url_violation_with_source(+ &policy,+ &protected_url(),+ ContentSecurityPolicyNonUrlKind::Eval,+ None,+ ContentSecurityPolicyDisposition::Enforce,+ &ContentSecurityPolicyReportingEndpoints::default(),+ )+ .is_some(),+ "{policy:?}",+ );+ assert!(policy_requires_trusted_types_for_script(&policy));+ assert!(policy_allows_trusted_type_policy_name(&policy, "unlisted"));++ let policy = format!(+ "trusted-types allowed; require-trusted-types-for 'script'{invalid}; img-src 'none'"+ );+ assert!(!policy_requires_trusted_types_for_script(&policy));+ assert!(policy_allows_trusted_type_policy_name(&policy, "allowed"));+ assert!(!policy_allows_trusted_type_policy_name(&policy, "unlisted"));+ assert!(!allowed(+ &policy,+ ContentSecurityPolicyResourceKind::DocumentImage,+ "https://app.test/asset",+ ));+ }+ }++ #[test]+ fn csp_invalid_first_values_do_not_activate_later_duplicate_directives() {+ for invalid in ['\0', '\u{000b}', '\u{001f}', '\u{007f}', '\u{00a0}', '中'] {+ let policy = format!("default-src 'none'; img-src *{invalid}; IMG-SRC *");+ assert!(!allowed(+ &policy,+ ContentSecurityPolicyResourceKind::DocumentImage,+ "https://cdn.test/asset",+ ));+ let policy = format!("img-src 'none'{invalid}; IMG-SRC 'none'");+ assert!(allowed(+ &policy,+ ContentSecurityPolicyResourceKind::DocumentImage,+ "https://cdn.test/asset",+ ));+ for policy in [+ format!("default-src 'none'; img-src{invalid} 'none'; img-src *"),+ format!("default-src 'none'; {invalid}img-src 'none'; img-src *"),+ ] {+ assert!(+ allowed(+ &policy,+ ContentSecurityPolicyResourceKind::DocumentImage,+ "https://cdn.test/asset",+ ),+ "{policy:?}"+ );+ }+ let policy = format!("trusted-types *{invalid}; TRUSTED-TYPES allowed");+ assert!(policy_allows_trusted_type_policy_name(&policy, "unlisted"));+ let policy = format!("trusted-types allowed; TRUSTED-TYPES *{invalid}");+ assert!(policy_allows_trusted_type_policy_name(&policy, "allowed"));+ assert!(!policy_allows_trusted_type_policy_name(&policy, "unlisted"));}}@@ -2309,7 +2464,10 @@ mod tests {format!("{padding}'{keyword}'"),format!("'{keyword}'{padding}"),] {- let policy = format!("default-src {source}");+ let policy = format!(+ "default-src 'none'; {} {source}",+ kind.effective_directive()+ );let violation = content_security_policy_non_url_violation_with_source(&policy,&protected_url(),@@ -2336,7 +2494,7 @@ mod tests {("", "\u{000b}", false),] {let token = format!("{prefix}{expression}{suffix}");- let policy = format!("default-src {token}");+ let policy = format!("default-src 'none'; script-src {token}; style-src {token}");let request = ContentSecurityPolicyScriptElementRequest {nonce: Some("abc"),integrity: Some(integrity),@@ -2362,25 +2520,19 @@ mod tests {// Invalid nonce/hash tokens must not disable unsafe-inline or// activate strict-dynamic and erase a URL allowlist either.- let inline_policy = format!("script-src 'unsafe-inline' {token}");- let handler = content_security_policy_inline_source_violation_with_disposition_and_reporting_endpoints(- &inline_policy, &protected_url(),- ContentSecurityPolicyNonUrlKind::DocumentInlineEventHandler,- "untrusted()", ContentSecurityPolicyDisposition::Enforce, &endpoints,- );- assert_eq!(handler.is_none(), !valid, "{inline_policy:?}");- let dynamic_policy =- format!("script-src 'strict-dynamic' https://cdn.test {token}");+ let inline_sources = format!("'unsafe-inline' {token}");+ let inline_sources: Vec<_> = inline_sources.split_ascii_whitespace().collect();assert_eq!(- script_element_request_allowed(- &dynamic_policy,- ContentSecurityPolicyScriptElementRequest {- parser_inserted: true,- ..ContentSecurityPolicyScriptElementRequest::default()- }- ),+ inline_event_handler_source_list_allows(&inline_sources, "untrusted()"),!valid,- "{dynamic_policy:?}",+ "{inline_sources:?}",+ );+ let dynamic_sources = format!("'strict-dynamic' https://cdn.test {token}");+ let dynamic_sources: Vec<_> = dynamic_sources.split_ascii_whitespace().collect();+ assert_eq!(+ source_list_activates_strict_dynamic(&dynamic_sources),+ valid,+ "{dynamic_sources:?}",);}}@@ -2401,7 +2553,8 @@ mod tests {ContentSecurityPolicyNonUrlKind::DocumentInlineStyleAttribute,] {let violation = content_security_policy_inline_source_violation_with_disposition_and_reporting_endpoints(- &format!("default-src {sources}"), &protected_url(), kind, "t1.done();",+ &format!("default-src 'none'; {} {sources}", kind.effective_directive()),+ &protected_url(), kind, "t1.done();",ContentSecurityPolicyDisposition::Enforce,&ContentSecurityPolicyReportingEndpoints::default(),);@@ -2418,7 +2571,7 @@ mod tests {("\u{000b}", "", false),("", "\u{000b}", false),] {- let policy = format!("script-src {prefix}'report-sample'{suffix}");+ let policy = format!("default-src 'none'; script-src {prefix}'report-sample'{suffix}");for disposition in [ContentSecurityPolicyDisposition::Enforce,ContentSecurityPolicyDisposition::Report,@@ -3014,9 +3167,18 @@ mod tests {ContentSecurityPolicyRedirectStatus::NoRedirect,ContentSecurityPolicyRedirectStatus::FollowedRedirect,] {+ assert!(+ !source_expression_matches(+ source,+ &protected_url(),+ &request_url(request),+ redirect_status,+ ),+ "{source:?} must not match {request:?} ({redirect_status:?})"+ );assert!(!content_security_policy_allows_url_with_redirect_status(- &[format!("img-src {source}")],+ &[format!("default-src 'none'; img-src {source}")],&protected_url(),&request_url(request),ContentSecurityPolicyResourceKind::DocumentImage,@@ -4363,7 +4525,13 @@ mod tests {#[test]fn trusted_types_csp_name_grammar_matches_only_valid_tokens() {- for byte in 0..=127u8 {+ // Directive grammar errors and policy delimiters are tested separately: they discard the+ // directive before its tt-policy-name tokens are interpreted.+ for byte in [b'\t', b'\n', b'\x0c', b'\r']+ .into_iter()+ .chain(b' '..=b'~')+ .filter(|byte| !matches!(*byte, b',' | b';'))+ {let name = format!("policy{}name", char::from(byte));let policy = format!("trusted-types {name}");let expected = byte.is_ascii_alphanumeric() || b"-#=_/@.%".contains(&byte);@@ -4387,7 +4555,7 @@ mod tests {"trusted-types valid policy*name","policy*name"));- for wildcard in ["\u{000b}*", "*\u{000b}", "policy*"] {+ for wildcard in ["policy*", "**"] {assert!(!policy_allows_trusted_type_policy_name(&format!("trusted-types {wildcard}"),"valid"@@ -4412,7 +4580,7 @@ mod tests {}#[test]- fn csp_directive_parser_discards_each_non_ascii_directive_before_duplicate_matching() {+ fn csp_directive_parser_discards_invalid_values_without_replacing_first_directives() {for directive in ["trusted-types allowed política","trusted-types\u{a0}allowed",@@ -4428,18 +4596,12 @@ mod tests {"trusted-types invalid política; TRUSTED-TYPES allowed; trusted-types *; \script-src 'none'; img-src https://例子.test; img-src 'self'",);- assert_eq!(- directive_source_list(&directives, TRUSTED_TYPES),- Some(["allowed"].as_slice()),- );+ assert!(directive_source_list(&directives, TRUSTED_TYPES).is_none());assert_eq!(directive_source_list(&directives, SCRIPT_SRC),Some(["'none'"].as_slice()),);- assert_eq!(- directive_source_list(&directives, IMG_SRC),- Some(["'self'"].as_slice()),- );+ assert!(directive_source_list(&directives, IMG_SRC).is_none());}#[test]diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/security_policy.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/security_policy.rsindex a2436c569..e5aafc1e2 100644--- a/moli-renderer-v8/src/script_vm/tests/browser_api/security_policy.rs+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/security_policy.rs@@ -1115,7 +1115,7 @@ fn document_csp_eval_keywords_preserve_source_token_boundaries() {("\u{000b}", "", false),("", "\u{000b}", false),] {- let policy = format!("script-src {prefix}'{keyword}'{suffix}");+ let policy = format!("default-src 'none'; script-src {prefix}'{keyword}'{suffix}");let mut vm = new_storage_test_vm("https://csp-source-tokens.test/");if from_meta {vm.document_runtimediff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/trusted_types.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/trusted_types.rsindex 023e799ec..b59f58bda 100644--- a/moli-renderer-v8/src/script_vm/tests/browser_api/trusted_types.rs+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/trusted_types.rs@@ -23,9 +23,25 @@ fn trusted_types_csp_policy_creation_matches_only_valid_name_tokens() {("trusted-types ignored política; trusted-types valid","ignored",+ "allowed",+ ),+ (+ "trusted-types valid; trusted-types ignored política",+ "ignored","TypeError",),- ("trusted-types \u{000b}*", "arbitrary", "TypeError"),+ (+ "trusted-types valid; trusted-types ignored política",+ "valid",+ "allowed",+ ),+ ("trusted-types \u{000b}*", "arbitrary", "allowed"),+ ("trusted-types *\u{000b}", "arbitrary", "allowed"),+ (+ "trusted-types valid \u{000b}'allow-duplicates'",+ "arbitrary",+ "allowed",+ ),("trusted-types *", "name with spaces", "allowed"),("", "name with spaces", "allowed"),("trusted-types valid", "policy$name", "TypeError"),@@ -1742,3 +1758,26 @@ fn transformed_trusted_script_rejection_does_not_report_a_sink_mismatch() {let _ = drain_pre_domcontentloaded_non_script_page_tasks_for_test(&mut vm);assert_eq!(vm.eval("JSON.stringify(compilationReports)").unwrap(), "[]");}++#[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 element = document.createElement('div');+ let errorName = 'none';+ try { element.innerHTML = '<i>untrusted</i>'; }+ catch (error) { errorName = error.name; }+ element.innerHTML = first.createHTML('<b>trusted</b>');+ return [errorName, element.innerHTML].join('|');+ })()"#,+ )+ .expect("discarding an invalid name directive must retain valid sink requirements");+ assert_eq!(result, "TypeError|<b>trusted</b>", "{policy:?}");+ }+}diff --git a/moli-renderer-v8/src/worker/thread/tests/modules.rs b/moli-renderer-v8/src/worker/thread/tests/modules.rsindex 690253730..b40cff813 100644--- a/moli-renderer-v8/src/worker/thread/tests/modules.rs+++ b/moli-renderer-v8/src/worker/thread/tests/modules.rs@@ -820,7 +820,7 @@ async fn worker_csp_string_compilation_keywords_preserve_source_token_boundaries("\u{000b}", "", false),("", "\u{000b}", false),] {- let policy = format!("script-src {prefix}'{keyword}'{suffix}");+ let policy = format!("default-src 'none'; script-src {prefix}'{keyword}'{suffix}");let source = format!("postMessage(({}).slice(0, 2)); close();",include_str!("../../../../tests/fixtures/csp-eval-source-tokens.js"),