Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(csp): preserve parsed source token boundaries

ldm0488b8713d278
diff --git a/moli-renderer-v8/src/content_security_policy.rs b/moli-renderer-v8/src/content_security_policy.rs
index 466289fda..03d8be051 100644
--- a/moli-renderer-v8/src/content_security_policy.rs
+++ b/moli-renderer-v8/src/content_security_policy.rs
@@ -1146,7 +1146,7 @@ fn policy_allows_trusted_types_eval(policy: &str) -> bool {
.is_some_and(|sources| {
sources
.iter()
- .any(|source| csp_keyword_eq(source.trim(), "trusted-types-eval"))
+ .any(|source| csp_keyword_eq(source, "trusted-types-eval"))
})
}
@@ -1448,7 +1448,7 @@ fn inline_style_element_source_list_allows(
fn inline_source_violation_sample(source_list: &[&str], source: &str) -> String {
if !source_list
.iter()
- .any(|source| csp_keyword_eq(source.trim(), "report-sample"))
+ .any(|source| csp_keyword_eq(source, "report-sample"))
{
return String::new();
}
@@ -1483,14 +1483,7 @@ fn normalized_source_list_allows_url(
}
fn csp_nonce_source_matches(source: &str, nonce: &str) -> bool {
- let source = source.trim();
- let Some(value) = source
- .strip_prefix("'nonce-")
- .and_then(|value| value.strip_suffix('\''))
- else {
- return false;
- };
- value == nonce
+ csp_nonce_source_value(source).is_some_and(|value| value == nonce)
}
fn source_list_activates_strict_dynamic(sources: &[&str]) -> bool {
@@ -1504,7 +1497,6 @@ fn source_list_activates_strict_dynamic(sources: &[&str]) -> bool {
fn csp_nonce_source_value(source: &str) -> Option<&str> {
source
- .trim()
.strip_prefix("'nonce-")
.and_then(|value| value.strip_suffix('\''))
}
@@ -1545,7 +1537,7 @@ fn hash_source_value(value: &str) -> Option<CspHashSourceValue<'_>> {
}
fn csp_hash_source_value(source: &str) -> Option<CspHashSourceValue<'_>> {
- let source = source.trim().strip_prefix('\'')?.strip_suffix('\'')?;
+ let source = source.strip_prefix('\'')?.strip_suffix('\'')?;
hash_source_value(source)
}
@@ -2002,20 +1994,18 @@ impl ContentSecurityPolicyNonUrlKind {
}
Self::DocumentInlineScript => source_list
.iter()
- .any(|source| csp_keyword_eq(source.trim(), "unsafe-inline")),
+ .any(|source| csp_keyword_eq(source, "unsafe-inline")),
Self::DocumentInlineStyleElement => source_list
.iter()
- .any(|source| csp_keyword_eq(source.trim(), "unsafe-inline")),
+ .any(|source| csp_keyword_eq(source, "unsafe-inline")),
Self::Eval => source_list
.iter()
- .any(|source| csp_keyword_eq(source.trim(), "unsafe-eval")),
+ .any(|source| csp_keyword_eq(source, "unsafe-eval")),
Self::TrustedTypesEval => source_list.iter().any(|source| {
- let source = source.trim();
csp_keyword_eq(source, "unsafe-eval")
|| csp_keyword_eq(source, "trusted-types-eval")
}),
Self::WasmEval => source_list.iter().any(|source| {
- let source = source.trim();
csp_keyword_eq(source, "unsafe-eval") || csp_keyword_eq(source, "wasm-unsafe-eval")
}),
}
@@ -2023,10 +2013,6 @@ impl ContentSecurityPolicyNonUrlKind {
}
fn inline_event_handler_source_list_allows(source_list: &[&str], source: &str) -> bool {
- let source_list = source_list
- .iter()
- .map(|source| source.trim())
- .collect::<Vec<_>>();
let has_nonce_or_hash = source_list.iter().any(|source| {
csp_nonce_source_value(source).is_some() || csp_hash_source_value(source).is_some()
});
@@ -2314,6 +2300,176 @@ mod tests {
}
}
+ #[test]
+ fn csp_keywords_do_not_strip_control_characters_from_source_tokens() {
+ use ContentSecurityPolicyNonUrlKind::*;
+ for (kind, keyword) in [
+ (DocumentInlineScript, "unsafe-inline"),
+ (DocumentInlineStyleElement, "unsafe-inline"),
+ (DocumentInlineEventHandler, "unsafe-inline"),
+ (DocumentInlineNavigation, "unsafe-inline"),
+ (DocumentInlineStyleAttribute, "unsafe-inline"),
+ (Eval, "unsafe-eval"),
+ (TrustedTypesEval, "unsafe-eval"),
+ (TrustedTypesEval, "trusted-types-eval"),
+ (WasmEval, "unsafe-eval"),
+ (WasmEval, "wasm-unsafe-eval"),
+ ] {
+ for padding in [
+ "",
+ " \t\n\r\u{000c}",
+ "\0",
+ "\u{000b}",
+ "\u{001f}",
+ "\u{007f}",
+ ] {
+ let valid = padding.is_empty() || padding.starts_with(' ');
+ for source in [
+ format!("{padding}'{keyword}'"),
+ format!("'{keyword}'{padding}"),
+ ] {
+ let policy = format!("default-src {source}");
+ let violation = content_security_policy_non_url_violation_with_source(
+ &policy,
+ &protected_url(),
+ kind,
+ None,
+ ContentSecurityPolicyDisposition::Enforce,
+ &ContentSecurityPolicyReportingEndpoints::default(),
+ );
+ assert_eq!(violation.is_none(), valid, "{kind:?}: {policy:?}");
+ }
+ }
+ }
+ }
+
+ #[test]
+ fn csp_nonce_and_hash_tokens_preserve_their_boundaries() {
+ let source = "t1.done();";
+ let integrity = "sha256-wmuLCpoj8EMqfQlPnt5NIMgKkCK62CxAkAiewI0zZps=";
+ for expression in ["'nonce-abc'".to_owned(), format!("'{integrity}'")] {
+ for (prefix, suffix, valid) in [
+ ("", "", true),
+ ("\t\n", " \r\u{000c}", true),
+ ("\u{000b}", "", false),
+ ("", "\u{000b}", false),
+ ] {
+ let token = format!("{prefix}{expression}{suffix}");
+ let policy = format!("default-src {token}");
+ let request = ContentSecurityPolicyScriptElementRequest {
+ nonce: Some("abc"),
+ integrity: Some(integrity),
+ parser_inserted: true,
+ };
+ assert_eq!(
+ script_element_request_allowed(&policy, request),
+ valid,
+ "{policy:?}"
+ );
+ let endpoints = ContentSecurityPolicyReportingEndpoints::default();
+ let script = content_security_policy_inline_script_element_violation_with_disposition_and_reporting_endpoints(
+ &policy, &protected_url(), source, request,
+ ContentSecurityPolicyDisposition::Enforce, &endpoints,
+ );
+ assert_eq!(script.is_none(), valid, "inline script: {policy:?}");
+ let style = content_security_policy_inline_style_element_violation_with_disposition_and_reporting_endpoints(
+ &policy, &protected_url(), source,
+ ContentSecurityPolicyStyleElementRequest { nonce: Some("abc") },
+ ContentSecurityPolicyDisposition::Enforce, &endpoints,
+ );
+ assert_eq!(style.is_none(), valid, "inline style: {policy:?}");
+
+ // 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}");
+ assert_eq!(
+ script_element_request_allowed(
+ &dynamic_policy,
+ ContentSecurityPolicyScriptElementRequest {
+ parser_inserted: true,
+ ..ContentSecurityPolicyScriptElementRequest::default()
+ }
+ ),
+ !valid,
+ "{dynamic_policy:?}",
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn csp_unsafe_hashes_requires_an_exact_keyword_and_hash_token() {
+ let hash = "'sha256-wmuLCpoj8EMqfQlPnt5NIMgKkCK62CxAkAiewI0zZps='";
+ for (sources, allowed) in [
+ (format!("'unsafe-hashes' {hash}"), true),
+ (format!("\u{000b}'unsafe-hashes' {hash}"), false),
+ (format!("'unsafe-hashes'\u{000b} {hash}"), false),
+ (format!("'unsafe-hashes' \u{000b}{hash}"), false),
+ (format!("'unsafe-hashes' {hash}\u{000b}"), false),
+ ] {
+ for kind in [
+ ContentSecurityPolicyNonUrlKind::DocumentInlineEventHandler,
+ ContentSecurityPolicyNonUrlKind::DocumentInlineStyleAttribute,
+ ] {
+ let violation = content_security_policy_inline_source_violation_with_disposition_and_reporting_endpoints(
+ &format!("default-src {sources}"), &protected_url(), kind, "t1.done();",
+ ContentSecurityPolicyDisposition::Enforce,
+ &ContentSecurityPolicyReportingEndpoints::default(),
+ );
+ assert_eq!(violation.is_none(), allowed, "{kind:?}: {sources:?}");
+ }
+ }
+ }
+
+ #[test]
+ fn csp_report_sample_and_trusted_types_eval_preserve_source_tokens() {
+ for (prefix, suffix, valid) in [
+ ("", "", true),
+ ("\t", "\r\u{000c}", true),
+ ("\u{000b}", "", false),
+ ("", "\u{000b}", false),
+ ] {
+ let policy = format!("script-src {prefix}'report-sample'{suffix}");
+ for disposition in [
+ ContentSecurityPolicyDisposition::Enforce,
+ ContentSecurityPolicyDisposition::Report,
+ ] {
+ let violation = content_security_policy_non_url_violation_with_source(
+ &policy,
+ &protected_url(),
+ ContentSecurityPolicyNonUrlKind::Eval,
+ Some("blocked()"),
+ disposition,
+ &ContentSecurityPolicyReportingEndpoints::default(),
+ )
+ .expect("report-sample does not authorize evaluation");
+ assert_eq!(violation.original_policy, policy);
+ assert_eq!(violation.disposition, disposition);
+ assert_eq!(
+ violation.sample,
+ if valid { "blocked()" } else { "" },
+ "{policy:?}"
+ );
+ }
+ let policy = format!(
+ "require-trusted-types-for 'script'; script-src {prefix}'trusted-types-eval'{suffix}"
+ );
+ assert_eq!(
+ content_security_policy_allows_trusted_types_eval(std::slice::from_ref(&policy)),
+ valid,
+ "{policy:?}"
+ );
+ }
+ }
+
#[test]
fn frame_ancestors_requires_every_ancestor_to_match() {
let protected_url = "https://child.test/frame.html";
diff --git a/moli-renderer-v8/src/document_runtime/security_policy.rs b/moli-renderer-v8/src/document_runtime/security_policy.rs
index e8e37a401..d78334bed 100644
--- a/moli-renderer-v8/src/document_runtime/security_policy.rs
+++ b/moli-renderer-v8/src/document_runtime/security_policy.rs
@@ -1703,7 +1703,7 @@ impl DocumentRuntime {
}
element
.attribute("content")
- .map(str::trim)
+ .map(str::trim_ascii)
.filter(|content| !content.is_empty())
.map(str::to_owned)
};
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.rs
index bedb00618..d6e413392 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
@@ -1099,6 +1099,54 @@ async fn prepared_parser_inline_script_csp_blocks_before_v8_execution_and_report
);
}
+#[test]
+fn document_csp_eval_keywords_preserve_source_token_boundaries() {
+ for from_meta in [false, true] {
+ for keyword in ["unsafe-eval", "wasm-unsafe-eval"] {
+ for (prefix, suffix, valid) in [
+ ("", "", true),
+ ("\t\n", " \r\u{000c}", true),
+ ("\u{000b}", "", false),
+ ("", "\u{000b}", false),
+ ] {
+ let policy = format!("script-src {prefix}'{keyword}'{suffix}");
+ let mut vm = new_storage_test_vm("https://csp-source-tokens.test/");
+ if from_meta {
+ vm.document_runtime
+ .dom_host_mut()
+ .reset_html_document_shell();
+ vm.eval(&format!(
+ "const meta = document.createElement('meta'); meta.httpEquiv = 'Content-Security-Policy'; meta.content = {}; document.head.appendChild(meta);",
+ serde_json::to_string(&policy).unwrap(),
+ )).expect("install meta CSP without changing source token boundaries");
+ } else {
+ vm.set_response_content_security_policies(std::slice::from_ref(&policy));
+ }
+ let result = vm
+ .eval(&format!(
+ "JSON.stringify({})",
+ include_str!("../../../../tests/fixtures/csp-eval-source-tokens.js"),
+ ))
+ .expect("CSP execution probe");
+ let expected = serde_json::json!([
+ if valid && keyword == "unsafe-eval" {
+ "allowed"
+ } else {
+ "EvalError"
+ },
+ if valid && keyword == "unsafe-eval" {
+ "allowed"
+ } else {
+ "EvalError"
+ },
+ if valid { "allowed" } else { "CompileError" },
+ ]);
+ assert_eq!(result, expected.to_string(), "meta={from_meta}: {policy:?}");
+ }
+ }
+ }
+}
+
#[tokio::test]
async fn eval_csp_violation_reports_external_script_scheme_and_call_location() {
let source = r#"
diff --git a/moli-renderer-v8/tests/fixtures/csp-eval-source-tokens.js b/moli-renderer-v8/tests/fixtures/csp-eval-source-tokens.js
new file mode 100644
index 000000000..5761dd58e
--- /dev/null
+++ b/moli-renderer-v8/tests/fixtures/csp-eval-source-tokens.js
@@ -0,0 +1,15 @@
+(() => {
+ const attempt = callback => {
+ try {
+ callback();
+ return 'allowed';
+ } catch (error) {
+ return error.name;
+ }
+ };
+ return [
+ attempt(() => eval('21 * 2')),
+ attempt(() => new Function('return 42')()),
+ attempt(() => new WebAssembly.Module(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0]))),
+ ];
+})()