Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(window): preserve top identity across cross-origin ancestor walks

ldm0d4b7493259c3
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 54e4a7c45..a236042f2 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
@@ -1368,7 +1368,6 @@ impl JsContextHost {
window: v8::Local<'s, v8::Object>,
) {
let global = scope.get_current_context().global(scope);
- let same_origin_with_top = self.child_browsing_context_is_same_origin_with_top(handle);
let existing_parent =
Self::child_window_non_cross_origin_object_slot(scope, window, "__moliWindowParent");
let existing_top =
@@ -1384,27 +1383,11 @@ impl JsContextHost {
.filter(|top| !is_cross_origin_window_proxy(scope, *top))
.unwrap_or_else(|| self.child_browsing_context_root_window(scope, handle, global));
let popup_owner = self.child_browsing_context_popup_owner_window_for_realm(scope, handle);
- let top = if let Some(window) = popup_owner {
- window
- } else if same_origin_with_top {
- root
- } else {
- self.cross_origin_window_endpoint_projection_for_child(
- scope,
- handle,
- PendingWindowMessageEndpoint::TopWindow,
- )
- .unwrap_or(window)
- };
- let parent = if let Some(window) = popup_owner {
- window
- } else if same_origin_with_top {
- stable_parent
- .or(existing_parent)
- .unwrap_or_else(|| self.child_browsing_context_parent_window(scope, handle, top))
- } else {
- self.child_browsing_context_parent_window(scope, handle, top)
- };
+ let top = popup_owner.unwrap_or(root);
+ let parent = popup_owner
+ .or(stable_parent)
+ .or(existing_parent)
+ .unwrap_or_else(|| self.child_browsing_context_parent_window(scope, handle, top));
set_object_slot(scope, window, "__moliWindowParent", parent.into());
set_object_slot(scope, window, "__moliWindowTop", top.into());
}
@@ -1456,44 +1439,20 @@ impl JsContextHost {
return (popup_owner, popup_owner);
}
- if self.child_browsing_context_is_same_origin_with_top(handle) {
- let top = self
- .child_window_proxy_records
- .browsing_context_top(scope, handle)
- .or_else(|| {
- Self::child_window_non_cross_origin_object_slot(
- scope,
- window,
- "__moliWindowTop",
- )
- })
- .unwrap_or_else(|| self.child_browsing_context_root_window(scope, handle, global));
- let parent = self
- .child_window_proxy_records
- .browsing_context_parent(scope, handle)
- .or_else(|| {
- Self::child_window_non_cross_origin_object_slot(
- scope,
- window,
- "__moliWindowParent",
- )
- })
- .unwrap_or_else(|| self.child_browsing_context_parent_window(scope, handle, top));
- return (parent, top);
- }
-
- let existing_top = Self::child_window_object_slot(scope, window, "__moliWindowTop");
- let top = existing_top
- .filter(|top| is_cross_origin_window_proxy(scope, *top))
+ let top = self
+ .child_window_proxy_records
+ .browsing_context_top(scope, handle)
.or_else(|| {
- self.cross_origin_window_endpoint_projection_for_child(
- scope,
- handle,
- PendingWindowMessageEndpoint::TopWindow,
- )
+ Self::child_window_non_cross_origin_object_slot(scope, window, "__moliWindowTop")
})
- .unwrap_or(global);
- let parent = self.child_browsing_context_parent_window(scope, handle, top);
+ .unwrap_or_else(|| self.child_browsing_context_root_window(scope, handle, global));
+ let parent = self
+ .child_window_proxy_records
+ .browsing_context_parent(scope, handle)
+ .or_else(|| {
+ Self::child_window_non_cross_origin_object_slot(scope, window, "__moliWindowParent")
+ })
+ .unwrap_or_else(|| self.child_browsing_context_parent_window(scope, handle, top));
(parent, top)
}
@@ -1555,17 +1514,10 @@ impl JsContextHost {
}
match endpoint {
PendingWindowMessageEndpoint::TopWindow => {
- let projection = build_cross_origin_top_window_proxy(scope);
- self.child_window_proxy_records
- .set_cross_origin_endpoint_projection(
- scope,
- accessing_handle,
- endpoint,
- projection,
- );
- let storage = cross_origin_proxy_storage_object(scope, projection);
- set_cross_origin_object_slot(scope, storage, "opener", v8::null(scope).into());
- Some(projection)
+ let dispatch = super::super::OwnerDispatchScope::Top;
+ let owner = self.current_window_execution_context_owner(dispatch)?;
+ let (_, context) = self.window_execution_context(scope, owner, dispatch)?;
+ Some(context.global(scope))
}
PendingWindowMessageEndpoint::ChildWindow(handle) => {
self.child_browsing_context_cross_origin_window_proxy(scope, handle)
@@ -1836,12 +1788,6 @@ fn build_detached_cross_origin_window_index_proxy<'s>(
proxy
}
-fn build_cross_origin_top_window_proxy<'s>(
- scope: &mut v8::PinScope<'s, '_>,
-) -> v8::Local<'s, v8::Object> {
- build_cross_origin_top_level_window_proxy(scope, PendingWindowMessageEndpoint::TopWindow)
-}
-
fn build_cross_origin_popup_window_proxy<'s>(
scope: &mut v8::PinScope<'s, '_>,
popup_id: u64,
diff --git a/moli-renderer-v8/src/script_vm/tests/extracted/realms_and_teardown.rs b/moli-renderer-v8/src/script_vm/tests/extracted/realms_and_teardown.rs
index b99bb598b..3d83eda2f 100644
--- a/moli-renderer-v8/src/script_vm/tests/extracted/realms_and_teardown.rs
+++ b/moli-renderer-v8/src/script_vm/tests/extracted/realms_and_teardown.rs
@@ -2916,8 +2916,7 @@ fn context_weak_was_collected_for_test(
}
fn expose_real_top_window_for_test(vm: &mut StandaloneScriptVmHarness, context_id: i64) {
- // `parent` on an opaque child is a caller-local projection. The cache and
- // borrowed-receiver bugs live on the real top Window.
+ // Exercise the native access-check handlers on the actual top WindowProxy.
vm.with_child_frame_realm_context_scope(context_id, |scope, host_ptr| {
let parent_context = unsafe { &*host_ptr }
.page_default_context(scope)
@@ -3132,6 +3131,45 @@ void sibling.contentWindow;
);
}
+#[tokio::test]
+async fn cross_origin_ancestor_search_reaches_the_actual_top_window() {
+ let mut vm = new_storage_test_vm("https://cross-origin-ancestors.test/");
+ let outer = commit_opaque_child_for_test(&mut vm, "outer").await;
+ vm.eval_in_child_default_context(
+ outer,
+ r#"
+ const nested = document.createElement('iframe');
+ nested.srcdoc = '<body>nested';
+ document.body.appendChild(nested);
+ void nested.contentWindow;
+ 'created'
+ "#,
+ )
+ .unwrap();
+ run_child_navigation_commit_and_host_load_for_test(&mut vm, "nested opaque child").await;
+ let inner = vm
+ .live_child_default_runtime_realm_inventory()
+ .into_iter()
+ .find(|realm| realm.context_id != outer)
+ .expect("nested realm")
+ .context_id;
+ let result = vm.eval_in_child_default_context(inner, r#"
+ (() => {
+ // Adform's trackpoint-async.js walks parent until it reaches the
+ // captured top. Bound the search so a split identity fails quickly.
+ const capturedTop = top;
+ let ancestor = window, steps = 0;
+ for (; ancestor && steps < 8; steps++) {
+ try { if (ancestor.frames.__tcfapiLocator) break; } catch (_) {}
+ if (ancestor === capturedTop) break;
+ ancestor = ancestor.parent;
+ }
+ return JSON.stringify([ancestor === capturedTop, steps, parent.parent === top, top.parent === top]);
+ })()
+ "#).expect("bounded cross-origin ancestor search");
+ assert_eq!(result, "[true,2,true,true]");
+}
+
#[test]
fn frame_owner_content_accessors_live_on_exact_owner_prototypes() {
let mut vm = new_storage_html_test_vm("https://frame-owner-content-accessors.test/");