fix(iframe): discard child window relations synchronously

This commit is contained in:
ldm0
2026-11-22 02:01:48 +08:00
parent 351b5198ce
commit 1b2ebf2998
5 changed files with 116 additions and 16 deletions
@@ -755,6 +755,10 @@ fn legacy_unforgeable_parent_getter<'s>(
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'s, v8::Value>,
) {
if super::window_accessors::window_has_discarded_child_browsing_context(scope, args.this()) {
rv.set_null();
return;
}
if let Some(value) =
legacy_unforgeable_window_slot_value(scope, args.this(), WINDOW_PARENT_SLOT)
{
@@ -775,6 +779,10 @@ fn legacy_unforgeable_top_getter<'s>(
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'s, v8::Value>,
) {
if super::window_accessors::window_has_discarded_child_browsing_context(scope, args.this()) {
rv.set_null();
return;
}
if let Some(value) = legacy_unforgeable_window_slot_value(scope, args.this(), WINDOW_TOP_SLOT) {
rv.set(value);
}
@@ -27,7 +27,10 @@ pub(crate) use document::{
bind_current_child_window_document, retain_window_document_in_retired_realm,
};
pub(crate) use helpers::{current_window_style_viewport, window_host_ptr};
pub(super) use helpers::{window_child_context_handle, window_current_dispatch_scope};
pub(super) use helpers::{
window_child_context_handle, window_current_dispatch_scope,
window_has_discarded_child_browsing_context,
};
pub(super) use interceptors::{
window_indexed_property_deleter, window_indexed_property_descriptor,
window_indexed_property_enumerator, window_indexed_property_getter,
@@ -80,15 +80,9 @@ impl PageScriptHost {
if let Some(entry) = self.child_browsing_contexts.get_mut(&handle) {
entry.clear_current_document_loader_id();
}
let retired_owner = self
.frame_owner_store
.current_child_document_task_owner(handle);
self.remove_child_browsing_context_current_document_storage(handle);
let transition = self.frame_owner_store.detach_current_child_document(handle);
debug_assert_eq!(
transition.and_then(|item| item.retired_owner()),
retired_owner
);
let retired_owner = transition.and_then(|item| item.retired_owner());
self.remove_child_browsing_context_current_document_storage(handle, retired_owner);
transition
}
@@ -135,14 +129,12 @@ impl PageScriptHost {
fn remove_child_browsing_context_current_document_storage(
&mut self,
handle: DomHandle,
retired_owner: Option<crate::frame_owner_model::FrameDocumentTaskOwner>,
) -> Option<DomHandle> {
let document_handle = self
.child_browsing_context_document_handles
.remove(&handle)?;
if let Some(owner) = self
.frame_owner_store
.current_child_document_task_owner(handle)
{
if let Some(owner) = retired_owner {
self.retire_child_document_external_state(handle, owner, document_handle);
return Some(document_handle);
}
@@ -797,18 +797,19 @@ fn child_initial_about_blank_executes_dynamic_inline_script_from_parent_document
const script = document.createElement("script");
script.textContent = `
top.__childDynamicScriptEvents.push(
const ownerWindow = top;
ownerWindow.__childDynamicScriptEvents.push(
"start:" + String(crypto.subtle instanceof SubtleCrypto)
);
const algorithm = { name: "SHA-256" };
Object.defineProperty(algorithm, "name", {
get() {
top.__closeChildDynamicScriptFrame(frameElement);
ownerWindow.__closeChildDynamicScriptFrame(frameElement);
return "SHA-256";
}
});
crypto.subtle.digest(algorithm, new Uint8Array());
top.__childDynamicScriptEvents.push("after-call");
ownerWindow.__childDynamicScriptEvents.push("after-call");
`;
const child = frame.contentDocument;
child.body.appendChild(script);
@@ -3990,3 +3990,99 @@ fn no_src_iframe_initial_about_blank_has_a_quirks_empty_document() {
r#"{"compatMode":"BackCompat","contentType":"text/html","readyState":"complete","documentURI":"about:blank","url":"about:blank","doctypeIsNull":true,"characterSet":"UTF-8","documentChildCount":1,"documentElement":"HTML","documentElementChildCount":2,"head":"HEAD","headChildCount":0,"body":"BODY","bodyChildCount":0}"#
);
}
#[test]
fn removing_iframe_discards_retained_window_relations_synchronously() {
let mut vm = new_storage_test_vm("https://iframe-discard-window-relations.test/");
let result = vm
.eval(
r#"
(() => {
const child = document.createElement("iframe");
(document.body || document.documentElement || document).appendChild(child);
const childWindow = child.contentWindow;
const childDocument = childWindow.document;
const grandchild = childDocument.createElement("iframe");
childDocument.body.appendChild(grandchild);
const grandchildWindow = grandchild.contentWindow;
const grandchildDocument = grandchildWindow.document;
const before = {
childContentWindowIsStable: child.contentWindow === childWindow,
childParentIsTop: childWindow.parent === window,
childTopIsTop: childWindow.top === window,
childFrameElementIsOwner: childWindow.frameElement === child,
grandchildContentWindowIsStable: grandchild.contentWindow === grandchildWindow,
grandchildParentIsChild: grandchildWindow.parent === childWindow,
grandchildTopIsTop: grandchildWindow.top === window,
grandchildFrameElementIsOwner: grandchildWindow.frameElement === grandchild
};
child.parentNode.removeChild(child);
const after = {
childContentWindowIsNull: child.contentWindow === null,
childParentIsNull: childWindow.parent === null,
childTopIsNull: childWindow.top === null,
childFrameElementIsNull: childWindow.frameElement === null,
childDocumentIsRetained: childWindow.document === childDocument,
grandchildContentWindowIsNull: grandchild.contentWindow === null,
grandchildParentIsNull: grandchildWindow.parent === null,
grandchildTopIsNull: grandchildWindow.top === null,
grandchildFrameElementIsNull: grandchildWindow.frameElement === null,
grandchildDocumentIsRetained: grandchildWindow.document === grandchildDocument
};
return JSON.stringify({ before, after });
})()
"#,
)
.expect("iframe discard relation probe should evaluate");
assert_eq!(
result,
r#"{"before":{"childContentWindowIsStable":true,"childParentIsTop":true,"childTopIsTop":true,"childFrameElementIsOwner":true,"grandchildContentWindowIsStable":true,"grandchildParentIsChild":true,"grandchildTopIsTop":true,"grandchildFrameElementIsOwner":true},"after":{"childContentWindowIsNull":true,"childParentIsNull":true,"childTopIsNull":true,"childFrameElementIsNull":true,"childDocumentIsRetained":true,"grandchildContentWindowIsNull":true,"grandchildParentIsNull":true,"grandchildTopIsNull":true,"grandchildFrameElementIsNull":true,"grandchildDocumentIsRetained":true}}"#
);
}
#[test]
fn moving_iframe_into_own_child_document_discards_retained_window_relations() {
let mut vm = new_storage_test_vm("https://iframe-own-child-document.test/");
let result = vm
.eval(
r#"
(() => {
const iframe = document.createElement("iframe");
(document.body || document.documentElement || document).appendChild(iframe);
const childWindow = iframe.contentWindow;
const childDocument = childWindow.document;
const before = {
parentIsTop: childWindow.parent === window,
topIsTop: childWindow.top === window,
frameElementIsOwner: childWindow.frameElement === iframe,
contentWindowIsStable: iframe.contentWindow === childWindow
};
childDocument.body.appendChild(iframe);
const after = {
parentIsNull: childWindow.parent === null,
topIsNull: childWindow.top === null,
frameElementIsNull: childWindow.frameElement === null,
contentWindowIsNull: iframe.contentWindow === null,
documentIsRetained: childWindow.document === childDocument,
movedIntoChildDocument: iframe.ownerDocument === childDocument,
remainsInserted: childDocument.body.firstChild === iframe
};
return JSON.stringify({ before, after });
})()
"#,
)
.expect("self-descendant iframe move probe should evaluate");
assert_eq!(
result,
r#"{"before":{"parentIsTop":true,"topIsTop":true,"frameElementIsOwner":true,"contentWindowIsStable":true},"after":{"parentIsNull":true,"topIsNull":true,"frameElementIsNull":true,"contentWindowIsNull":true,"documentIsRetained":true,"movedIntoChildDocument":true,"remainsInserted":true}}"#
);
}