Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(sandbox): block disallowed JavaScript dialogs

ldm00fb89bcd3130
diff --git a/moli-benchmark/wpt-cross-current/passed-cases.txt b/moli-benchmark/wpt-cross-current/passed-cases.txt
index af7942745..d246d25fd 100644
--- a/moli-benchmark/wpt-cross-current/passed-cases.txt
+++ b/moli-benchmark/wpt-cross-current/passed-cases.txt
@@ -5527,6 +5527,9 @@ html/semantics/embedded-content/the-iframe-element/iframe_javascript_url_remove_
html/semantics/embedded-content/the-iframe-element/iframe_remove_src.html
html/semantics/embedded-content/the-iframe-element/iframe_sandbox_allow_script.html
html/semantics/embedded-content/the-iframe-element/iframe_sandbox_allow_top_navigation-2.html
+html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-1.html
+html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-2.html
+html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-3.html
html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-4.html
html/semantics/embedded-content/the-iframe-element/iframe_sandbox_navigate_ancestor-2.html
html/semantics/embedded-content/the-iframe-element/iframe_sandbox_navigate_history_go_back-2.html
diff --git a/moli-renderer-v8/src/content_security_policy.rs b/moli-renderer-v8/src/content_security_policy.rs
index a812f6d2b..865c437ea 100644
--- a/moli-renderer-v8/src/content_security_policy.rs
+++ b/moli-renderer-v8/src/content_security_policy.rs
@@ -343,6 +343,12 @@ pub(crate) fn content_security_policy_sandbox_allows_scripts(policies: &[String]
.all(|policy| policy_sandbox_allows_scripts(policy).unwrap_or(true))
}
+pub(crate) fn content_security_policy_sandbox_allows_modals(policies: &[String]) -> bool {
+ policies
+ .iter()
+ .all(|policy| policy_sandbox_allows_modals(policy).unwrap_or(true))
+}
+
pub(crate) fn content_security_policy_sandbox_allows_popups_to_escape(policies: &[String]) -> bool {
let mut has_sandbox = false;
for policy in policies {
@@ -1171,6 +1177,11 @@ fn policy_sandbox_allows_scripts(policy: &str) -> Option<bool> {
directive_source_list(&directives, SANDBOX).map(sandbox_sources_allow_scripts)
}
+fn policy_sandbox_allows_modals(policy: &str) -> Option<bool> {
+ let directives = parsed_directives(policy);
+ directive_source_list(&directives, SANDBOX).map(sandbox_sources_allow_modals)
+}
+
fn policy_sandbox_allows_popups_to_escape(policy: &str) -> Option<bool> {
let directives = parsed_directives(policy);
directive_source_list(&directives, SANDBOX).map(sandbox_sources_allow_popups_to_escape)
@@ -1188,6 +1199,12 @@ fn sandbox_sources_allow_scripts(sources: &[&str]) -> bool {
.any(|token| token.eq_ignore_ascii_case("allow-scripts"))
}
+fn sandbox_sources_allow_modals(sources: &[&str]) -> bool {
+ sources
+ .iter()
+ .any(|token| token.eq_ignore_ascii_case("allow-modals"))
+}
+
fn sandbox_sources_allow_popups_to_escape(sources: &[&str]) -> bool {
sources
.iter()
@@ -3397,6 +3414,28 @@ mod tests {
]));
}
+ #[test]
+ fn sandbox_directive_allows_modals_only_when_all_active_sandboxes_allow_it() {
+ let allows_modals = |policies: &[&str]| {
+ content_security_policy_sandbox_allows_modals(
+ &policies
+ .iter()
+ .map(|policy| policy.to_string())
+ .collect::<Vec<_>>(),
+ )
+ };
+
+ assert!(allows_modals(&[]));
+ assert!(allows_modals(&["script-src 'self'"]));
+ assert!(!allows_modals(&["sandbox"]));
+ assert!(allows_modals(&["sandbox allow-modals"]));
+ assert!(allows_modals(&["sandbox ALLOW-MODALS"]));
+ assert!(!allows_modals(&[
+ "sandbox allow-modals",
+ "sandbox allow-scripts"
+ ]));
+ }
+
#[test]
fn sandbox_directive_allows_popups_to_escape_only_when_all_active_sandboxes_allow_it() {
let allows_popups_to_escape = |policies: &[&str]| {
diff --git a/moli-renderer-v8/src/context_bootstrap/window_runtime/dialogs.rs b/moli-renderer-v8/src/context_bootstrap/window_runtime/dialogs.rs
index 7d852c800..3df6aaad4 100644
--- a/moli-renderer-v8/src/context_bootstrap/window_runtime/dialogs.rs
+++ b/moli-renderer-v8/src/context_bootstrap/window_runtime/dialogs.rs
@@ -6,7 +6,7 @@ use crate::{
context_bootstrap::CHILD_BROWSING_CONTEXT_HANDLE_SLOT,
document_runtime::{DocumentPolicyContainer, DomHandle},
native_bridge::{
- InputNavigationPolicy, child_window_handle_from_marker_data,
+ InputNavigationPolicy, OwnerDispatchScope, child_window_handle_from_marker_data,
element::{
SpecialBrowsingContextTarget, navigate_existing_browsing_context_target,
navigate_named_iframe_target,
@@ -431,6 +431,18 @@ fn open_dialog(
// Page/Document source. A standalone or stale realm uses the headless
// default result instead of claiming a dialog that cannot be emitted.
let (target, source_document, source) = host.current_renderer_window_document_source(scope)?;
+ let allows_modals = match target.dispatch_scope() {
+ OwnerDispatchScope::Top => host.document_policy_container().sandbox.allows_modals,
+ OwnerDispatchScope::Child(handle) => host
+ .child_browsing_context_policy_container_snapshot(handle)
+ .is_some_and(|policy| policy.sandbox.allows_modals),
+ OwnerDispatchScope::LightweightPopup(popup_id) => host
+ .lightweight_popup_policy_container(popup_id)
+ .is_some_and(|policy| policy.sandbox.allows_modals),
+ };
+ if !allows_modals {
+ return None;
+ }
let source_url = window_open_entered_document_url(scope, host).to_string();
let dialog_id = host.allocate_javascript_dialog_id();
host.open_modal_javascript_dialog(
diff --git a/moli-renderer-v8/src/document_runtime.rs b/moli-renderer-v8/src/document_runtime.rs
index 69adbc96a..3814c5353 100644
--- a/moli-renderer-v8/src/document_runtime.rs
+++ b/moli-renderer-v8/src/document_runtime.rs
@@ -693,6 +693,7 @@ pub(crate) struct DocumentPolicyContainer {
pub(crate) struct DocumentSandboxPolicy {
pub(crate) forces_opaque_origin: bool,
pub(crate) allows_scripts: bool,
+ pub(crate) allows_modals: bool,
pub(crate) allows_popups_to_escape: bool,
pub(crate) sandboxes_document_domain: bool,
}
@@ -702,6 +703,7 @@ impl Default for DocumentSandboxPolicy {
Self {
forces_opaque_origin: false,
allows_scripts: true,
+ allows_modals: true,
allows_popups_to_escape: false,
sandboxes_document_domain: false,
}
@@ -719,6 +721,10 @@ impl DocumentSandboxPolicy {
crate::content_security_policy::content_security_policy_sandbox_allows_scripts(
policies,
),
+ allows_modals:
+ crate::content_security_policy::content_security_policy_sandbox_allows_modals(
+ policies,
+ ),
allows_popups_to_escape:
crate::content_security_policy::content_security_policy_sandbox_allows_popups_to_escape(
policies,
@@ -734,6 +740,7 @@ impl DocumentSandboxPolicy {
if response.sandboxes_document_domain {
self.forces_opaque_origin |= response.forces_opaque_origin;
self.allows_scripts &= response.allows_scripts;
+ self.allows_modals &= response.allows_modals;
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.rs
index b7a27379b..18feab26e 100644
--- a/moli-renderer-v8/src/document_runtime/security_policy.rs
+++ b/moli-renderer-v8/src/document_runtime/security_policy.rs
@@ -2229,26 +2229,28 @@ mod tests {
DocumentSandboxPolicy {
forces_opaque_origin: true,
allows_scripts: false,
+ allows_modals: false,
allows_popups_to_escape: false,
sandboxes_document_domain: true,
}
);
runtime.set_response_content_security_policies(&[String::from(
- "sandbox allow-scripts allow-same-origin allow-popups-to-escape-sandbox",
+ "sandbox allow-scripts allow-same-origin allow-modals allow-popups-to-escape-sandbox",
)]);
assert_eq!(
runtime.document_sandbox_policy(),
DocumentSandboxPolicy {
forces_opaque_origin: false,
allows_scripts: true,
+ allows_modals: true,
allows_popups_to_escape: true,
sandboxes_document_domain: true,
}
);
runtime.set_response_content_security_policies(&[
- String::from("sandbox allow-scripts allow-popups-to-escape-sandbox"),
+ String::from("sandbox allow-scripts allow-modals allow-popups-to-escape-sandbox"),
String::from("sandbox allow-same-origin"),
]);
assert_eq!(
@@ -2256,6 +2258,7 @@ mod tests {
DocumentSandboxPolicy {
forces_opaque_origin: true,
allows_scripts: false,
+ allows_modals: 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.rs
index ee5c53e4f..e6b9200f8 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
@@ -879,6 +879,7 @@ pub(in crate::native_bridge::context_host) fn document_sandbox_policy_from_attri
crate::document_runtime::DocumentSandboxPolicy {
forces_opaque_origin: sandbox_attribute_forces_opaque_origin(value),
allows_scripts: sandbox_attribute_allows_scripts(value),
+ allows_modals: sandbox_attribute_allows_modals(value),
allows_popups_to_escape: sandbox_attribute_allows_popups_to_escape(value),
sandboxes_document_domain: sandbox_attribute_sets_document_domain_flag(value),
}
@@ -902,6 +903,12 @@ fn sandbox_attribute_allows_scripts(value: &str) -> bool {
.any(|token| token.eq_ignore_ascii_case("allow-scripts"))
}
+fn sandbox_attribute_allows_modals(value: &str) -> bool {
+ value
+ .split_ascii_whitespace()
+ .any(|token| token.eq_ignore_ascii_case("allow-modals"))
+}
+
fn sandbox_attribute_allows_top_navigation(value: &str) -> bool {
value
.split_ascii_whitespace()
@@ -965,4 +972,43 @@ mod tests {
"allow-scripts ALLOW-SAME-ORIGIN"
));
}
+
+ #[test]
+ fn sandbox_attribute_allows_modals_only_with_the_escape_token() {
+ assert!(!document_sandbox_policy_from_attribute(Some("")).allows_modals);
+ assert!(
+ document_sandbox_policy_from_attribute(Some("allow-scripts allow-modals"))
+ .allows_modals
+ );
+ assert!(document_sandbox_policy_from_attribute(Some("ALLOW-MODALS")).allows_modals);
+ assert!(document_sandbox_policy_from_attribute(None).allows_modals);
+ }
+
+ #[test]
+ fn iframe_and_response_sandboxes_must_both_allow_modals() {
+ let response_without_modals =
+ crate::document_runtime::DocumentSandboxPolicy::from_response_content_security_policies(
+ &["sandbox allow-scripts".to_owned()],
+ );
+ let response_with_modals =
+ crate::document_runtime::DocumentSandboxPolicy::from_response_content_security_policies(
+ &["sandbox allow-scripts allow-modals".to_owned()],
+ );
+
+ assert!(
+ document_sandbox_policy_from_attribute(Some("allow-modals"))
+ .with_response_content_security_policy(response_with_modals)
+ .allows_modals
+ );
+ assert!(
+ !document_sandbox_policy_from_attribute(Some("allow-modals"))
+ .with_response_content_security_policy(response_without_modals)
+ .allows_modals
+ );
+ assert!(
+ !document_sandbox_policy_from_attribute(Some("allow-scripts"))
+ .with_response_content_security_policy(response_with_modals)
+ .allows_modals
+ );
+ }
}
diff --git a/moli-renderer-v8/src/native_bridge/context_host/popups.rs b/moli-renderer-v8/src/native_bridge/context_host/popups.rs
index e0d71c035..fa8bf834a 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/popups.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/popups.rs
@@ -439,6 +439,7 @@ fn inherit_lightweight_popup_opener_sandbox(
};
target.forces_opaque_origin |= opener.forces_opaque_origin;
target.allows_scripts &= opener.allows_scripts;
+ target.allows_modals &= opener.allows_modals;
target.allows_popups_to_escape &= opener.allows_popups_to_escape;
target.sandboxes_document_domain |= opener.sandboxes_document_domain;
}
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
index 798ec67b4..1d1d22cb7 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
@@ -23599,6 +23599,98 @@ fn standalone_dialog_handler_without_page_residence_uses_headless_defaults() {
);
}
+#[tokio::test]
+async fn sandboxed_child_without_allow_modals_uses_dialog_defaults_without_opening_one() {
+ let mut vm = new_storage_test_vm("https://sandbox-dialogs.test/");
+ vm.eval(
+ r#"
+(() => {
+ const frame = document.createElement("iframe");
+ frame.id = "sandboxed-dialog-frame";
+ frame.sandbox = "allow-scripts allow-same-origin";
+ frame.srcdoc = `<script>
+ function openDialogs() {
+ return [
+ String(alert("blocked alert")),
+ String(confirm("blocked confirm")),
+ String(prompt("blocked prompt", "default"))
+ ].join("|");
+ }
+ <\/script>`;
+ (document.body || document.documentElement || document).appendChild(frame);
+})()
+"#,
+ )
+ .expect("sandboxed dialog child setup should evaluate");
+ run_realm_prerequisite_then_expected_child_frame_semantic_turn_for_test(
+ &mut vm,
+ ChildFrameSemanticTurnKind::NavigationCommit,
+ "sandboxed dialog child should commit before its parser script",
+ )
+ .await;
+ run_realm_prerequisite_then_expected_child_frame_semantic_turn_for_test(
+ &mut vm,
+ ChildFrameSemanticTurnKind::DocumentScriptReady,
+ "sandboxed dialog child script should run",
+ )
+ .await;
+ run_child_document_lifecycle_and_host_load_for_test(&mut vm, "sandboxed dialog child").await;
+
+ assert_eq!(
+ vm.eval("document.getElementById('sandboxed-dialog-frame').contentWindow.openDialogs()")
+ .expect("sandboxed child dialog defaults should evaluate"),
+ "undefined|false|null"
+ );
+ assert!(
+ vm.take_pending_javascript_dialogs().is_empty(),
+ "a child without allow-modals must not publish a JavaScript dialog"
+ );
+}
+
+#[tokio::test]
+async fn sandboxed_child_with_allow_modals_can_open_a_dialog() {
+ let mut vm = new_storage_test_vm("https://sandbox-dialogs.test/");
+ vm.eval(
+ r#"
+(() => {
+ const frame = document.createElement("iframe");
+ frame.id = "allowed-dialog-frame";
+ frame.sandbox = "allow-scripts allow-same-origin allow-modals";
+ frame.srcdoc = `<script>
+ function openAlert() { return alert("allowed alert"); }
+ <\/script>`;
+ (document.body || document.documentElement || document).appendChild(frame);
+})()
+"#,
+ )
+ .expect("allowed dialog child setup should evaluate");
+ run_realm_prerequisite_then_expected_child_frame_semantic_turn_for_test(
+ &mut vm,
+ ChildFrameSemanticTurnKind::NavigationCommit,
+ "allowed dialog child should commit before its parser script",
+ )
+ .await;
+ run_realm_prerequisite_then_expected_child_frame_semantic_turn_for_test(
+ &mut vm,
+ ChildFrameSemanticTurnKind::DocumentScriptReady,
+ "allowed dialog child script should run",
+ )
+ .await;
+ run_child_document_lifecycle_and_host_load_for_test(&mut vm, "allowed dialog child").await;
+
+ assert_eq!(
+ vm.eval(
+ "String(document.getElementById('allowed-dialog-frame').contentWindow.openAlert())"
+ )
+ .expect("allowed child dialog should evaluate"),
+ "undefined"
+ );
+ let dialogs = vm.take_pending_javascript_dialogs();
+ assert_eq!(dialogs.len(), 1);
+ assert_eq!(dialogs[0].dialog_type(), "alert");
+ assert_eq!(dialogs[0].message(), "allowed alert");
+}
+
#[test]
fn window_open_rejects_invalid_urls_before_selecting_a_target() {
let mut vm = new_storage_test_vm("https://example.com/");