From 67f195cbefb89cd60bc8255ee34fc184397b3df8 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Sun, 27 Sep 2026 10:20:20 +0800 Subject: [PATCH] fix(navigation): inherit document encoding from eligible containers Capture the actual container Document's encoding and origin for child navigation. Apply the same-origin and UTF-16 restrictions using the final response URL and sandbox flags, including response CSP. Decode local Blob and data navigations from response bytes. Keep popup fallback independent of its opener and use UTF-8 for srcdoc snapshots. Exercise nested frames, redirects, sandboxing, encoding precedence and local URLs over real HTTP. Correct the existing MIME fixture's popup fallback expectation while retaining all 64 iframe and popup cases. --- moli-core/tests/fetch_behaviors.rs | 2 + .../fetch_behaviors/document_encoding.rs | 137 ++++++++++++++++++ .../fixtures/document-encoding-inheritance.js | 69 +++++++++ .../fixtures/document-encoding-parent.js | 38 +++++ .../tests/fixtures/document-response-mime.js | 30 ++-- .../context_host/child_documents.rs | 1 + .../context_host/child_documents/encoding.rs | 67 +++++++++ .../context_host/child_documents/loads.rs | 21 ++- .../context_host/child_documents/snapshots.rs | 75 +++++----- .../context_host/child_frame_snapshots.rs | 9 +- .../child_frame_snapshots/detached.rs | 12 +- .../child_frame_snapshots/markup.rs | 6 +- .../src/native_bridge/context_host/popups.rs | 21 +-- 13 files changed, 397 insertions(+), 91 deletions(-) create mode 100644 moli-core/tests/fetch_behaviors/document_encoding.rs create mode 100644 moli-core/tests/fixtures/document-encoding-inheritance.js create mode 100644 moli-core/tests/fixtures/document-encoding-parent.js create mode 100644 moli-renderer-v8/src/native_bridge/context_host/child_documents/encoding.rs diff --git a/moli-core/tests/fetch_behaviors.rs b/moli-core/tests/fetch_behaviors.rs index ab6e690dd1b..7f7c62be64f 100644 --- a/moli-core/tests/fetch_behaviors.rs +++ b/moli-core/tests/fetch_behaviors.rs @@ -12,6 +12,8 @@ use tokio::time::Duration; #[path = "fetch_behaviors/cache_timing.rs"] mod cache_timing; +#[path = "fetch_behaviors/document_encoding.rs"] +mod document_encoding; #[path = "fetch_behaviors/document_mime.rs"] mod document_mime; #[path = "fetch_behaviors/preload_as.rs"] diff --git a/moli-core/tests/fetch_behaviors/document_encoding.rs b/moli-core/tests/fetch_behaviors/document_encoding.rs new file mode 100644 index 00000000000..fa15223c595 --- /dev/null +++ b/moli-core/tests/fetch_behaviors/document_encoding.rs @@ -0,0 +1,137 @@ +use super::*; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; + +async fn serve(mut socket: tokio::net::TcpStream) -> Result<()> { + let mut request = Vec::new(); + while !request.windows(4).any(|bytes| bytes == b"\r\n\r\n") { + let mut buffer = [0; 4096]; + let count = socket.read(&mut buffer).await?; + if count == 0 { + return Ok(()); + } + request.extend_from_slice(&buffer[..count]); + } + let request = String::from_utf8(request)?; + let path = request + .split_whitespace() + .nth(1) + .context("request target")?; + let url = url::Url::parse(&format!("http://fixture{path}"))?; + let parameter = |name: &str| { + url.query_pairs() + .find(|(key, _)| key == name) + .map(|(_, value)| value.into_owned()) + }; + let mut extra = String::new(); + let (mime, body) = match url.path() { + "/encoding-parent" => { + let charset = parameter("charset").unwrap_or_else(|| "windows-1253".to_owned()); + let source = ""; + let body = match charset.as_str() { + "UTF-16LE" => source.encode_utf16().flat_map(u16::to_le_bytes).collect(), + "UTF-16BE" => source.encode_utf16().flat_map(u16::to_be_bytes).collect(), + _ => source.as_bytes().to_vec(), + }; + (format!("text/html;charset={charset}"), body) + } + "/encoding-parent.js" => ( + "text/javascript;charset=utf-8".to_owned(), + include_bytes!("../fixtures/document-encoding-parent.js").to_vec(), + ), + "/encoding-child" => { + let mime = parameter("header").map_or_else( + || "text/html".to_owned(), + |charset| format!("text/html;charset={charset}"), + ); + let meta = parameter("meta") + .map_or_else(String::new, |charset| format!("")); + let token = serde_json::to_string(¶meter("token").unwrap_or_default())?; + let report = format!( + "" + ); + let mut body = format!("{meta}

").into_bytes(); + body.push(0xa2); + body.extend_from_slice(format!("

{report}").as_bytes()); + if parameter("bom").as_deref() == Some("utf8") { + body.splice(0..0, [0xef, 0xbb, 0xbf]); + } + if let Some(csp) = parameter("csp") { + extra.push_str(&format!("Content-Security-Policy: {csp}\r\n")); + } + (mime, body) + } + "/redirect" => { + extra.push_str(&format!( + "Location: {}\r\n", + parameter("to").context("redirect target")? + )); + ("text/html".to_owned(), Vec::new()) + } + _ => ( + "text/html;charset=utf-8".to_owned(), + b"root".to_vec(), + ), + }; + let status = if url.path() == "/redirect" { + "302 Found" + } else { + "200 OK" + }; + let head = format!( + "HTTP/1.1 {status}\r\nContent-Type: {mime}\r\nContent-Length: {}\r\nCache-Control: no-store\r\n{extra}Connection: close\r\n\r\n", + body.len() + ); + socket.write_all(head.as_bytes()).await?; + socket.write_all(&body).await?; + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn document_encoding_inherits_only_from_the_eligible_container() -> Result<()> { + let local = tokio::net::TcpListener::bind("127.0.0.1:0").await?; + let remote = tokio::net::TcpListener::bind("127.0.0.1:0").await?; + let origins = [ + format!("http://{}", local.local_addr()?), + format!("http://{}", remote.local_addr()?), + ]; + let (shutdown, mut shutting_down) = tokio::sync::oneshot::channel::<()>(); + let server = tokio::spawn(async move { + let mut requests = tokio::task::JoinSet::new(); + loop { + tokio::select! { + _ = &mut shutting_down => break, + accepted = async { tokio::select! { + accepted = local.accept() => accepted, + accepted = remote.accept() => accepted, + }} => { + let (socket, _) = accepted?; + requests.spawn(serve(socket)); + } + Some(result) = requests.join_next(), if !requests.is_empty() => { result??; } + } + } + requests.shutdown().await; + Ok::<_, anyhow::Error>(()) + }); + let result = tokio::time::timeout(Duration::from_secs(30), async { + let browser = Browser::new(AppConfig::default())?; + let mut page = browser.fetch(&origins[0]).await?; + let script = format!( + "(globalThis.encodingOrigins={}, {})", + serde_json::to_string(&origins)?, + include_str!("../fixtures/document-encoding-inheritance.js") + ); + let result = page + .evaluate_runtime_expression_with_await_async(&script, true) + .await?; + let observed: serde_json::Value = + serde_json::from_str(result["value"].as_str().context("encoding result")?)?; + assert_eq!(observed["errors"], serde_json::json!([])); + assert_eq!(observed["observations"].as_array().unwrap().len(), 27); + Ok::<_, anyhow::Error>(()) + }) + .await; + let _ = shutdown.send(()); + server.await??; + result? +} diff --git a/moli-core/tests/fixtures/document-encoding-inheritance.js b/moli-core/tests/fixtures/document-encoding-inheritance.js new file mode 100644 index 00000000000..cdb27017b1c --- /dev/null +++ b/moli-core/tests/fixtures/document-encoding-inheritance.js @@ -0,0 +1,69 @@ +(async () => { + const [origin, other] = globalThis.encodingOrigins || globalThis.__fetchOrigins; + const cases = [ + ['same-origin container', {}, 'windows-1253', 'Ά'], + ['different nested encoding', {charset: 'windows-1251'}, 'windows-1251', 'ў'], + ['cross-origin child', {childOrigin: other}, 'windows-1252', '¢'], + ['cross-origin parent with same-origin child', {parentOrigin: other, childOrigin: other}, 'windows-1253', 'Ά'], + ['cross-origin parent with top-origin child', {parentOrigin: other, childOrigin: origin}, 'windows-1252', '¢'], + ['redirect to other origin', {childOrigin: other, redirectOrigin: origin}, 'windows-1252', '¢'], + ['redirect to parent origin', {redirectOrigin: other}, 'windows-1253', 'Ά'], + ['opaque child', {sandbox: 'allow-scripts'}, 'windows-1252', '¢'], + ['same-origin sandbox child', {sandbox: 'allow-scripts allow-same-origin'}, 'windows-1253', 'Ά'], + ['CSP opaque child', {csp: 'sandbox allow-scripts'}, 'windows-1252', '¢'], + ['opaque parent', {parentSandbox: 'allow-scripts'}, 'windows-1252', '¢'], + ['UTF-16LE parent', {charset: 'UTF-16LE'}, 'windows-1252', '¢'], + ['UTF-16BE parent', {charset: 'UTF-16BE'}, 'windows-1252', '¢'], + ['transport wins', {header: 'windows-1251'}, 'windows-1251', 'ў'], + ['meta wins', {meta: 'windows-1251'}, 'windows-1251', 'ў'], + ['invalid transport inherits', {header: 'not-an-encoding'}, 'windows-1253', 'Ά'], + ['cross-origin invalid transport', {header: 'not-an-encoding', childOrigin: other}, 'windows-1252', '¢'], + ['BOM wins', {bom: 'utf8'}, 'UTF-8', '�'], + ['data URL is opaque', {mode: 'data'}, 'windows-1252', '¢'], + ['blob URL shares parent origin', {mode: 'blob'}, 'windows-1253', 'Ά'], + ['sandboxed blob', {mode: 'blob', sandbox: 'allow-scripts'}, 'windows-1252', '¢'], + ['srcdoc uses UTF-8', {mode: 'srcdoc'}, 'UTF-8', '¢'], + ['sandboxed srcdoc uses UTF-8', {mode: 'srcdoc', sandbox: 'allow-scripts'}, 'UTF-8', '¢'], + ['srcdoc from UTF-16 parent uses UTF-8', {mode: 'srcdoc', charset: 'UTF-16LE'}, 'UTF-8', '¢'], + ['popup has no container', {popup: '1'}, 'windows-1252', '¢'], + ['popup transport wins', {popup: '1', header: 'windows-1251'}, 'windows-1251', 'ў'], + ['popup blob has no container', {popup: '1', mode: 'blob'}, 'windows-1252', '¢'], + ]; + const errors = [], observations = []; + for (const [name, options, charset, text] of cases) { + const token = 'encoding-' + observations.length; + const frame = document.createElement('iframe'); + const url = new URL('/encoding-parent', options.parentOrigin || origin); + url.searchParams.set('token', token); + url.searchParams.set('charset', options.charset || 'windows-1253'); + for (const [key, value] of Object.entries(options)) url.searchParams.set(key, value); + if (options.parentSandbox) frame.setAttribute('sandbox', options.parentSandbox); + let listener, timer, parent; + try { + const observed = await new Promise((resolve, reject) => { + listener = event => { + if (event.data && event.data.token === token + ':parent') parent = event.data.charset; + if (event.data && event.data.token === token) resolve(event.data); + }; + addEventListener('message', listener); + timer = setTimeout(() => reject(new Error('child did not report')), 5000); + frame.src = url.href; + document.body.append(frame); + }); + observations.push({name, parent, observed}); + if (parent !== (options.charset || 'windows-1253') || observed.charset !== charset || observed.text !== text) { + errors.push({name, parent, observed, expected: {charset, text}}); + } + } catch (error) { + observations.push({name, parent, error: String(error)}); + errors.push({name, parent, error: String(error)}); + } finally { + clearTimeout(timer); + removeEventListener('message', listener); + if (frame.contentWindow) frame.contentWindow.postMessage('cleanup', '*'); + await new Promise(resolve => setTimeout(resolve, 0)); + frame.remove(); + } + } + return JSON.stringify({errors, observations}); +})() diff --git a/moli-core/tests/fixtures/document-encoding-parent.js b/moli-core/tests/fixtures/document-encoding-parent.js new file mode 100644 index 00000000000..09fa31fd286 --- /dev/null +++ b/moli-core/tests/fixtures/document-encoding-parent.js @@ -0,0 +1,38 @@ +(() => { + const q = new URLSearchParams(location.search); + const token = q.get('token'); + addEventListener('error', event => top.postMessage({token, error: event.message}, '*')); + top.postMessage({token: token + ':parent', charset: document.characterSet}, '*'); + const child = new URL('/encoding-child', q.get('childOrigin') || location.href); + for (const name of ['token', 'header', 'meta', 'csp', 'bom']) { + if (q.has(name)) child.searchParams.set(name, q.get(name)); + } + const report = ''; + const markup = '

\xa2

' + report; + let target = child.href; + if (q.has('redirectOrigin')) { + target = q.get('redirectOrigin') + '/redirect?to=' + encodeURIComponent(target); + } + const mode = q.get('mode'); + if (mode === 'data') { + target = 'data:text/html,' + Array.from(markup, c => '%' + c.charCodeAt(0).toString(16).padStart(2, '0')).join(''); + } else if (mode === 'blob') { + target = URL.createObjectURL(new Blob([Uint8Array.from(markup, c => c.charCodeAt(0))], {type: 'text/html'})); + } + if (q.has('popup')) { + const popup = open(target, '_blank'); + addEventListener('message', event => { if (event.data === 'cleanup') popup.close(); }); + } else { + const frame = document.createElement('iframe'); + if (q.has('sandbox')) frame.setAttribute('sandbox', q.get('sandbox')); + if (mode === 'srcdoc') frame.srcdoc = markup; + else frame.src = target; + document.body.append(frame); + } + addEventListener('message', event => { + if (event.data === 'cleanup' && mode === 'blob') URL.revokeObjectURL(target); + }); +})() diff --git a/moli-core/tests/fixtures/document-response-mime.js b/moli-core/tests/fixtures/document-response-mime.js index 73fe791a0a5..23597b50218 100644 --- a/moli-core/tests/fixtures/document-response-mime.js +++ b/moli-core/tests/fixtures/document-response-mime.js @@ -1,21 +1,23 @@ (async () => { + // A missing/invalid charset inherits from an iframe's UTF-8 container; + // a top-level popup uses the browser default instead. const cases = [ - [['', 'text/plain'], 'text/plain', 'UTF-8', 'hi'], - [['text/plain', ''], 'text/plain', 'UTF-8', 'hi'], - [['text/html', 'text/plain'], 'text/plain', 'UTF-8', 'hi'], - [['text/plain;charset=gbk', 'text/html'], 'text/html', 'UTF-8', 'hi'], + [['', 'text/plain'], 'text/plain', null, 'hi'], + [['text/plain', ''], 'text/plain', null, 'hi'], + [['text/html', 'text/plain'], 'text/plain', null, 'hi'], + [['text/plain;charset=gbk', 'text/html'], 'text/html', null, 'hi'], [['text/plain;charset=gbk', 'text/html;charset=windows-1254'], 'text/html', 'windows-1254', 'Ğ'], [['text/plain;charset=gbk', 'text/plain'], 'text/plain', 'GBK', '家居'], [['text/html;charset=gbk', 'text/html;charset=utf-8', 'text/html'], 'text/html', 'GBK', '家居'], - [['text/html;charset=gbk', 'text/plain', 'text/html'], 'text/html', 'UTF-8', 'hi'], - [['text/plain', '*/*;charset=gbk'], 'text/plain', 'UTF-8', 'hi'], - [['text/html', '*/*'], 'text/html', 'UTF-8', 'hi'], - [['text/html;x="', 'text/plain'], 'text/html', 'UTF-8', 'hi'], + [['text/html;charset=gbk', 'text/plain', 'text/html'], 'text/html', null, 'hi'], + [['text/plain', '*/*;charset=gbk'], 'text/plain', null, 'hi'], + [['text/html', '*/*'], 'text/html', null, 'hi'], + [['text/html;x="', 'text/plain'], 'text/html', null, 'hi'], [['text/html;"', '\\"', 'text/plain', '";charset=GBK'], 'text/html', 'GBK', '家居'], - [['text/html;"', '"', 'text/plain'], 'text/plain', 'UTF-8', 'hi'], - [['application/octet-stream', 'text/html', 'invalid'], 'text/html', 'UTF-8', 'hi'], - [['text/html;charset=gbk', 'text/html;charset=unknown'], 'text/html', 'UTF-8', 'hi'], - [['text/html;charset=gbk', 'text/html;charset=""'], 'text/html', 'UTF-8', 'hi'], + [['text/html;"', '"', 'text/plain'], 'text/plain', null, 'hi'], + [['application/octet-stream', 'text/html', 'invalid'], 'text/html', null, 'hi'], + [['text/html;charset=gbk', 'text/html;charset=unknown'], 'text/html', null, 'hi'], + [['text/html;charset=gbk', 'text/html;charset=""'], 'text/html', null, 'hi'], ]; const errors = []; let checked = 0; @@ -24,7 +26,7 @@ for (const fields of [values, [values.join(',')]]) { const url = new URL('/mime', location.href); for (const field of fields) url.searchParams.append('value', field); - url.searchParams.set('encoding', charset.toLowerCase()); + url.searchParams.set('encoding', (charset || 'UTF-8').toLowerCase()); let frame, popup; try { const doc = await new Promise((resolve, reject) => { @@ -45,7 +47,7 @@ }); const observed = [doc.contentType, doc.characterSet.toUpperCase(), doc.body.firstChild.localName, doc.body.textContent]; - const expected = [type, charset.toUpperCase(), type === 'text/plain' ? 'pre' : 'b', + const expected = [type, (charset || (kind === 'iframe' ? 'UTF-8' : 'windows-1252')).toUpperCase(), type === 'text/plain' ? 'pre' : 'b', type === 'text/plain' ? `${text}\n` : `${text}\n`]; if (JSON.stringify(observed) !== JSON.stringify(expected)) { errors.push({kind, fields, observed, expected}); diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_documents.rs b/moli-renderer-v8/src/native_bridge/context_host/child_documents.rs index efeeb34dbf5..055717ec1f8 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_documents.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_documents.rs @@ -1,6 +1,7 @@ mod async_policy; mod bootstrap_commit; mod commit; +mod encoding; mod frame_owner_resource_timing; mod initial_empty; mod lifecycle; diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_documents/encoding.rs b/moli-renderer-v8/src/native_bridge/context_host/child_documents/encoding.rs new file mode 100644 index 00000000000..527ef0c8410 --- /dev/null +++ b/moli-renderer-v8/src/native_bridge/context_host/child_documents/encoding.rs @@ -0,0 +1,67 @@ +use super::super::PageScriptHost; +use crate::document_runtime::DomHandle; +use moli_url::WebOrigin; +use url::Url; + +/// The container Document's encoding and origin, captured for this navigation. +/// An opener is not a container, and a response can introduce another origin +/// through a redirect or CSP sandboxing. +#[derive(Default)] +pub(super) struct ChildDocumentEncodingContext { + parent: Option<(WebOrigin, String)>, + sandbox_forces_opaque_origin: bool, +} + +impl ChildDocumentEncodingContext { + pub(super) fn fallback_encoding( + &self, + response_url: &Url, + content_type: Option<&str>, + response_forces_opaque_origin: bool, + ) -> Option<&str> { + if content_type.is_some_and(moli_web_mime::is_dom_parser_xml_mime) { + return Some("UTF-8"); + } + if self.sandbox_forces_opaque_origin || response_forces_opaque_origin { + return None; + } + let (origin, charset) = self.parent.as_ref()?; + if !origin.same_origin_url(response_url) { + return None; + } + let encoding = moli_encoding::encoding_for_label(charset)?; + // HTML permits inheritance from the same-origin container except for + // UTF-16, which would reinterpret the child's markup byte structure. + (!matches!(encoding.name(), "UTF-16LE" | "UTF-16BE")).then_some(charset.as_str()) + } +} + +impl PageScriptHost { + pub(super) fn child_document_container_character_set(&self, handle: DomHandle) -> Option<&str> { + let parent = self.dom_host().node(handle)?.owner_document()?; + // SAFETY: PageScriptHost is owned by the ScriptVm that owns this DocumentRuntime. + unsafe { &*self.document_runtime }.document_character_set_for_handle(parent) + } + + pub(super) fn child_document_encoding_context( + &self, + handle: DomHandle, + ) -> ChildDocumentEncodingContext { + let parent = self + .owner_dispatch_scope_for_node(handle) + .and_then(|owner| self.window_access_origin_for_dispatch_scope(owner)) + .zip(self.child_document_container_character_set(handle)) + .map(|(origin, charset)| { + ( + WebOrigin::from_serialized(&origin.serialized_origin()), + charset.to_owned(), + ) + }); + ChildDocumentEncodingContext { + parent, + sandbox_forces_opaque_origin: self + .child_browsing_context_sandbox_policy_from_owner(handle) + .forces_opaque_origin, + } + } +} diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_documents/loads.rs b/moli-renderer-v8/src/native_bridge/context_host/child_documents/loads.rs index b95afd1df02..6ea54ff1f1f 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_documents/loads.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_documents/loads.rs @@ -260,7 +260,7 @@ impl PageScriptHost { browser_context_runtime, resource_loader, completion_tx, - parent_character_set: self.document_character_set().to_owned(), + encoding_context: self.child_document_encoding_context(handle), }; let paused_fetch = self.start_child_document_fetch(handle, initiating_owner, &initiating_loader, fetch); @@ -1129,7 +1129,7 @@ pub(crate) struct ChildDocumentFetch { browser_context_runtime: crate::runtime::RendererBrowserContextRuntime, resource_loader: crate::network::navigation::NavigationResourceLoader, completion_tx: crate::page_task_queue::RendererResourceCompletionSender, - parent_character_set: String, + encoding_context: super::encoding::ChildDocumentEncodingContext, } impl std::fmt::Debug for ChildDocumentFetch { @@ -1460,7 +1460,7 @@ impl ChildDocumentFetch { head, body, navigation_url, - &self.parent_character_set, + &self.encoding_context, self.download_policy, self.response_body.take(), resource_timing_status, @@ -1526,7 +1526,7 @@ fn child_document_load_outcome_from_response( head: moli_fetch::ResponseHead, body: moli_fetch::ResponseBody, navigation_url: url::Url, - parent_character_set: &str, + encoding_context: &super::encoding::ChildDocumentEncodingContext, download_policy: ChildNavigationDownloadPolicy, download_body: Option, resource_timing_status: Option, @@ -1596,6 +1596,13 @@ fn child_document_load_outcome_from_response( } let content_type = child_document_content_type_from_headers(&head.headers) .or_else(|| child_document_content_type_for_url(&navigation_url)); + let policy_container = + DocumentPolicyContainer::from_navigation_response_headers(&head.headers, &navigation_url); + let fallback = encoding_context.fallback_encoding( + &navigation_url, + content_type.as_deref(), + policy_container.sandbox.forces_opaque_origin, + ); let (markup, character_set) = { let body_bytes = response_body .try_bytes() @@ -1605,7 +1612,7 @@ fn child_document_load_outcome_from_response( &head.headers, content_type.as_deref(), &navigation_url, - Some(parent_character_set), + fallback, ); (markup, character_set.to_owned()) }; @@ -1747,7 +1754,7 @@ mod tests { browser_context_runtime: host.browser_context_runtime(), resource_loader: resource_loader.clone(), completion_tx: completions.sender(), - parent_character_set: "UTF-8".to_owned(), + encoding_context: Default::default(), }; assert_eq!( host.start_child_document_fetch( @@ -1797,7 +1804,7 @@ mod tests { }, moli_fetch::ResponseBody::materialized_bytes(body_bytes.clone()), url::Url::parse("https://example.test/child").unwrap(), - "UTF-8", + &super::super::encoding::ChildDocumentEncodingContext::default(), ChildNavigationDownloadPolicy { source_allows: true, target_allows: true, diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_documents/snapshots.rs b/moli-renderer-v8/src/native_bridge/context_host/child_documents/snapshots.rs index 0092510312a..97997f11347 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_documents/snapshots.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_documents/snapshots.rs @@ -26,9 +26,12 @@ impl PageScriptHost { handle: DomHandle, url: &Url, ) -> Option { - let snapshot = self.apply_page_csp_bypass_to_child_snapshot( - self.materialize_local_child_snapshot_for_url(url)?, - ); + let mut snapshot = self.materialize_local_child_snapshot_for_url(url, Some(handle))?; + if ChildBrowsingContextBootstrap::Url(url.clone()).content_security_policy_inherited() { + snapshot.policy_container = + self.initial_child_about_blank_policy_container_from_parent(handle); + } + let snapshot = self.apply_page_csp_bypass_to_child_snapshot(snapshot); if moli_url::is_about_blank(&snapshot.url) { Some(snapshot.with_fallback_base_url(self.document_base_url_for_child_context(handle))) } else { @@ -39,6 +42,7 @@ impl PageScriptHost { pub(crate) fn materialize_local_child_snapshot_for_url( &self, url: &Url, + owner: Option, ) -> Option { match url.scheme() { "about" if url.path() == "blank" => Some(ChildBrowsingContextSnapshot::html( @@ -46,41 +50,44 @@ impl PageScriptHost { "".into(), )), "http" | "https" => None, - "blob" => { - let (body, mime_type) = crate::blob::object_url_body_and_type(url.as_str())?; - if !mime_type.is_empty() && !is_html_document_mime(&mime_type) { - return None; - } - let character_set = self.document_character_set().to_owned(); - Some(ChildBrowsingContextSnapshot::with_character_set( - url.clone(), - body, - child_document_content_type_from_header_value(&mime_type) - .or_else(|| Some("text/html".to_owned())), - character_set, - )) - } - "data" => crate::network_host::local_url_response(url).map(|response| { + "blob" | "data" => crate::network_host::local_url_response(url).and_then(|response| { let head = response.head(); let content_type = child_document_content_type_from_headers(&head.headers); + if url.scheme() == "blob" + && content_type + .as_deref() + .is_some_and(|mime| !is_html_document_mime(mime)) + { + return None; + } + let policy_container = DocumentPolicyContainer::from_navigation_response_headers( + &head.headers, + &head.final_url, + ); + let encoding_context = owner + .map(|handle| self.child_document_encoding_context(handle)) + .unwrap_or_default(); + let fallback = encoding_context.fallback_encoding( + &head.final_url, + content_type.as_deref(), + policy_container.sandbox.forces_opaque_origin, + ); let (body, character_set) = decode_document_response( response.body_bytes(), &head.headers, content_type.as_deref(), &head.final_url, - Some(self.document_character_set()), + fallback, ); - let policy_container = DocumentPolicyContainer::from_navigation_response_headers( - &head.headers, - &head.final_url, - ); - ChildBrowsingContextSnapshot::with_character_set( - head.final_url, - body, - content_type, - character_set, + Some( + ChildBrowsingContextSnapshot::with_character_set( + head.final_url, + body, + content_type, + character_set, + ) + .with_policy_container(policy_container), ) - .with_policy_container(policy_container) }), _ => None, } @@ -113,11 +120,7 @@ impl PageScriptHost { return Some(self.apply_page_csp_bypass_to_child_snapshot(snapshot)); } Some(self.apply_page_csp_bypass_to_child_snapshot( - ChildBrowsingContextSnapshot::srcdoc( - base_url.clone(), - markup.clone(), - self.document_character_set().to_owned(), - ), + ChildBrowsingContextSnapshot::srcdoc(base_url.clone(), markup.clone()), )) } ChildBrowsingContextBootstrap::Url(url) => { @@ -134,10 +137,6 @@ pub(in crate::native_bridge::context_host) fn child_document_content_type_from_h response_document_content_type(headers) } -fn child_document_content_type_from_header_value(value: &str) -> Option { - moli_web_mime::mime_essence(value) -} - #[cfg(test)] pub(in crate::native_bridge::context_host) fn child_document_content_type_for_url( url: &Url, diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots.rs index 9176892bd07..3bdc9eb29af 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots.rs @@ -85,16 +85,13 @@ impl ChildBrowsingContextSnapshot { .with_fallback_base_url(fallback_base_url) } - pub(crate) fn srcdoc( - fallback_base_url: Url, - markup: String, - character_set: impl Into, - ) -> Self { + pub(crate) fn srcdoc(fallback_base_url: Url, markup: String) -> Self { + // srcdoc supplies Unicode markup, serialized as UTF-8 for navigation. Self::with_character_set( Url::parse("about:srcdoc").expect("static about:srcdoc should parse"), markup, Some("text/html".to_owned()), - character_set, + "UTF-8", ) .with_fallback_base_url(fallback_base_url) } diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/detached.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/detached.rs index 8cb84924d14..edb1c177686 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/detached.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/detached.rs @@ -374,15 +374,11 @@ impl PageScriptHost { ChildBrowsingContextBootstrap::AboutBlank => Some( ChildBrowsingContextSnapshot::about_blank(document_base_url.clone()), ), - ChildBrowsingContextBootstrap::Srcdoc { base_url, markup } => { - Some(ChildBrowsingContextSnapshot::srcdoc( - base_url.clone(), - markup.clone(), - self.document_character_set().to_owned(), - )) - } + ChildBrowsingContextBootstrap::Srcdoc { base_url, markup } => Some( + ChildBrowsingContextSnapshot::srcdoc(base_url.clone(), markup.clone()), + ), ChildBrowsingContextBootstrap::Url(url) => self - .materialize_local_child_snapshot_for_url(url) + .materialize_local_child_snapshot_for_url(url, None) .map(|snapshot| self.apply_page_csp_bypass_to_child_snapshot(snapshot)), ChildBrowsingContextBootstrap::Request(_) => None, } diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/markup.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/markup.rs index b7b019d1d6f..190bf15201d 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/markup.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/markup.rs @@ -30,11 +30,7 @@ impl PageScriptHost { )) } ChildBrowsingContextBootstrap::Srcdoc { base_url, markup } => { - Some(ChildBrowsingContextSnapshot::srcdoc( - base_url, - markup, - self.document_character_set().to_owned(), - )) + Some(ChildBrowsingContextSnapshot::srcdoc(base_url, markup)) } ChildBrowsingContextBootstrap::Url(url) => { if self.child_document_load_is_pending(handle) { diff --git a/moli-renderer-v8/src/native_bridge/context_host/popups.rs b/moli-renderer-v8/src/native_bridge/context_host/popups.rs index 9bdd88f230d..84fb435f5a2 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/popups.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/popups.rs @@ -2,6 +2,8 @@ mod renderer_owned; use super::{NavigationHistoryEntrySeed, PageScriptHost}; use crate::content_security_policy::ContentSecurityPolicy; +use crate::document_response_decoder::decode_document_response; +use crate::web_api_interfaces; use crate::{ content_security_policy::content_security_policy_forces_opaque_origin, context_bootstrap::{ @@ -48,7 +50,6 @@ use crate::{ }, }; use anyhow::Result; -use moli_encoding::decode_html_document_with_fallback; use moli_fetch::Request; use moli_storage_key::{MoliStorageKey, StoragePartitionRelation, site_for_url}; use moli_url::origin_ascii_serialization; @@ -3032,7 +3033,7 @@ impl PageScriptHost { self.next_lightweight_popup_document_load_id.wrapping_add(1); let target = LightweightPopupDocumentFetchTarget::new(load_id, task); let local_snapshot = (request.method == "GET") - .then(|| self.materialize_local_child_snapshot_for_url(&target_url)) + .then(|| self.materialize_local_child_snapshot_for_url(&target_url, None)) .flatten(); let (resource_loader, request_origin) = if local_snapshot.is_none() { let source_owner = self.current_lightweight_popup_document_owner(popup_id)?; @@ -3098,7 +3099,6 @@ impl PageScriptHost { let resource_loader = resource_loader.expect("remote popup navigation requires its captured loader"); let completion_tx = self.resource_completion_tx.clone(); - let opener_character_set = self.document_character_set().to_owned(); let task_resource_loader = resource_loader.clone(); resource_loader.spawn_resource_task(async move { use anyhow::Context as _; @@ -3135,18 +3135,13 @@ impl PageScriptHost { )?; let content_type = super::child_documents::child_document_content_type_from_headers(&head.headers); - let fallback = if content_type - .as_deref() - .is_some_and(moli_web_mime::is_xml_document_mime) - { - "UTF-8".to_owned() - } else { - opener_character_set.clone() - }; - let (markup, character_set) = decode_html_document_with_fallback( + // A top-level popup has no container Document encoding to inherit. + let (markup, character_set) = decode_document_response( response.body_bytes(), &head.headers, - Some(&fallback), + content_type.as_deref(), + &head.final_url, + None, ); let policy_container = DocumentPolicyContainer::from_navigation_response_headers( &head.headers,