From ffbb5baef4eb494c121984cd559677056602dd4e Mon Sep 17 00:00:00 2001 From: ldm0 Date: Tue, 22 Sep 2026 18:05:02 +0800 Subject: [PATCH] fix(history): restore historical srcdoc documents Retain srcdoc resources by history Document identity, including the original source, base URL, policy and origin. Restore each entry URL separately and apply the current iframe sandbox flags when reconstructing the Document. Cover top-level and popup child traversal, classic and Navigation API state, and sandbox script/origin changes. --- .../fixtures/iframe-srcdoc-sandbox-history.js | 29 +++++ .../tests/fixtures/iframe-srcdoc-traversal.js | 79 +++++++++++++ moli-core/tests/history_iframe.rs | 109 ++++++++++++++++++ .../context_host/child_documents/commit.rs | 5 + .../context_host/child_documents/snapshots.rs | 26 +++-- .../child_frame_navigation/commands.rs | 12 +- .../context_host/child_frames.rs | 11 +- .../context_host/child_frames/registry.rs | 4 + .../child_frames/request_scope.rs | 3 + .../child_frames/srcdoc_history.rs | 102 ++++++++++++++++ .../context_host/window_security_tokens.rs | 3 + 11 files changed, 368 insertions(+), 15 deletions(-) create mode 100644 moli-core/tests/fixtures/iframe-srcdoc-sandbox-history.js create mode 100644 moli-core/tests/fixtures/iframe-srcdoc-traversal.js create mode 100644 moli-renderer-v8/src/native_bridge/context_host/child_frames/srcdoc_history.rs diff --git a/moli-core/tests/fixtures/iframe-srcdoc-sandbox-history.js b/moli-core/tests/fixtures/iframe-srcdoc-sandbox-history.js new file mode 100644 index 00000000000..cb5b0c17118 --- /dev/null +++ b/moli-core/tests/fixtures/iframe-srcdoc-sandbox-history.js @@ -0,0 +1,29 @@ +async function iframeSrcdocSandboxHistory(before, after, ordinaryURL) { + const frame = document.createElement('iframe'); + const loaded = () => new Promise(resolve => frame.addEventListener('load', resolve, {once: true})); + const settle = () => new Promise(resolve => setTimeout(resolve, 0)); + const messages = []; + const receive = event => { if (event.source === frame.contentWindow) messages.push(event.data); }; + addEventListener('message', receive); + const navigate = async action => { + const ready = loaded(); + action(); + await ready; + await settle(); + }; + try { + frame.setAttribute('sandbox', before); + await navigate(() => { frame.src = ordinaryURL; document.body.append(frame); }); + await navigate(() => frame.srcdoc = '

historical sandbox source

' + + '"# + ); + let mut url = Url::parse(&server.url("/compat/child-dynamic-markup-document"))?; + url.query_pairs_mut().append_pair("markup", &markup); + let result = tokio::time::timeout(Duration::from_secs(10), async { + let mut page = browser.fetch(url.as_str()).await?; + page.evaluate_runtime_expression_with_await_async( + "finished.then(value => JSON.stringify(value))", + true, + ) + .await + }) + .await??; + let result: Value = + serde_json::from_str(result["value"].as_str().expect("sandbox history result"))?; + let messages = if scripts { + json!([{"origin": if accessible { url.origin().ascii_serialization() } else { "null".to_owned() }, + "text": "historical sandbox source"}]) + } else { + json!([]) + }; + assert_eq!( + result, + json!({"messages": messages, "accessible": accessible}), + "{before} -> {after}" + ); + } + server.shutdown().await; + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn iframe_srcdoc_traversal_restores_each_historical_document_resource() -> Result<()> { + let fixture = include_str!("fixtures/iframe-srcdoc-traversal.js"); + let server = FixtureServer::spawn().await?; + for mode in ["top", "popup"] { + for api in ["property", "setAttribute"] { + for route in ["fragment", "url"] { + let browser = Browser::new(BrowserConfig::default())?; + let markup = format!( + r#""# + ); + let mut url = Url::parse(&server.url("/compat/child-dynamic-markup-document"))?; + url.query_pairs_mut().append_pair("markup", &markup); + let result = tokio::time::timeout(Duration::from_secs(10), async { + let mut page = browser.fetch(url.as_str()).await?; + page.evaluate_runtime_expression_with_await_async( + "finished.then(value => JSON.stringify(value))", + true, + ) + .await + }) + .await??; + assert_eq!( + result["value"], + json!(r#"["back-middle","back-old","forward-new","child-back-old"]"#), + "{mode}/{api}/{route}: {result}" + ); + } + } + } + server.shutdown().await; + Ok(()) +} + async fn iframe_attribute_history(phase: &str, api: &str, attribute: &str) -> Result { let child = r#"