fix(window): preserve popup roots for nested frames

This commit is contained in:
ldm0
2026-11-22 02:01:54 +08:00
parent 05292bca7f
commit fdd1cfe433
6 changed files with 161 additions and 21 deletions
@@ -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<v8::Local<'s, v8::Object>> {
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<v8::Local<'s, v8::Object>> {
// 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<v8::Local<'s, v8::Object>> {
if self.child_window_can_access_lightweight_popup(handle, popup_id) {
return self.lightweight_popup_window(scope, popup_id);
}
@@ -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<OwnerDispatchScope> {
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,
@@ -107,14 +107,6 @@ impl NestedHistoryStore {
}
impl PageScriptHost {
fn nested_history_root(&self, handle: DomHandle) -> Option<OwnerDispatchScope> {
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);
@@ -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;
@@ -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!(
"<!doctype html><body><script>{PROBE}\npopupRootFrame({}, {index});</script>",
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());
}
}
+31
View File
@@ -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),
}, '*');
});
}