Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(renderer): synchronize child Documents in their current Realms

ldm048be4ffcc890
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs
index ebc6b9c77..872a2d97e 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs
@@ -70,10 +70,18 @@ impl JsContextHost {
.refresh_child_browsing_context(scope, handle)
.into_iter()
.collect::<Vec<_>>();
- let current_realm_child =
- crate::context_bootstrap::child_browsing_context_handle_for_current_realm_scope(scope);
- let context = if current_realm_child == Some(handle) {
- scope.get_current_context()
+ let current_context = scope.get_current_context();
+ // Refresh can commit a new Document while native synchronization still
+ // runs in the outgoing child Context. A frame handle survives that
+ // navigation; only its exact current Window/Realm can wrap the new DOM.
+ let current_realm_is_child = self
+ .window_execution_context_identity_for_access_check(current_context)
+ .is_some_and(|identity| {
+ identity.dispatch_scope() == OwnerDispatchScope::Child(handle)
+ && self.window_execution_context_identity_is_current(identity)
+ });
+ let context = if current_realm_is_child {
+ current_context
} else {
let context = self
.current_child_document_task_owner(handle)
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/sync.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/sync.rs
index 05d64c2d7..c1448239b 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/sync.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/sync.rs
@@ -68,6 +68,13 @@ impl JsContextHost {
handle: DomHandle,
wrapper: v8::Local<'s, v8::Object>,
) {
+ // Browser-owned updates use the target Realm. Reading its Location
+ // from the parent would apply cross-origin script checks during a
+ // legal iframe source setter and leave an exception pending.
+ let context = wrapper
+ .get_creation_context(scope)
+ .unwrap_or_else(|| scope.get_current_context());
+ let scope = &mut v8::ContextScope::new(scope, context);
self.sync_child_browsing_context_window_parent_top_slots(scope, handle, wrapper);
self.sync_child_window_origin_slot(scope, handle, wrapper);
let Some(visible_state) =
@@ -140,6 +147,10 @@ impl JsContextHost {
let Some(wrapper) = self.child_window_proxy_records.live_window(scope, handle) else {
return;
};
+ let context = wrapper
+ .get_creation_context(scope)
+ .unwrap_or_else(|| scope.get_current_context());
+ let scope = &mut v8::ContextScope::new(scope, context);
let Some(entry_seed) = self
.child_browsing_contexts
.get(&handle)
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/window.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/window.rs
index d5e2b1ad8..ed587174f 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/window.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/window.rs
@@ -992,16 +992,18 @@ impl JsContextHost {
scope: &mut v8::PinScope<'s, '_, ()>,
handle: DomHandle,
) -> bool {
+ // Endpoint projections contain functions from the outgoing Realm.
+ // Clear them for both same-origin and cross-origin navigation; a
+ // successor must not reuse a parent's postMessage function whose
+ // creating child Realm has already retired.
+ self.child_window_proxy_records
+ .clear_retired_realm_projections(handle);
// A same-origin caller can retain functions from the retired inner
// global. Keep that proxy detached until the replacement LocalWindow
// takes it over so those closures continue to resolve against their
// old inner global. Cross-origin callers can observe only the stable
// WindowProxy whitelist, so park that proxy on a restricted facade.
if self.top_window_can_access_child(handle) {
- // Drop caller-realm endpoint projections while the stable
- // WindowProxy waits for a replacement LocalWindow.
- self.child_window_proxy_records
- .clear_retired_realm_projections(handle);
return true;
}
let Some(window_proxy) = self.child_window_proxy_records.live_window(scope, handle) else {
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs
index 68dcd0d5d..63e549508 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs
@@ -1549,3 +1549,62 @@ fn child_browsing_context_lookup_tolerates_document_handle_cycle() {
Some(handle)
);
}
+
+#[tokio::test(flavor = "current_thread")]
+async fn cross_origin_child_source_changes_preserve_the_parent_execution_realm() {
+ for policy in [
+ moli_page_types::LayoutPolicy::Mock,
+ moli_page_types::LayoutPolicy::OnDemand,
+ ] {
+ let url = "https://child-source-sync.test/";
+ let mut vm = new_storage_test_vm(url);
+ vm.set_layout_policy(policy);
+ vm.eval("globalThis.frame=document.createElement('iframe');frame.srcdoc='<body>first</body>';(document.body||document.documentElement||document).appendChild(frame);'ready'").unwrap();
+ run_child_navigation_commit_and_host_load_for_test(&mut vm, "child source sync setup")
+ .await;
+ vm.eval("globalThis.savedDefault=frame.contentWindow;frame.removeAttribute('srcdoc');frame.src='data:text/html,<body>opaque</body>'; 'navigate'").unwrap();
+ run_child_navigation_commit_and_host_load_for_test(&mut vm, "opaque child source sync")
+ .await;
+ assert_eq!(vm.eval("(()=>{try{savedDefault.document;return 'accepted'}catch(error){return error.name+':'+(error instanceof DOMException)}})()").unwrap(), "SecurityError:true");
+ let opaque_context = vm
+ .live_child_default_runtime_realm_inventory()
+ .into_iter()
+ .next()
+ .expect("opaque child should have a committed default Realm")
+ .context_id;
+ assert_eq!(
+ vm.eval_in_child_default_context(
+ opaque_context,
+ "String(document instanceof Document)+'|'+String(document.createElement.constructor===Function)+'|'+String(document.defaultView===window)",
+ ).unwrap(),
+ "true|true|true",
+ "{policy:?}: a replacement Document must use its current child Realm"
+ );
+ assert_eq!(
+ vm.eval("frame.srcdoc='<body>restored</body>'; String(window===top)+'|'+document.URL")
+ .unwrap(),
+ format!("true|{url}"),
+ "{policy:?}"
+ );
+ run_child_navigation_commit_and_host_load_for_test(
+ &mut vm,
+ "same-origin child source sync",
+ )
+ .await;
+ assert_eq!(vm.eval("frame.contentDocument.body.textContent+'|'+String(savedDefault===frame.contentWindow)").unwrap(),"restored|true","{policy:?}");
+ let restored_context = vm
+ .live_child_default_runtime_realm_inventory()
+ .into_iter()
+ .next()
+ .expect("restored child should have a committed default Realm")
+ .context_id;
+ assert_eq!(
+ vm.eval_in_child_default_context(
+ restored_context,
+ "String(document instanceof Document)+'|'+String(document.createElement.constructor===Function)+'|'+String(document.defaultView===window)",
+ ).unwrap(),
+ "true|true|true",
+ "{policy:?}: returning to the parent origin must not retain old DOM prototypes"
+ );
+ }
+}