From fdd1cfe4331c6950cc359fd9b0943fcbfdc7a361 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Fri, 25 Sep 2026 08:13:37 +0800 Subject: [PATCH] fix(window): preserve popup roots for nested frames --- .../child_frame_runtime/window.rs | 41 +++++++--- .../context_host/child_frames/lookup.rs | 11 +++ .../child_frames/nested_history.rs | 16 +--- moli-renderer-v8/src/script_vm/tests/mod.rs | 2 + .../src/script_vm/tests/popup_root_window.rs | 81 +++++++++++++++++++ .../tests/fixtures/popup-root-window.js | 31 +++++++ 6 files changed, 161 insertions(+), 21 deletions(-) create mode 100644 moli-renderer-v8/src/script_vm/tests/popup_root_window.rs create mode 100644 moli-renderer-v8/tests/fixtures/popup-root-window.js 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 86346d9c191..dcc65a3bcd1 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 @@ -1404,7 +1404,9 @@ impl PageScriptHost { .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 = popup_owner.unwrap_or(root); + let top = self + .child_browsing_context_popup_root_window_for_realm(scope, handle) + .unwrap_or(root); let parent = popup_owner .or(stable_parent) .or(existing_parent) @@ -1441,6 +1443,10 @@ impl PageScriptHost { handle: DomHandle, global: v8::Local<'s, v8::Object>, ) -> (v8::Local<'s, v8::Object>, v8::Local<'s, v8::Object>) { + if let Some(top) = self.child_browsing_context_popup_root_window_for_realm(scope, handle) { + let parent = self.child_browsing_context_parent_window(scope, handle, top); + return (parent, top); + } let Some(window) = self.existing_child_browsing_context_window_wrapper(scope, handle) else { let top = self @@ -1454,12 +1460,6 @@ impl PageScriptHost { return (parent, top); }; - if let Some(popup_owner) = - self.child_browsing_context_popup_owner_window_for_realm(scope, handle) - { - return (popup_owner, popup_owner); - } - let top = self .child_window_proxy_records .browsing_context_top(scope, handle) @@ -1483,8 +1483,7 @@ impl PageScriptHost { handle: DomHandle, fallback: v8::Local<'s, v8::Object>, ) -> v8::Local<'s, v8::Object> { - if let Some(window) = - self.child_browsing_context_popup_owner_window_for_realm(scope, handle) + if let Some(window) = self.child_browsing_context_popup_root_window_for_realm(scope, handle) { return window; } @@ -1511,6 +1510,30 @@ impl PageScriptHost { handle: DomHandle, ) -> Option> { let popup_id = self.child_browsing_context_popup_owner_id(handle)?; + self.popup_window_for_child_realm(scope, handle, popup_id) + } + + fn child_browsing_context_popup_root_window_for_realm<'s>( + &mut self, + scope: &mut v8::PinScope<'s, '_>, + handle: DomHandle, + ) -> Option> { + // The root can be a popup even when the immediate parent is a child + // frame. Project it for the accessing child, not for its parent. + let super::super::OwnerDispatchScope::LightweightPopup(popup_id) = + self.child_browsing_context_root_scope(handle)? + else { + return None; + }; + self.popup_window_for_child_realm(scope, handle, popup_id) + } + + fn popup_window_for_child_realm<'s>( + &mut self, + scope: &mut v8::PinScope<'s, '_>, + handle: DomHandle, + popup_id: u64, + ) -> Option> { if self.child_window_can_access_lightweight_popup(handle, popup_id) { return self.lightweight_popup_window(scope, popup_id); } diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/lookup.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/lookup.rs index 310115d7b54..2a854f1ff8f 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/lookup.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/lookup.rs @@ -48,6 +48,17 @@ impl PageScriptHost { self.lightweight_popup_id_for_document_handle(owner_document) } + pub(in crate::native_bridge::context_host) fn child_browsing_context_root_scope( + &self, + handle: DomHandle, + ) -> Option { + let mut owner = self.owner_dispatch_scope_for_node(handle)?; + while let OwnerDispatchScope::Child(parent) = owner { + owner = self.owner_dispatch_scope_for_node(parent)?; + } + Some(owner) + } + pub(in crate::native_bridge::context_host) fn collect_child_browsing_context_handles_in_document_order_from_document( &self, document: DomHandle, diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/nested_history.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/nested_history.rs index ca8c2927c01..c7910dc15d5 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/nested_history.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/nested_history.rs @@ -107,14 +107,6 @@ impl NestedHistoryStore { } impl PageScriptHost { - fn nested_history_root(&self, handle: DomHandle) -> Option { - let mut owner = self.owner_dispatch_scope_for_node(handle)?; - while let OwnerDispatchScope::Child(parent) = owner { - owner = self.owner_dispatch_scope_for_node(parent)?; - } - Some(owner) - } - pub(crate) fn nested_history_store(&mut self, root: OwnerDispatchScope) -> NestedHistoryStore { if root == OwnerDispatchScope::Top { return self.top_level_navigation_history.nested_history(); @@ -196,7 +188,7 @@ impl PageScriptHost { { return None; } - let root = self.nested_history_root(handle)?; + let root = self.child_browsing_context_root_scope(handle)?; let popup = match root { OwnerDispatchScope::LightweightPopup(id) => Some(id), _ => None, @@ -239,7 +231,7 @@ impl PageScriptHost { }; let url = target.url.clone(); let root = self - .nested_history_root(handle) + .child_browsing_context_root_scope(handle) .unwrap_or(OwnerDispatchScope::Top); let popup = match root { OwnerDispatchScope::LightweightPopup(id) => Some(id), @@ -269,7 +261,7 @@ impl PageScriptHost { } pub(crate) fn remember_child_history(&mut self, handle: DomHandle) { - let Some(root) = self.nested_history_root(handle) else { + let Some(root) = self.child_browsing_context_root_scope(handle) else { return; }; let store = self.nested_history_store(root); @@ -332,7 +324,7 @@ impl PageScriptHost { .child_browsing_contexts .keys() .copied() - .filter(|handle| self.nested_history_root(*handle) == Some(root)) + .filter(|handle| self.child_browsing_context_root_scope(*handle) == Some(root)) .collect(); for handle in handles { self.remember_child_history(handle); diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs index e2f31c757aa..d271191619e 100644 --- a/moli-renderer-v8/src/script_vm/tests/mod.rs +++ b/moli-renderer-v8/src/script_vm/tests/mod.rs @@ -26,6 +26,7 @@ mod native_drag_pointer_coordinates; mod native_drag_pointer_suppression; #[cfg(feature = "devtools")] mod native_drag_source_events; +mod offline_audio_float; mod picture_in_picture_interfaces; mod touch_native_receivers; #[cfg(feature = "devtools")] @@ -2425,6 +2426,7 @@ mod observer_receivers; mod offline_audio_context_interfaces; mod offline_audio_float; mod periodic_wave_interfaces; +mod popup_root_window; mod post_parse; mod promise_jobs; diff --git a/moli-renderer-v8/src/script_vm/tests/popup_root_window.rs b/moli-renderer-v8/src/script_vm/tests/popup_root_window.rs new file mode 100644 index 00000000000..c811fe7ebd3 --- /dev/null +++ b/moli-renderer-v8/src/script_vm/tests/popup_root_window.rs @@ -0,0 +1,81 @@ +use super::*; + +#[tokio::test] +async fn popup_root_window_preserves_nested_parent_top_identity_and_origin_checks() { + const PROBE: &str = include_str!("../../../tests/fixtures/popup-root-window.js"); + const A: &str = "www.example.test"; + const B: &str = "remote.example.test"; + for hosts in [ + vec![B, B], + vec![A, B], + vec![A, B, B], + vec![A, B, A], + vec![B, A, B], + vec![B, B, A], + vec![A, A, A], + vec![A, B, A, B], + ] { + let bodies = (0..hosts.len()) + .map(|index| { + format!( + "", + serde_json::json!(hosts), + ) + }) + .collect(); + let server = StaticHttpServer::spawn_with_bodies(bodies).await; + let loader = static_http_loader([server.resolve_entry(A), server.resolve_entry(B)]); + let opener_url = server.url_for_host(A, "/opener"); + let popup_url = server.url_for_host(hosts[0], "/popup"); + let mut vm = + new_storage_page_task_executor_test_vm_with_loader(opener_url.as_str(), &loader); + vm.eval(&format!( + "globalThis.popupRootResult = null;\n\ + onmessage = event => popupRootResult = event.data;\n\ + globalThis.rootPopup = open({:?});", + popup_url.as_str(), + )) + .unwrap(); + advance_page_task_executor_until_eval_equals( + &mut vm, + &loader, + "String(popupRootResult !== null)", + "true", + &format!("nested popup roots: {hosts:?}"), + ) + .await; + let result: serde_json::Value = + serde_json::from_str(&vm.eval("JSON.stringify(popupRootResult)").unwrap()).unwrap(); + let child = hosts.last().unwrap(); + let parent_path = if hosts.len() == 2 { + "/popup".to_owned() + } else { + format!("/frame-{}", hosts.len() - 2) + }; + let top_path = if *child == hosts[0] { + "/popup" + } else { + "SecurityError" + }; + assert_eq!( + result, + serde_json::json!({ + "topHasOpener": true, + "topIsParent": hosts.len() == 2, + "parentTopIsTop": true, + "topParentIsTop": true, + "topSelfIsTop": true, + "topDocumentPath": top_path, + "parentDocumentPath": if *child == hosts[hosts.len() - 2] { + parent_path.as_str() + } else { + "SecurityError" + }, + "openerDocumentPath": if *child == A { "/opener" } else { "SecurityError" }, + }), + "{hosts:?}", + ); + vm.eval("rootPopup.close()").unwrap(); + assert_eq!(server.finish_targets().await.len(), hosts.len()); + } +} diff --git a/moli-renderer-v8/tests/fixtures/popup-root-window.js b/moli-renderer-v8/tests/fixtures/popup-root-window.js new file mode 100644 index 00000000000..fd20dceda7d --- /dev/null +++ b/moli-renderer-v8/tests/fixtures/popup-root-window.js @@ -0,0 +1,31 @@ +function popupRootFrame(hosts, index) { + if (index < hosts.length - 1) { + addEventListener('message', event => { + (index === 0 ? opener : parent).postMessage(event.data, '*'); + }); + addEventListener('load', () => { + const frame = document.createElement('iframe'); + const url = new URL('/frame-' + (index + 1), location.href); + url.hostname = hosts[index + 1]; + frame.src = url.href; + document.body.appendChild(frame); + }); + return; + } + + addEventListener('load', () => { + const read = callback => { + try { return callback(); } catch (error) { return error.name; } + }; + parent.postMessage({ + topHasOpener: top.opener !== null, + topIsParent: top === parent, + parentTopIsTop: parent.top === top, + topParentIsTop: top.parent === top, + topSelfIsTop: top.self === top, + topDocumentPath: read(() => new URL(top.document.URL).pathname), + parentDocumentPath: read(() => new URL(parent.document.URL).pathname), + openerDocumentPath: read(() => new URL(top.opener.document.URL).pathname), + }, '*'); + }); +}