From 1b2ebf2998b6c5e741c331e42ddefdb004e9dc75 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Thu, 3 Sep 2026 19:49:10 +0800 Subject: [PATCH] fix(iframe): discard child window relations synchronously --- .../src/context_bootstrap/runtime_state.rs | 8 ++ .../src/context_bootstrap/window_accessors.rs | 5 +- .../context_host/child_frames/registry.rs | 16 +--- .../tests/browser_api/crypto_misc.rs | 7 +- .../extracted/frame_navigation_and_history.rs | 96 +++++++++++++++++++ 5 files changed, 116 insertions(+), 16 deletions(-) diff --git a/moli-renderer-v8/src/context_bootstrap/runtime_state.rs b/moli-renderer-v8/src/context_bootstrap/runtime_state.rs index 2e95d96fee8..36cdad77eb4 100644 --- a/moli-renderer-v8/src/context_bootstrap/runtime_state.rs +++ b/moli-renderer-v8/src/context_bootstrap/runtime_state.rs @@ -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); } diff --git a/moli-renderer-v8/src/context_bootstrap/window_accessors.rs b/moli-renderer-v8/src/context_bootstrap/window_accessors.rs index 9ff65244a32..c95475645b8 100644 --- a/moli-renderer-v8/src/context_bootstrap/window_accessors.rs +++ b/moli-renderer-v8/src/context_bootstrap/window_accessors.rs @@ -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, diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs index 5ff199c36e6..c3cf9eb22ca 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs @@ -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, ) -> Option { 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); } diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/crypto_misc.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/crypto_misc.rs index 0cbb9e99228..56a4e05a495 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/crypto_misc.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/crypto_misc.rs @@ -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); diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs index 1017e2f5bc9..f3788b9e725 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs @@ -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}}"# + ); +}