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(sandbox): preserve active document navigation permissions
ldm0791c4c1dc569
diff --git a/moli-renderer-v8/src/content_security_policy.rs b/moli-renderer-v8/src/content_security_policy.rsindex 5c54e1619..d8a7da2b3 100644--- a/moli-renderer-v8/src/content_security_policy.rs+++ b/moli-renderer-v8/src/content_security_policy.rs@@ -364,6 +364,17 @@ pub(crate) fn content_security_policy_sandbox_allows_modals(policies: &[String]).all(|policy| policy_sandbox_allows_modals(policy).unwrap_or(true))}+pub(crate) fn content_security_policy_sandbox_allows_top_navigation(policies: &[String]) -> bool {+ policies.iter().all(|policy| {+ let directives = parsed_directives(policy);+ directive_source_list(&directives, SANDBOX).is_none_or(|sources| {+ sources+ .iter()+ .any(|token| token.eq_ignore_ascii_case("allow-top-navigation"))+ })+ })+}+pub(crate) fn content_security_policy_sandbox_allows_popups_to_escape(policies: &[String]) -> bool {let mut has_sandbox = false;for policy in policies {diff --git a/moli-renderer-v8/src/document_runtime.rs b/moli-renderer-v8/src/document_runtime.rsindex 726e1fba7..4126a45ee 100644--- a/moli-renderer-v8/src/document_runtime.rs+++ b/moli-renderer-v8/src/document_runtime.rs@@ -651,6 +651,7 @@ pub(crate) struct DocumentSandboxPolicy {pub(crate) forces_opaque_origin: bool,pub(crate) allows_scripts: bool,pub(crate) allows_modals: bool,+ pub(crate) allows_top_navigation: bool,pub(crate) allows_popups_to_escape: bool,pub(crate) sandboxes_document_domain: bool,}@@ -661,6 +662,7 @@ impl Default for DocumentSandboxPolicy {forces_opaque_origin: false,allows_scripts: true,allows_modals: true,+ allows_top_navigation: true,allows_popups_to_escape: false,sandboxes_document_domain: false,}@@ -682,6 +684,10 @@ impl DocumentSandboxPolicy {crate::content_security_policy::content_security_policy_sandbox_allows_modals(policies,),+ allows_top_navigation:+ crate::content_security_policy::content_security_policy_sandbox_allows_top_navigation(+ policies,+ ),allows_popups_to_escape:crate::content_security_policy::content_security_policy_sandbox_allows_popups_to_escape(policies,@@ -698,6 +704,7 @@ impl DocumentSandboxPolicy {self.forces_opaque_origin |= response.forces_opaque_origin;self.allows_scripts &= response.allows_scripts;self.allows_modals &= response.allows_modals;+ self.allows_top_navigation &= response.allows_top_navigation;self.allows_popups_to_escape = if self.sandboxes_document_domain {self.allows_popups_to_escape && response.allows_popups_to_escape} else {diff --git a/moli-renderer-v8/src/document_runtime/security_policy.rs b/moli-renderer-v8/src/document_runtime/security_policy.rsindex 9f7b3ee29..359cec7dc 100644--- a/moli-renderer-v8/src/document_runtime/security_policy.rs+++ b/moli-renderer-v8/src/document_runtime/security_policy.rs@@ -2263,6 +2263,7 @@ mod tests {forces_opaque_origin: true,allows_scripts: false,allows_modals: false,+ allows_top_navigation: false,allows_popups_to_escape: false,sandboxes_document_domain: true,}@@ -2277,6 +2278,7 @@ mod tests {forces_opaque_origin: false,allows_scripts: true,allows_modals: true,+ allows_top_navigation: false,allows_popups_to_escape: true,sandboxes_document_domain: true,}@@ -2292,6 +2294,7 @@ mod tests {forces_opaque_origin: true,allows_scripts: false,allows_modals: false,+ allows_top_navigation: false,allows_popups_to_escape: false,sandboxes_document_domain: true,}diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rsindex a8629a413..db0c9e488 100644--- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs@@ -847,9 +847,9 @@ impl JsContextHost {}pub(crate) fn child_browsing_context_allows_top_navigation(&self, handle: DomHandle) -> bool {- self.dom_host()- .get_attribute(handle, "sandbox")- .is_none_or(|sandbox| sandbox_attribute_allows_top_navigation(&sandbox))+ self.child_browsing_contexts+ .get(&handle)+ .is_some_and(|entry| entry.document_sandbox_policy().allows_top_navigation)}}@@ -921,6 +921,7 @@ pub(in crate::native_bridge::context_host) fn document_sandbox_policy_from_attriforces_opaque_origin: sandbox_attribute_forces_opaque_origin(value),allows_scripts: sandbox_attribute_allows_scripts(value),allows_modals: sandbox_attribute_allows_modals(value),+ allows_top_navigation: sandbox_attribute_allows_top_navigation(value),allows_popups_to_escape: sandbox_attribute_allows_popups_to_escape(value),sandboxes_document_domain: sandbox_attribute_sets_document_domain_flag(value),}@@ -1025,6 +1026,33 @@ mod tests {assert!(document_sandbox_policy_from_attribute(None).allows_modals);}+ #[test]+ fn iframe_and_response_sandboxes_must_both_allow_top_navigation() {+ for (attribute, owner_allows) in [+ (None, true),+ (Some("allow-scripts"), false),+ (Some("ALLOW-TOP-NAVIGATION"), true),+ ] {+ for (policies, response_allows) in [+ (vec!["script-src 'self'"], true),+ (vec!["sandbox allow-scripts"], false),+ (vec!["sandbox allow-top-navigation"], true),+ (vec!["sandbox allow-top-navigation", "sandbox"], false),+ ] {+ let response = crate::document_runtime::DocumentSandboxPolicy::from_response_content_security_policies(+ &policies.iter().map(|policy| (*policy).to_owned()).collect::<Vec<_>>(),+ );+ assert_eq!(+ document_sandbox_policy_from_attribute(attribute)+ .with_response_content_security_policy(response)+ .allows_top_navigation,+ owner_allows && response_allows,+ "attribute={attribute:?}, response={policies:?}"+ );+ }+ }+ }+#[test]fn iframe_and_response_sandboxes_must_both_allow_modals() {let response_without_modals =diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation.rsindex a0f6410f8..724b2f43e 100644--- a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation.rs+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation.rs@@ -4565,6 +4565,83 @@ async fn reset_navigation_history_updates_prebootstrapped_child_default_realm());}+#[tokio::test]+async fn history_traversal_uses_active_document_sandbox_until_navigation() {+ let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");+ let restricted = "allow-scripts allow-same-origin";+ let permitted = "allow-scripts allow-same-origin allow-top-navigation";+ for (initial, changed, initially_allowed, subsequently_allowed) in [+ (None, Some(restricted), true, false),+ (Some(permitted), Some(restricted), true, false),+ (Some(restricted), None, false, true),+ (Some(restricted), Some(permitted), false, true),+ ] {+ let mut vm = new_storage_page_task_executor_test_vm_with_loader(+ "https://history-active-sandbox.test/parent",+ &loader,+ );+ let initial = serde_json::to_string(&initial).unwrap();+ let changed = serde_json::to_string(&changed).unwrap();+ vm.eval(&format!(+ r#"+globalThis.frame = document.createElement('iframe');+if ({initial} !== null) frame.setAttribute('sandbox', {initial});+frame.srcdoc = '<p>original document</p>';+(document.body || document.documentElement || document).appendChild(frame);+"#+ ))+ .unwrap();+ vm.drain_ready_page_task_executor_turns_for_setup(&loader, 128)+ .await+ .unwrap();+ vm.eval(&format!(+ r#"+globalThis.originalDocument = frame.contentDocument;+if ({changed} === null) frame.removeAttribute('sandbox');+else frame.setAttribute('sandbox', {changed});+"#+ ))+ .unwrap();+ assert_eq!(+ vm.eval("frame.contentDocument === originalDocument")+ .unwrap(),+ "true"+ );++ for (navigate, allowed) in [(false, initially_allowed), (true, subsequently_allowed)] {+ if navigate {+ vm.eval("frame.srcdoc = '<p>replacement document</p>';")+ .unwrap();+ vm.drain_ready_page_task_executor_turns_for_setup(&loader, 128)+ .await+ .unwrap();+ assert_eq!(+ vm.eval("frame.contentDocument === originalDocument")+ .unwrap(),+ "false"+ );+ }+ vm.eval(+ "history.replaceState(0, ''); history.pushState(1, ''); frame.contentWindow.eval('history.back()');",+ )+ .unwrap();+ assert_eq!(+ vm.run_one_history_traversal_executor_turn(&loader)+ .await+ .unwrap(),+ allowed,+ "initial={initial}, changed={changed}, navigated={navigate}"+ );+ assert_eq!(+ vm.eval("String(history.state)").unwrap(),+ if allowed { "0" } else { "1" },+ "initial={initial}, changed={changed}, navigated={navigate}"+ );+ assert!(vm.take_pending_top_level_history_traversal().is_none());+ }+ }+}+#[tokio::test]async fn history_methods_from_child_realm_traverse_receiver_history() {let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");