diff --git a/moli-core/src/page/mod.rs b/moli-core/src/page/mod.rs index 85b7e3d8c25..f930b71f738 100644 --- a/moli-core/src/page/mod.rs +++ b/moli-core/src/page/mod.rs @@ -87,12 +87,12 @@ pub use moli_renderer_v8::{ RendererAgentAttachmentId, RendererAutofillAddressField, RendererAutofillCreditCard, RendererAutofillTriggerOutcome, RendererAutofillTriggerRequest, RendererAuxiliaryDocumentResponse, RendererAuxiliaryNavigationKind, RendererAuxiliaryWindow, - RendererBrowsingContextGroup, RendererBrowsingContextName, RendererCaptureScreencastFrameReply, - RendererCaptureScreencastFrameRequest, RendererCaptureScreenshotReply, - RendererCaptureScreenshotRequest, RendererCapturedDocumentEnvironment, - RendererCommandTurnCompletion, RendererCommandTurnOutput, RendererContentStripOptions, - RendererDedicatedWorkerTargetEvent, RendererDedicatedWorkerTargetInfo, - RendererDevToolsAgentToken, RendererDocumentHitTestResult, + RendererBlobUrlResource, RendererBrowsingContextGroup, RendererBrowsingContextName, + RendererCaptureScreencastFrameReply, RendererCaptureScreencastFrameRequest, + RendererCaptureScreenshotReply, RendererCaptureScreenshotRequest, + RendererCapturedDocumentEnvironment, RendererCommandTurnCompletion, RendererCommandTurnOutput, + RendererContentStripOptions, RendererDedicatedWorkerTargetEvent, + RendererDedicatedWorkerTargetInfo, RendererDevToolsAgentToken, RendererDocumentHitTestResult, RendererDocumentIsolateAccountingDiagnostics, RendererDocumentLifecycleEvent, RendererDocumentLifecycleEventKind, RendererDocumentLifecycleIdentity, RendererDocumentLifecycleMilestone, RendererDocumentLifecycleSnapshot, diff --git a/moli-file-api/src/blob_store.rs b/moli-file-api/src/blob_store.rs index b177a20d919..3bf00481e50 100644 --- a/moli-file-api/src/blob_store.rs +++ b/moli-file-api/src/blob_store.rs @@ -47,6 +47,22 @@ pub struct ObjectUrlEntry { pub access_key: Option, } +/// The backing retained when a byte-resource request resolves a Blob URL. +/// MediaSource URLs retain their unsupported kind without moving a native +/// MediaSource object across the resource-loading boundary. +#[derive(Clone, Debug)] +pub enum ObjectUrlResourceData { + Blob { data: BlobData, mime_type: String }, + MediaSource, +} + +/// Captures an immutable backing and creator key without starting I/O. +#[derive(Clone, Debug)] +pub struct ObjectUrlResourceEntry { + pub data: ObjectUrlResourceData, + pub access_key: Option, +} + #[derive(Clone, Debug)] struct BlobState { owner_id: Option, @@ -479,6 +495,29 @@ where self.object_url_entry_if(url, |_| true) } + /// Resolve the URL at request preparation, retaining its backing even if + /// the URL and its Blob wrapper are later released. Permission checks and + /// byte consumption remain the resource consumer's responsibility. + pub fn object_url_resource_entry(&self, url: &str) -> Option> + where + AccessKey: Clone, + { + let url = url.split_once('#').map_or(url, |(url, _)| url); + let object_urls = self.object_urls.lock(); + let state = object_urls.get(url)?; + let data = match &state.target { + ObjectUrlTarget::Blob(id) => { + let (data, mime_type) = self.object_url_blob_data(*id)?; + ObjectUrlResourceData::Blob { data, mime_type } + } + ObjectUrlTarget::MediaSource(_) => ObjectUrlResourceData::MediaSource, + }; + Some(ObjectUrlResourceEntry { + data, + access_key: state.access_key.clone(), + }) + } + /// Authorize and capture the entry under the URL-store lock, before reading /// a file or database backing store. Missing creator keys are not public. pub fn object_url_entry_with_access_key( @@ -734,6 +773,79 @@ mod tests { assert_eq!(source.0.load(Ordering::SeqCst), 1); } + #[test] + fn resource_entries_retain_lazy_data_without_url_or_wrapper_references() { + use std::sync::atomic::AtomicUsize; + + #[derive(Debug)] + struct Source(AtomicUsize); + impl crate::BlobSource for Source { + fn read(&self, range: std::ops::Range, complete: crate::BlobReadCompletion) { + self.0.fetch_add(1, Ordering::Relaxed); + complete(Ok(b"retained"[range].to_vec())); + } + } + let store = BlobStore::::default(); + let source = Arc::new(Source(AtomicUsize::new(0))); + let weak = Arc::downgrade(&source); + let blob = store.create_blob_from_data( + Some(1), + Some(2), + BlobData::from_source(source.clone(), 8), + "text/javascript".to_owned(), + ); + let key = "creator".to_owned(); + let url = store + .create_object_url_with_access_key(Some(1), blob, "null", Some(key.clone())) + .unwrap(); + let captured = store + .object_url_resource_entry(&format!("{url}#fragment")) + .unwrap(); + assert!( + store + .object_url_resource_entry(&format!("{url}?query")) + .is_none() + ); + assert_eq!(source.0.load(Ordering::Relaxed), 0); + assert!(store.revoke_object_url(&url)); + store.release_blob_wrapper_ref(blob); + assert!(store.blob_size(blob).is_none()); + assert!(store.object_url_resource_entry(&url).is_none()); + assert_eq!(captured.access_key, Some(key)); + let ObjectUrlResourceData::Blob { data, mime_type } = captured.data else { + panic!("Blob entry must retain its lazy byte source"); + }; + assert_eq!(mime_type, "text/javascript"); + assert_eq!(data.slice(2..6).read_blocking().unwrap(), b"tain"); + assert_eq!(source.0.load(Ordering::Relaxed), 1); + drop(source); + assert!(weak.upgrade().is_some()); + drop(data); + assert!(weak.upgrade().is_none()); + } + + #[test] + fn resource_entries_preserve_media_source_kind_without_retaining_native_objects() { + let store = BlobStore::>::default(); + let source = Arc::new(()); + let weak = Arc::downgrade(&source); + let url = store + .create_object_url_with_target( + None, + None, + ObjectUrlTarget::MediaSource(source), + "null", + None, + None, + ) + .unwrap(); + let captured = store.object_url_resource_entry(&url).unwrap(); + assert!(matches!(captured.data, ObjectUrlResourceData::MediaSource)); + assert!(captured.access_key.is_none()); + assert!(store.revoke_object_url(&url)); + assert!(weak.upgrade().is_none()); + } + #[test] fn shared_blob_backing_is_installed_without_copy_and_outlives_wrapper_release() { let store = BlobStore::::default(); diff --git a/moli-file-api/src/lib.rs b/moli-file-api/src/lib.rs index df2455492f9..5c6f7e6a572 100644 --- a/moli-file-api/src/lib.rs +++ b/moli-file-api/src/lib.rs @@ -7,7 +7,10 @@ pub mod data_transfer; pub mod file; pub use blob_data::{BlobData, BlobReadCompletion, BlobSource}; -pub use blob_store::{BlobId, BlobStore, ObjectUrlData, ObjectUrlEntry, ObjectUrlTarget}; +pub use blob_store::{ + BlobId, BlobStore, ObjectUrlData, ObjectUrlEntry, ObjectUrlResourceData, + ObjectUrlResourceEntry, ObjectUrlTarget, +}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum BlobLineEndings { diff --git a/moli-protocol-server/src/protocol_server/tests/websocket.rs b/moli-protocol-server/src/protocol_server/tests/websocket.rs index cd26cb91866..3e8391b7feb 100644 --- a/moli-protocol-server/src/protocol_server/tests/websocket.rs +++ b/moli-protocol-server/src/protocol_server/tests/websocket.rs @@ -1,5 +1,6 @@ use super::*; +mod blob_url_lifetime; mod browser_close; mod captured_document_navigation; mod child_navigation_wait; diff --git a/moli-protocol-server/src/protocol_server/tests/websocket/blob_url_lifetime.js b/moli-protocol-server/src/protocol_server/tests/websocket/blob_url_lifetime.js new file mode 100644 index 00000000000..0c6f00c0b6a --- /dev/null +++ b/moli-protocol-server/src/protocol_server/tests/websocket/blob_url_lifetime.js @@ -0,0 +1,84 @@ +async function probeBlobLifetime(kind, revoke) { + const events = []; + globalThis.__blobLifetimePhase = 'starting'; + const id = `blob-lifetime-${Date.now()}-${Math.random()}`; + let settle; + const done = new Promise(resolve => { settle = resolve; }); + const channel = new BroadcastChannel(id); + channel.onmessage = event => { + events.push(['channel', event.data]); + if (event.data === id) settle('loaded'); + }; + const source = (kind === 'script' || kind === 'module') + ? `globalThis.__blobLifetimeMarker = ${JSON.stringify(id)}; globalThis.__blobLifetimeExecutedPhase = globalThis.__blobLifetimePhase;` + : `${id}`; + const url = URL.createObjectURL(new Blob([source], {type: kind === 'module' ? 'text/javascript' : kind === 'script' ? '' : 'text/html'})); + let element; + let popup; + const timeout = setTimeout(() => settle('timeout'), 5000); + try { + if ((kind === 'script' || kind === 'module')) { + element = document.createElement('script'); + element.src = url; + if (kind === 'module') element.type = 'module'; + element.onload = () => { + events.push(['load', globalThis.__blobLifetimeMarker]); + settle(globalThis.__blobLifetimeMarker === id && globalThis.__blobLifetimeExecutedPhase === 'after-start' ? 'loaded' : 'wrong-marker-or-order'); + }; + element.onerror = () => { events.push(['error']); settle('error'); }; + document.body.appendChild(element); + } else if (kind === 'iframe' || kind === 'iframe-navigation') { + element = document.createElement('iframe'); + if (kind === 'iframe-navigation') { + element.src = '/blank'; + await new Promise((resolve, reject) => { + element.onload = resolve; + element.onerror = reject; + document.body.appendChild(element); + }); + } else { + element.src = url; + } + element.onload = () => { + let marker; + try { marker = element.contentWindow.test_result; } + catch (error) { events.push(['access-error', error.name]); } + events.push(['load', marker || null]); + if (marker === id) settle('loaded'); + }; + if (kind === 'iframe-navigation') element.contentWindow.location = url; + else document.body.appendChild(element); + } else if (kind === 'popup') { + popup = window.open(url); + popup.onload = () => { + const marker = popup.test_result; + events.push(['load', marker || null]); + if (marker === id) settle('loaded'); + }; + } else if (kind === 'popup-noopener') { + popup = window.open(); + popup.opener = null; + popup.location = url; + } else if (kind === 'anchor') { + element = document.createElement('a'); + element.href = url; + element.target = '_blank'; + document.body.appendChild(element); + element.click(); + } else { + throw new Error(`unknown kind ${kind}`); + } + globalThis.__blobLifetimePhase = 'after-start'; + events.push(['started']); + if (revoke) URL.revokeObjectURL(url); + events.push(['after-revoke', revoke]); + const outcome = await done; + return {kind, revoke, outcome, events}; + } finally { + clearTimeout(timeout); + channel.close(); + element?.remove(); + if (popup && !popup.closed) popup.close(); + URL.revokeObjectURL(url); + } +} diff --git a/moli-protocol-server/src/protocol_server/tests/websocket/blob_url_lifetime.rs b/moli-protocol-server/src/protocol_server/tests/websocket/blob_url_lifetime.rs new file mode 100644 index 00000000000..86487142801 --- /dev/null +++ b/moli-protocol-server/src/protocol_server/tests/websocket/blob_url_lifetime.rs @@ -0,0 +1,110 @@ +use super::*; + +#[tokio::test] +async fn websocket_cdp_blob_url_loads_survive_revocation_after_start() { + let app = Router::new() + .route( + "/page", + get(|| async { + axum::response::Html( + "", + ) + }), + ) + .route( + "/blank", + get(|| async { axum::response::Html("blank") }), + ) + .route( + "/probe.js", + get(|| async { + ( + [(header::CONTENT_TYPE.as_str(), "text/javascript; charset=utf-8")], + include_str!("blob_url_lifetime.js"), + ) + }), + ); + let (fixture_addr, _fixture) = spawn_dedicated_fixture_server(app, "blob-url-lifetime"); + let (addr, server) = spawn_test_protocol_server().await; + let (mut socket, _) = connect_async( + discover_browser_websocket_url(addr) + .await + .expect("discover browser endpoint"), + ) + .await + .unwrap(); + let mut results = Vec::new(); + for (index, (kind, revoke)) in [ + "script", + "module", + "iframe", + "iframe-navigation", + "popup", + "popup-noopener", + "anchor", + ] + .into_iter() + .flat_map(|kind| [false, true].map(|revoke| (kind, revoke))) + .enumerate() + { + let id = 100 * index as u64 + 1; + let context = cdp_create_browser_context(&mut socket, id).await; + let session = cdp_create_attached_target(&mut socket, id + 1, &context).await; + send_cdp_command( + &mut socket, + id + 4, + "Page.enable", + Some(&session.session_id), + json!({}), + ) + .await; + cdp_navigate_and_wait_for_load( + &mut socket, + id + 5, + &session.session_id, + &format!("http://{fixture_addr}/page"), + ) + .await; + let expression = format!( + "probeBlobLifetime({}, {revoke})", + serde_json::to_string(kind).unwrap(), + ); + let messages = send_cdp_command( + &mut socket, + id + 6, + "Runtime.evaluate", + Some(&session.session_id), + json!({"expression": expression, "awaitPromise": true, "returnByValue": true}), + ) + .await; + let response = messages + .iter() + .find(|message| message["id"] == id + 6) + .expect("Blob lifetime evaluation response"); + assert!(response["error"].is_null(), "{kind}/{revoke}: {response}"); + assert!( + response["result"]["exceptionDetails"].is_null(), + "{kind}/{revoke}: {response}" + ); + results.push(response["result"]["result"]["value"].clone()); + send_cdp_command( + &mut socket, + id + 7, + "Target.disposeBrowserContext", + None, + json!({"browserContextId": context}), + ) + .await; + } + let _ = socket.close(None).await; + abort_test_cdp_server(server).await; + let failures: Vec<_> = results + .iter() + .filter(|result| result["outcome"] != "loaded") + .collect(); + assert_eq!(results.len(), 14); + assert!( + failures.is_empty(), + "Blob URL lifetime failures: {failures:?}" + ); +} diff --git a/moli-protocol/src/conn/browser_context/target_session_owner.rs b/moli-protocol/src/conn/browser_context/target_session_owner.rs index 41e1c37345b..cdac9549841 100644 --- a/moli-protocol/src/conn/browser_context/target_session_owner.rs +++ b/moli-protocol/src/conn/browser_context/target_session_owner.rs @@ -148,6 +148,7 @@ impl TargetNavigationStorageHandles { #[derive(Clone)] pub(crate) struct TargetNavigationLoadInputs { pub(crate) window_identity: moli_core::page::RendererPageWindowIdentity, + pub(crate) blob_resource: Option, pub(crate) auxiliary_document_response: Option, pub(crate) redirect_chain: Vec, @@ -333,6 +334,7 @@ impl TargetNavigationLoadInputs { .expect("resolved Page target retains document activity"); Self { + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, @@ -444,6 +446,7 @@ impl TargetNavigationLoadInputs { renderer_runtime: RendererBrowserContextRuntimeOwnerAccess, ) -> Self { Self { + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/conn/dispatch_tests/bidi.rs b/moli-protocol/src/conn/dispatch_tests/bidi.rs index 52d62c2e674..c2243e4365c 100644 --- a/moli-protocol/src/conn/dispatch_tests/bidi.rs +++ b/moli-protocol/src/conn/dispatch_tests/bidi.rs @@ -209,6 +209,7 @@ async fn bidi_fetch_control_resolves_background_request_owner() { web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/conn/page_state/fetch_state.rs b/moli-protocol/src/conn/page_state/fetch_state.rs index 72c4b4331b0..cd81c16c727 100644 --- a/moli-protocol/src/conn/page_state/fetch_state.rs +++ b/moli-protocol/src/conn/page_state/fetch_state.rs @@ -367,6 +367,7 @@ mod tests { web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/conn/popup_navigation_work.rs b/moli-protocol/src/conn/popup_navigation_work.rs index d7e59c2db4b..af7e01de93b 100644 --- a/moli-protocol/src/conn/popup_navigation_work.rs +++ b/moli-protocol/src/conn/popup_navigation_work.rs @@ -21,6 +21,7 @@ pub(crate) struct PopupTargetNavigationOwnerAction { document_response: Option, initial_document_environment: Option, navigation_initiator: Option, + blob_resource: Option, } #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -48,6 +49,7 @@ impl PopupTargetNavigationOwnerAction { document_response: None, initial_document_environment: None, navigation_initiator: None, + blob_resource: None, }) } @@ -71,6 +73,14 @@ impl PopupTargetNavigationOwnerAction { &self.browser_context_id } + pub(crate) fn with_blob_resource( + mut self, + resource: Option, + ) -> Self { + self.blob_resource = resource; + self + } + pub(crate) fn with_navigation_initiator( mut self, initiator: Option, @@ -114,6 +124,7 @@ impl PopupTargetNavigationOwnerAction { Option, Option, Option, + Option, ) { ( CommandOwnerScope::for_route(CdpSessionRoute::PageTarget { @@ -128,6 +139,7 @@ impl PopupTargetNavigationOwnerAction { self.document_response, self.initial_document_environment, self.navigation_initiator, + self.blob_resource, ) } } diff --git a/moli-protocol/src/conn/runtime_load.rs b/moli-protocol/src/conn/runtime_load.rs index fc97f9ee177..aff2cf8b79b 100644 --- a/moli-protocol/src/conn/runtime_load.rs +++ b/moli-protocol/src/conn/runtime_load.rs @@ -1101,6 +1101,36 @@ impl BackgroundNavigationLoadJob { } ensure_url_not_blocked_for_load_inputs(&self.load_inputs, &self.raw_url)?; + if let Some(resource) = &self.load_inputs.blob_resource { + let url = Url::parse(&self.raw_url)?; + if let Some(response) = resource + .response(&url, &self.method, &self.request_headers.to_byte_strings()) + .await + { + let (head, bytes) = response?.into_byte_parts(); + let body = CapturedBody::from_bytes(bytes); + let navigation = prepare_navigation_from_captured_raw_response_with_engine_async( + &mut engine, + self.page_reservation.clone(), + &self.load_inputs, + url, + self.method.clone(), + self.request_headers.clone(), + head, + body, + self.body_progress_source.clone(), + NetworkObservationJournal::default(), + None, + false, + reply_boundary, + ) + .await + .map(NavigationLoadOutcome::response_commit_ready); + early_result_sent = + Self::emit_early_result_for_successful_document(&mut early_result, &navigation); + return navigation; + } + } if self.load_inputs.network_offline { // Emulated offline is a network failure, not a protocol error. // Report it like any other transport failure: commit a @@ -1850,6 +1880,7 @@ impl CdpConnection { ) .with_navigation_history(navigation.navigation_history.clone()); inputs.auxiliary_document_response = navigation.auxiliary_document_response.clone(); + inputs.blob_resource = navigation.blob_resource.clone(); if let Some(initiator) = navigation.navigation_initiator.as_ref() { inputs.navigation_initiator_url = Some(initiator.url().clone()); inputs.navigation_request_metadata = Some(initiator.request_metadata()); diff --git a/moli-protocol/src/conn/state/fetch.rs b/moli-protocol/src/conn/state/fetch.rs index 167eff0d378..571222c7de6 100644 --- a/moli-protocol/src/conn/state/fetch.rs +++ b/moli-protocol/src/conn/state/fetch.rs @@ -2566,6 +2566,7 @@ mod tests { web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/conn/state/navigation_outcome.rs b/moli-protocol/src/conn/state/navigation_outcome.rs index 793a0a72963..a27b3d42a8d 100644 --- a/moli-protocol/src/conn/state/navigation_outcome.rs +++ b/moli-protocol/src/conn/state/navigation_outcome.rs @@ -364,6 +364,7 @@ impl NavigationResultProjection { #[derive(Debug, Clone)] pub struct NavigationDispatchState { + pub(crate) blob_resource: Option, pub(crate) web_mcp_invocation: Option, pub(crate) navigation_initiator: Option, pub(crate) initial_document_environment: diff --git a/moli-protocol/src/conn/state/target_state.rs b/moli-protocol/src/conn/state/target_state.rs index 8bc23442976..e836c3d76b8 100644 --- a/moli-protocol/src/conn/state/target_state.rs +++ b/moli-protocol/src/conn/state/target_state.rs @@ -498,6 +498,7 @@ pub(crate) struct PendingPopupNavigation { pub(crate) response: Option, pub(crate) environment: Option, pub(crate) initiator: Option, + pub(crate) blob_resource: Option, } #[derive(Debug, Clone, Default)] diff --git a/moli-protocol/src/conn/state/tests.rs b/moli-protocol/src/conn/state/tests.rs index de15f3bd2e6..974228e0ccc 100644 --- a/moli-protocol/src/conn/state/tests.rs +++ b/moli-protocol/src/conn/state/tests.rs @@ -28,6 +28,7 @@ fn test_navigation_dispatch_state(fetch_request_id: &str) -> NavigationDispatchS web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/conn/tests/mod.rs b/moli-protocol/src/conn/tests/mod.rs index 311b5aa6732..93dc3ef508d 100644 --- a/moli-protocol/src/conn/tests/mod.rs +++ b/moli-protocol/src/conn/tests/mod.rs @@ -1987,6 +1987,7 @@ fn materialized_navigation_test_state( web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/conn/tests/navigation_error.rs b/moli-protocol/src/conn/tests/navigation_error.rs index c7447c243af..06f5c05d4cd 100644 --- a/moli-protocol/src/conn/tests/navigation_error.rs +++ b/moli-protocol/src/conn/tests/navigation_error.rs @@ -567,6 +567,7 @@ pub(super) fn navigation_fixture() -> ( web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/conn/tests/resource_runtime.rs b/moli-protocol/src/conn/tests/resource_runtime.rs index 3155e9d445f..34da2ac5a4a 100644 --- a/moli-protocol/src/conn/tests/resource_runtime.rs +++ b/moli-protocol/src/conn/tests/resource_runtime.rs @@ -105,6 +105,7 @@ async fn buffered_navigation_for_inactive_session_retains_its_target_engine() { web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/domains/activity/main_document.rs b/moli-protocol/src/domains/activity/main_document.rs index 324563ba39a..7ccddefe4f9 100644 --- a/moli-protocol/src/domains/activity/main_document.rs +++ b/moli-protocol/src/domains/activity/main_document.rs @@ -773,6 +773,7 @@ mod tests { web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/domains/fetch/tests/basics.rs b/moli-protocol/src/domains/fetch/tests/basics.rs index eaa1ca0506e..97a253b2452 100644 --- a/moli-protocol/src/domains/fetch/tests/basics.rs +++ b/moli-protocol/src/domains/fetch/tests/basics.rs @@ -1249,6 +1249,7 @@ async fn disable_clears_fetch_state() { web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, @@ -1298,6 +1299,7 @@ async fn disable_clears_fetch_state() { web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, @@ -1550,6 +1552,7 @@ async fn continue_with_auth_rejects_invalid_response_without_consuming_pending_a web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, @@ -1636,6 +1639,7 @@ async fn continue_with_auth_unsupported_challenge_preserves_pending_auth_navigat web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, @@ -1823,6 +1827,7 @@ fn emit_auth_required_preserves_request_headers_and_post_data_shape() { web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, @@ -1898,6 +1903,7 @@ async fn untracked_fetch_actions_acknowledge_the_handler_and_abort_only_the_orig navigation_initiator: None, navigation_history: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/domains/network/main_document_progress/tests.rs b/moli-protocol/src/domains/network/main_document_progress/tests.rs index 22f0c738605..e738154f72b 100644 --- a/moli-protocol/src/domains/network/main_document_progress/tests.rs +++ b/moli-protocol/src/domains/network/main_document_progress/tests.rs @@ -1811,6 +1811,7 @@ fn no_content_navigation_retains_response_metadata_with_live_progress() { web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/domains/network/tests/cache.rs b/moli-protocol/src/domains/network/tests/cache.rs index 670d5439233..c6220790f6a 100644 --- a/moli-protocol/src/domains/network/tests/cache.rs +++ b/moli-protocol/src/domains/network/tests/cache.rs @@ -653,6 +653,7 @@ async fn clear_browser_cache_keeps_pending_response_navigation_transfer() { web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/domains/network/tests/response_body.rs b/moli-protocol/src/domains/network/tests/response_body.rs index d623e03a118..a728715028b 100644 --- a/moli-protocol/src/domains/network/tests/response_body.rs +++ b/moli-protocol/src/domains/network/tests/response_body.rs @@ -511,6 +511,7 @@ async fn get_request_post_data_returns_main_document_navigation_post_body() { web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, @@ -609,6 +610,7 @@ async fn get_request_post_data_uses_text_projection_while_bidi_collector_keeps_t web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/domains/page.rs b/moli-protocol/src/domains/page.rs index 3ff6fae0f49..97389d2d209 100644 --- a/moli-protocol/src/domains/page.rs +++ b/moli-protocol/src/domains/page.rs @@ -1261,6 +1261,7 @@ pub(in crate::domains) fn prepare_renderer_navigation_request( document_response: Option, initial_document_environment: Option, navigation_initiator: Option, + blob_resource: Option, ) -> PreparedRendererNavigation { let start = navigation::start_session_owner_navigation_from_renderer( conn, @@ -1276,6 +1277,7 @@ pub(in crate::domains) fn prepare_renderer_navigation_request( document_response, initial_document_environment, navigation_initiator, + blob_resource, ); let step = navigation::finish_started_navigation_command_for_parts( conn, diff --git a/moli-protocol/src/domains/page/navigation.rs b/moli-protocol/src/domains/page/navigation.rs index e5b0b1ac3a7..f36f39816a2 100644 --- a/moli-protocol/src/domains/page/navigation.rs +++ b/moli-protocol/src/domains/page/navigation.rs @@ -1878,6 +1878,7 @@ pub(super) fn start_session_owner_navigation_from_renderer( document_response: Option, initial_document_environment: Option, navigation_initiator: Option, + blob_resource: Option, ) -> NavigateCommandStart { if let Some(history) = &navigation_history { match history.navigation_type() { @@ -1982,6 +1983,7 @@ pub(super) fn start_session_owner_navigation_from_renderer( document_response, initial_document_environment, navigation_initiator, + blob_resource, ) }; clear_crash_state_for_renderer_navigation(conn, start, owner, &reloaded_after_crash_session_ids) @@ -2693,6 +2695,7 @@ fn start_navigate_to_url_command_with_background_policy( None, None, None, + None, ) } @@ -2723,6 +2726,7 @@ fn start_navigate_to_url_command_with_background_policy_and_request( document_response: Option, initial_document_environment: Option, navigation_initiator: Option, + blob_resource: Option, ) -> NavigateCommandStart { let command_session_id = owner.session_id(); let mut out = Vec::new(); @@ -2778,6 +2782,7 @@ fn start_navigate_to_url_command_with_background_policy_and_request( navigation_history, ); let mut navigation_state = NavigationDispatchState { + blob_resource, web_mcp_invocation, navigation_initiator, initial_document_environment, diff --git a/moli-protocol/src/domains/page/renderer_navigation.rs b/moli-protocol/src/domains/page/renderer_navigation.rs index b53126d9d94..58b51bbf12f 100644 --- a/moli-protocol/src/domains/page/renderer_navigation.rs +++ b/moli-protocol/src/domains/page/renderer_navigation.rs @@ -259,6 +259,7 @@ impl PreparedRendererNavigation { None, navigation.initial_document_environment().cloned(), navigation.navigation_initiator().cloned(), + navigation.blob_resource().cloned(), ); super::navigation::finish_started_navigation_command_for_parts( conn, diff --git a/moli-protocol/src/domains/page/tests/navigation/history.rs b/moli-protocol/src/domains/page/tests/navigation/history.rs index 7efcd87bf7f..0572580ad7f 100644 --- a/moli-protocol/src/domains/page/tests/navigation/history.rs +++ b/moli-protocol/src/domains/page/tests/navigation/history.rs @@ -41,6 +41,7 @@ async fn renderer_fragment_navigation_preserves_initial_document_residence() { None, None, None, + None, ) }); let mut command = crate::conn::CommandDispatchContext::default(); diff --git a/moli-protocol/src/domains/page/tests/navigation/navigation_flow.rs b/moli-protocol/src/domains/page/tests/navigation/navigation_flow.rs index 09602b05c12..583af71dccb 100644 --- a/moli-protocol/src/domains/page/tests/navigation/navigation_flow.rs +++ b/moli-protocol/src/domains/page/tests/navigation/navigation_flow.rs @@ -246,6 +246,7 @@ async fn renderer_navigation_publishes_one_fact_and_separate_cdp_projection() { None, None, None, + None, ) }); let mut command = crate::conn::CommandDispatchContext::default(); diff --git a/moli-protocol/src/domains/runtime/tests/evaluate/navigation.rs b/moli-protocol/src/domains/runtime/tests/evaluate/navigation.rs index 62ce0bfe6ac..6f0f74f94e5 100644 --- a/moli-protocol/src/domains/runtime/tests/evaluate/navigation.rs +++ b/moli-protocol/src/domains/runtime/tests/evaluate/navigation.rs @@ -670,6 +670,7 @@ async fn page_navigate_network_failure_commits_error_document() { web_mcp_invocation: None, navigation_initiator: None, initial_document_environment: None, + blob_resource: None, auxiliary_document_response: None, redirect_chain: Vec::new(), redirect_headers: None, diff --git a/moli-protocol/src/domains/target/popup.rs b/moli-protocol/src/domains/target/popup.rs index 853f3fa820e..f9ebf9b5a38 100644 --- a/moli-protocol/src/domains/target/popup.rs +++ b/moli-protocol/src/domains/target/popup.rs @@ -168,6 +168,7 @@ pub(crate) fn prepare_popup_target( initial_document_environment, pending_auxiliary_page, url, + blob_resource, target_name, session_storage_store, initial_empty_document_storage_key, @@ -266,7 +267,8 @@ pub(crate) fn prepare_popup_target( navigation .with_document_response(document_response) .with_initial_document_environment(initial_document_environment) - .with_navigation_initiator(navigation_initiator), + .with_navigation_initiator(navigation_initiator) + .with_blob_resource(blob_resource), ); } else if !has_loaded_page { // Keep the latest accepted request while the same Host joins @@ -280,6 +282,7 @@ pub(crate) fn prepare_popup_target( response: document_response, environment: initial_document_environment, initiator: navigation_initiator, + blob_resource, }); } if let Some(activation) = activation { @@ -398,6 +401,7 @@ pub(crate) fn prepare_popup_target( response: document_response, environment: initial_document_environment, initiator: navigation_initiator, + blob_resource, }); } @@ -628,6 +632,7 @@ impl PreparedPopupNavigation { mut document_response, mut initial_document_environment, mut navigation_initiator, + mut blob_resource, ) = action.into_parts(); let target_is_current = conn .target_owner_identity_for_owner(&owner_scope) @@ -681,6 +686,7 @@ impl PreparedPopupNavigation { document_response = pending.response; initial_document_environment = pending.environment; navigation_initiator = pending.initiator; + blob_resource = pending.blob_resource; } let auxiliary_navigation = match kind { PopupTargetNavigationKind::WindowReference(kind) => Some(kind), @@ -727,6 +733,7 @@ impl PreparedPopupNavigation { document_response, initial_document_environment, navigation_initiator, + blob_resource, ); Some(Self { navigation, diff --git a/moli-renderer-v8/src/blob.rs b/moli-renderer-v8/src/blob.rs index 48f4bebdc83..6492d5acba6 100644 --- a/moli-renderer-v8/src/blob.rs +++ b/moli-renderer-v8/src/blob.rs @@ -179,6 +179,12 @@ pub(crate) fn object_url_entry(url: &str) -> Option { blob_store().object_url_entry(url) } +pub(crate) fn object_url_resource_entry( + url: &str, +) -> Option> { + blob_store().object_url_resource_entry(url) +} + pub(crate) fn object_url_body_and_type_with_access_key( url: &str, access_key: &ObjectUrlAccessKey, diff --git a/moli-renderer-v8/src/dynamic_script_owner.rs b/moli-renderer-v8/src/dynamic_script_owner.rs index 7c7d473a0e0..735c7d21a60 100644 --- a/moli-renderer-v8/src/dynamic_script_owner.rs +++ b/moli-renderer-v8/src/dynamic_script_owner.rs @@ -72,6 +72,7 @@ pub(super) struct DynamicScriptOwner { continuation_tx: Arc>>, continuation_turn_queued: Arc, load_delay_bindings: HashMap, + blob_resources: HashMap, } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -392,6 +393,7 @@ impl DynamicScriptOwner { continuation_tx: Arc::default(), continuation_turn_queued: Arc::default(), load_delay_bindings: HashMap::new(), + blob_resources: HashMap::new(), } } @@ -466,8 +468,11 @@ impl DynamicScriptOwner { document_character_set: Option<&str>, service_worker_context: Option<&DynamicScriptServiceWorkerContext>, ) { - let (payload, binding) = admission.into_parts(); + let (payload, binding, blob_resource) = admission.into_parts(); let id = self.reserve_script_id(); + if let Some(resource) = blob_resource { + self.blob_resources.insert(id, resource); + } match payload { RuntimeScriptAdmissionPayload::Script(script) => { let queue_kind = DynamicScriptQueueKind::for_mode(script.mode); @@ -603,11 +608,19 @@ impl DynamicScriptOwner { id: DynamicScriptOwnerId, ) -> Option { let _ = self.remove_entry_by_id(id); + self.blob_resources.remove(&id); let lease = self.load_delay_bindings.remove(&id); self.refresh_followup_work(); lease } + pub(super) fn take_blob_resource( + &mut self, + id: DynamicScriptOwnerId, + ) -> Option { + self.blob_resources.remove(&id) + } + /// Transfers terminal responsibility for one already-selected non-module /// runtime script to its stable page-owned execution task. pub(super) fn claim_page_owned_execution( @@ -1487,9 +1500,18 @@ impl DynamicScriptOwner { let document_character_set = document_character_set.map(str::to_owned); let service_worker_context = service_worker_context.cloned(); let fetch_task_runner = task_runner.clone(); + let blob_resource = self.take_blob_resource(id); self.in_flight_loads += 1; task_runner.spawn(async move { - let outcome = if let Some(context) = service_worker_context { + let outcome = if let Some(resource) = blob_resource { + crate::planning::load_captured_blob_script_source_outcome( + &script_for_load, + &request_origin, + &resource, + document_character_set.as_deref(), + ) + .await + } else if let Some(context) = service_worker_context { load_service_worker_aware_external_script_source_outcome( &script_for_load, &request_origin, diff --git a/moli-renderer-v8/src/host/scripts.rs b/moli-renderer-v8/src/host/scripts.rs index dd716d9c5de..67894cf29b3 100644 --- a/moli-renderer-v8/src/host/scripts.rs +++ b/moli-renderer-v8/src/host/scripts.rs @@ -242,6 +242,7 @@ pub(crate) struct FailedDynamicScript { pub(crate) struct RuntimeScriptAdmission { payload: Box, load_delay_binding: MainDocumentScriptLoadDelayLease, + blob_resource: Option, } #[derive(Debug)] @@ -266,16 +267,39 @@ impl RuntimeScriptAdmission { Self { payload, load_delay_binding, + blob_resource: None, } } + pub(crate) fn capture_blob_resource( + mut self, + environment: impl FnOnce() -> Option, + ) -> Self { + if let RuntimeScriptAdmissionPayload::Script(script) = &*self.payload + && matches!(script.source, crate::planning::ScriptSource::External) + { + self.blob_resource = crate::network_host::CapturedBlobResource::capture(&script.url) + .map(|resource| resource.for_fetch_environment(environment().as_ref())); + } + self + } + + pub(crate) fn with_blob_resource( + mut self, + resource: Option, + ) -> Self { + self.blob_resource = resource; + self + } + pub(crate) fn into_parts( self, ) -> ( RuntimeScriptAdmissionPayload, MainDocumentScriptLoadDelayLease, + Option, ) { - (*self.payload, self.load_delay_binding) + (*self.payload, self.load_delay_binding, self.blob_resource) } pub(crate) fn owner(&self) -> crate::frame_owner_model::FrameDocumentTaskOwner { diff --git a/moli-renderer-v8/src/lib.rs b/moli-renderer-v8/src/lib.rs index 9147d21aa62..540fc05b2b7 100644 --- a/moli-renderer-v8/src/lib.rs +++ b/moli-renderer-v8/src/lib.rs @@ -253,7 +253,7 @@ pub use runtime::{ RendererActivityDiagnostics, RendererAgentAttachmentId, RendererAutofillAddressField, RendererAutofillCreditCard, RendererAutofillTriggerOutcome, RendererAutofillTriggerRequest, RendererAuxiliaryDocumentResponse, RendererAuxiliaryNavigationKind, RendererAuxiliaryWindow, - RendererBrowserContextRuntime, RendererBrowserContextRuntimeId, + RendererBlobUrlResource, RendererBrowserContextRuntime, RendererBrowserContextRuntimeId, RendererBrowserContextRuntimeOwner, RendererBrowserContextRuntimeOwnerAccess, RendererBrowsingContextGroup, RendererBrowsingContextName, RendererCaptureScreencastFrameReply, RendererCaptureScreencastFrameRequest, RendererCaptureScreenshotReply, diff --git a/moli-renderer-v8/src/module_runtime/fetch_context.rs b/moli-renderer-v8/src/module_runtime/fetch_context.rs index 97741f94f05..1fd6d840d80 100644 --- a/moli-renderer-v8/src/module_runtime/fetch_context.rs +++ b/moli-renderer-v8/src/module_runtime/fetch_context.rs @@ -230,6 +230,7 @@ impl NativeModuleFetchContext { self: Arc, load: ResourceLoadLease, request: Request, + blob_resource: Option, callback: impl FnOnce( anyhow::Result<( NavigationResponse, @@ -251,6 +252,11 @@ impl NativeModuleFetchContext { runner.clone().spawn(Box::pin(async move { let result = tokio::select! { result = async { + if let Some(resource) = blob_resource + && let Some(response) = resource.response(&request.url, "GET", &[]).await + { + return response.map(|response| (response.into(), None, false)); + } match self.service_worker.browser_context_runtime.fetch_service_worker_subresource_for_client_with_metadata_and_cancellation(self.service_worker.client_id, self.service_worker.document_url.clone(), &request, &request_client, runner, self.destination, self.resource_type, cancel.clone()).await? { Some(response) => Ok((*response.response, response.response_filter, !response.from_network_fallback)), None => { diff --git a/moli-renderer-v8/src/module_runtime/graph.rs b/moli-renderer-v8/src/module_runtime/graph.rs index 5c593ee2d2c..fc5be4f47df 100644 --- a/moli-renderer-v8/src/module_runtime/graph.rs +++ b/moli-renderer-v8/src/module_runtime/graph.rs @@ -63,6 +63,7 @@ pub(crate) struct ModuleGraphHandle { pub(crate) struct NativeModuleGraphJob { tree_job: Option, kind: NativeModuleGraphJobKind, + blob_resource: Option, } enum NativeModuleGraphJobKind { @@ -101,6 +102,7 @@ pub(crate) struct NativeModuleGraphFetchRequest { dependency: Option, csp_context: Option, fetch_context: Option>, + blob_resource: Option, csp_source_location: Option, } @@ -235,6 +237,7 @@ impl NativeModuleGraphFetchRequest { dependency: None, csp_context: None, fetch_context: None, + blob_resource: None, csp_source_location: None, } } @@ -394,6 +397,10 @@ impl NativeModuleGraphFetchRequest { self.fetch_context.as_ref() } + pub(crate) fn blob_resource(&self) -> Option<&crate::network_host::CapturedBlobResource> { + self.blob_resource.as_ref() + } + pub(crate) fn with_csp_source_location( mut self, location: Option, @@ -666,7 +673,21 @@ impl NativeModuleGraphFetchRequest { completion(Err(anyhow::anyhow!(error)), None); return Ok(()); } - context.fetch(load, request, completion); + context.fetch(load, request, self.blob_resource.clone(), completion); + Ok(()) + } else if let Some(resource) = self.blob_resource.clone() { + let url = self.source_url.clone(); + load.task_runner().spawn(async move { + let response = resource + .response(&url, "GET", &[]) + .await + .unwrap_or_else(|| Err(anyhow::anyhow!("captured Blob URL does not match"))); + load.finish(); + completion( + response.map(|response| (response.into(), None, false)), + None, + ); + }); Ok(()) } else { loader.fetch_cacheable_script_text_callback_with_load(request, load, move |response| { @@ -680,6 +701,14 @@ impl NativeModuleGraphFetchRequest { } impl NativeModuleGraphJob { + pub(crate) fn with_blob_resource( + mut self, + resource: Option, + ) -> Self { + self.blob_resource = resource; + self + } + fn module_script(root: ModuleRootInput, owner: ModuleScriptCompletionOwner) -> Self { let (label, chromium_tree, kind) = match owner { ModuleScriptCompletionOwner::Parser => ( @@ -703,6 +732,7 @@ impl NativeModuleGraphJob { Self { tree_job: Some(NativeModuleTreeJob::new(chromium_tree)), kind, + blob_resource: None, } } @@ -748,6 +778,7 @@ impl NativeModuleGraphJob { Self { tree_job: None, kind: NativeModuleGraphJobKind::DynamicImport(Box::new(request)), + blob_resource: None, } } @@ -776,7 +807,7 @@ impl NativeModuleGraphJob { &mut self, vm: &mut ScriptVm, ) -> std::result::Result { - match self.kind { + let mut advance = match self.kind { NativeModuleGraphJobKind::ParserOwned => { self.advance_owner_lane(vm, NATIVE_PARSER_MODULE_GRAPH_JOB) } @@ -786,7 +817,19 @@ impl NativeModuleGraphJob { NativeModuleGraphJobKind::DynamicImport(_) => { self.advance_owner_lane(vm, NATIVE_DYNAMIC_IMPORT_GRAPH_JOB) } + }?; + if let NativeModuleGraphJobAdvance::NeedFetches(requests) = &mut advance { + for request in requests { + if self + .blob_resource + .as_ref() + .is_some_and(|resource| resource.matches(&request.source_url)) + { + request.blob_resource = self.blob_resource.take(); + } + } } + Ok(advance) } pub(crate) fn advance_dynamic_import_owner_lane( @@ -2658,6 +2701,7 @@ fn native_module_graph_job_for_inline_entry( NativeModuleGraphJob { tree_job: Some(NativeModuleTreeJob::new(chromium_tree)), kind, + blob_resource: None, } } @@ -3487,6 +3531,7 @@ import "./c.mjs"; dependency: None, csp_context: None, fetch_context: None, + blob_resource: None, csp_source_location: None, } .request(&moli_url::WebOrigin::from_url(&url( @@ -3566,6 +3611,7 @@ import "./c.mjs"; dependency: None, csp_context: None, fetch_context: None, + blob_resource: None, csp_source_location: None, } .request(&moli_url::WebOrigin::from_url(&url( @@ -3620,6 +3666,7 @@ import "./c.mjs"; dependency: None, csp_context: None, fetch_context: None, + blob_resource: None, csp_source_location: None, } .request(&moli_url::WebOrigin::from_url(&url( @@ -3661,6 +3708,7 @@ import "./c.mjs"; dependency: None, csp_context: None, fetch_context: None, + blob_resource: None, csp_source_location: None, } .request(&moli_url::WebOrigin::from_url(&url( @@ -3721,6 +3769,7 @@ import "./c.mjs"; dependency: None, csp_context: None, fetch_context: None, + blob_resource: None, csp_source_location: None, } .request(&moli_url::WebOrigin::from_url(&url( @@ -3773,6 +3822,7 @@ import "./c.mjs"; dependency: None, csp_context: None, fetch_context: None, + blob_resource: None, csp_source_location: None, } .request(&moli_url::WebOrigin::from_url(&url( @@ -3814,6 +3864,7 @@ import "./c.mjs"; dependency: None, csp_context: None, fetch_context: None, + blob_resource: None, csp_source_location: None, } .request(&moli_url::WebOrigin::from_url(&url( @@ -3848,6 +3899,7 @@ import "./c.mjs"; dependency: None, csp_context: None, fetch_context: None, + blob_resource: None, csp_source_location: None, } .request(&moli_url::WebOrigin::from_url(&url( @@ -4635,6 +4687,7 @@ import "./c.mjs"; dependency: None, csp_context: None, fetch_context: None, + blob_resource: None, csp_source_location: None, }; let (tx, rx) = oneshot::channel(); @@ -4710,6 +4763,7 @@ import "./c.mjs"; dependency: None, csp_context: None, fetch_context: None, + blob_resource: None, csp_source_location: None, } .request(&moli_url::WebOrigin::from_url(&url( diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_documents/bootstrap_commit.rs b/moli-renderer-v8/src/native_bridge/context_host/child_documents/bootstrap_commit.rs index 376ed98e95f..667c06dc505 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_documents/bootstrap_commit.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_documents/bootstrap_commit.rs @@ -21,6 +21,7 @@ impl PageScriptHost { ) -> Option { let PendingChildFrameNavigation { bootstrap, + blob_resource, initiator, initiator_url, target_snapshot, @@ -94,72 +95,75 @@ impl PageScriptHost { .as_ref() .and_then(crate::runtime::RendererNavigationPolicy::referrer_policy) }); - let cached_snapshot = - match self.materialize_local_child_snapshot_for_bootstrap(handle, &bootstrap) { - LocalChildDocumentPreparation::Ready(snapshot) => { - let mut snapshot = *snapshot; - if let Some(policy) = &source_policy { - policy.apply_to(&mut snapshot.policy_container, &snapshot.url); - } - if matches!(&bootstrap, ChildBrowsingContextBootstrap::Srcdoc { .. }) { - let source_url = request_initiator - .as_ref() - .map(|source| source.url().clone()) - .or_else(|| initiator_url.clone()) - .or_else(|| { - self.owner_dispatch_scope_for_node(handle) - .and_then(|owner| { - self.document_referrer_source_url_for_dispatch_scope(owner) - }) - }); - snapshot.policy_container.document_referrer = source_url - .and_then(|source| { - moli_fetch::referrer_value( - &source, - &snapshot.url, - Some( - target_snapshot - .referrer_policy(initiator, source_referrer_policy), - ), - None, - ) - }) - .unwrap_or_default(); - } - snapshot = self.apply_page_csp_bypass_to_child_snapshot(snapshot); - if !target_snapshot.apply_response_policy( - &mut snapshot.policy_container, - &snapshot.url, - &[], - ) { - let policy = crate::document_runtime::DocumentPolicyContainer { - required_content_security_policy: snapshot - .policy_container - .required_content_security_policy, - ..Default::default() - }; - snapshot = ChildBrowsingContextSnapshot::html( - snapshot.url, - "".to_owned(), - ) - .with_policy_container(policy); - creation_kind = DocumentCreationKind::Error; - } - Some(snapshot) + let cached_snapshot = match self.materialize_local_child_snapshot_for_bootstrap( + handle, + &bootstrap, + blob_resource.as_ref(), + ) { + LocalChildDocumentPreparation::Ready(snapshot) => { + let mut snapshot = *snapshot; + if let Some(policy) = &source_policy { + policy.apply_to(&mut snapshot.policy_container, &snapshot.url); } - LocalChildDocumentPreparation::Historical(snapshot) => Some(*snapshot), - LocalChildDocumentPreparation::Unavailable => None, - LocalChildDocumentPreparation::IgnoredNavigation => { - crate::context_bootstrap::finish_joint_history_without_document_commit( - scope, - self, - super::super::OwnerDispatchScope::Child(handle), - ); - self.cancel_child_document_navigation_before_load(handle, navigation_load); - self.sync_existing_child_browsing_context_window_state(scope, handle); - return Some(ChildDocumentCommitResult::ready(None)); + if matches!(&bootstrap, ChildBrowsingContextBootstrap::Srcdoc { .. }) { + let source_url = request_initiator + .as_ref() + .map(|source| source.url().clone()) + .or_else(|| initiator_url.clone()) + .or_else(|| { + self.owner_dispatch_scope_for_node(handle) + .and_then(|owner| { + self.document_referrer_source_url_for_dispatch_scope(owner) + }) + }); + snapshot.policy_container.document_referrer = source_url + .and_then(|source| { + moli_fetch::referrer_value( + &source, + &snapshot.url, + Some( + target_snapshot + .referrer_policy(initiator, source_referrer_policy), + ), + None, + ) + }) + .unwrap_or_default(); } - }; + snapshot = self.apply_page_csp_bypass_to_child_snapshot(snapshot); + if !target_snapshot.apply_response_policy( + &mut snapshot.policy_container, + &snapshot.url, + &[], + ) { + let policy = crate::document_runtime::DocumentPolicyContainer { + required_content_security_policy: snapshot + .policy_container + .required_content_security_policy, + ..Default::default() + }; + snapshot = ChildBrowsingContextSnapshot::html( + snapshot.url, + "".to_owned(), + ) + .with_policy_container(policy); + creation_kind = DocumentCreationKind::Error; + } + Some(snapshot) + } + LocalChildDocumentPreparation::Historical(snapshot) => Some(*snapshot), + LocalChildDocumentPreparation::Unavailable => None, + LocalChildDocumentPreparation::IgnoredNavigation => { + crate::context_bootstrap::finish_joint_history_without_document_commit( + scope, + self, + super::super::OwnerDispatchScope::Child(handle), + ); + self.cancel_child_document_navigation_before_load(handle, navigation_load); + self.sync_existing_child_browsing_context_window_state(scope, handle); + return Some(ChildDocumentCommitResult::ready(None)); + } + }; let requires_async_load = cached_snapshot.is_none() && self.child_document_bootstrap_requires_async_load(&bootstrap); let owner_credentialless = self @@ -184,6 +188,7 @@ impl PageScriptHost { initiator, target_snapshot, ) + .with_blob_resource(blob_resource) .with_source_policy(source_policy) .with_request_initiator(request_initiator), ) 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 30755ade391..c8b19e510ea 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 @@ -130,6 +130,7 @@ impl PageScriptHost { ) -> Option { let PendingChildFrameNavigation { bootstrap, + blob_resource, initiator, initiator_url, target_snapshot, @@ -244,6 +245,7 @@ impl PageScriptHost { let fetch = ChildDocumentFetch { response_body: None, download_policy, + blob_resource, target_snapshot: target_snapshot.clone(), timing_origin: initiating_loader.fetch_context().request_origin(), target, @@ -1183,6 +1185,7 @@ impl PageScriptHost { pub(crate) struct ChildDocumentFetch { response_body: Option, download_policy: ChildNavigationDownloadPolicy, + blob_resource: Option, source_policy: Option, target_snapshot: ChildNavigationTargetSnapshot, timing_origin: moli_url::WebOrigin, @@ -1289,6 +1292,11 @@ impl ChildDocumentFetch { .as_ref() .map(|request| (client_id, request.request().clone())) }); + let blob_request = self.request.as_ref().and_then(|request| { + self.blob_resource + .as_ref() + .map(|resource| (resource.clone(), request.request().clone())) + }); let runtime = self.browser_context_runtime.clone(); let loader = self.resource_loader.clone(); let completion_tx = self.completion_tx.clone(); @@ -1298,6 +1306,17 @@ impl ChildDocumentFetch { if response.is_none() && buffered.is_none() { return Err(UNSUPPORTED_CHILD_DOCUMENT_BOOTSTRAP.to_owned()); } + if let Some((resource, request)) = blob_request + && let Some(response) = resource + .response( + &request.url, + &request.method, + &request.request_headers.to_byte_strings(), + ) + .await + { + return response.map(Into::into).map(AsyncSubresourceFetchResult::Response).map_err(|error| error.to_string()); + } if let Some((client_id, request)) = service_worker_request && let Some(response) = runtime .fetch_service_worker_child_main_resource_for_reserved_client( 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 543534d766a..377aa4c041c 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 @@ -27,7 +27,22 @@ impl PageScriptHost { handle: DomHandle, url: &Url, ) -> LocalChildDocumentPreparation { - let mut snapshot = match self.materialize_local_child_snapshot_for_url(url, Some(handle)) { + self.materialize_local_child_snapshot_for_navigation_url_with_blob_resource( + handle, url, None, + ) + } + + fn materialize_local_child_snapshot_for_navigation_url_with_blob_resource( + &self, + handle: DomHandle, + url: &Url, + blob_resource: Option<&crate::network_host::CapturedBlobResource>, + ) -> LocalChildDocumentPreparation { + let mut snapshot = match self.materialize_local_child_snapshot_for_url_with_blob_resource( + url, + Some(handle), + blob_resource, + ) { LocalChildDocumentPreparation::Ready(snapshot) => *snapshot, other => return other, }; @@ -49,6 +64,15 @@ impl PageScriptHost { &self, url: &Url, owner: Option, + ) -> LocalChildDocumentPreparation { + self.materialize_local_child_snapshot_for_url_with_blob_resource(url, owner, None) + } + + fn materialize_local_child_snapshot_for_url_with_blob_resource( + &self, + url: &Url, + owner: Option, + blob_resource: Option<&crate::network_host::CapturedBlobResource>, ) -> LocalChildDocumentPreparation { match url.scheme() { "about" if url.path() == "blank" => { @@ -59,7 +83,12 @@ impl PageScriptHost { } "http" | "https" => LocalChildDocumentPreparation::Unavailable, "blob" | "data" => { - let Some(response) = crate::network_host::local_url_response(url) else { + let response = + match blob_resource.and_then(|resource| resource.response_blocking(url)) { + Some(response) => response.ok(), + None => crate::network_host::local_url_response(url), + }; + let Some(response) = response else { return LocalChildDocumentPreparation::Unavailable; }; let head = response.head(); @@ -110,6 +139,7 @@ impl PageScriptHost { &self, handle: DomHandle, bootstrap: &ChildBrowsingContextBootstrap, + blob_resource: Option<&crate::network_host::CapturedBlobResource>, ) -> LocalChildDocumentPreparation { if let Some(snapshot) = self .child_browsing_contexts @@ -128,7 +158,12 @@ impl PageScriptHost { ChildBrowsingContextSnapshot::srcdoc(base_url.clone(), markup.clone()) } ChildBrowsingContextBootstrap::Url(url) => { - return self.materialize_local_child_snapshot_for_navigation_url(handle, url); + return self + .materialize_local_child_snapshot_for_navigation_url_with_blob_resource( + handle, + url, + blob_resource, + ); } ChildBrowsingContextBootstrap::Request(_) => { return LocalChildDocumentPreparation::Unavailable; diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_navigation/commit.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_navigation/commit.rs index e3d36c8ef80..2011158c18e 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_navigation/commit.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_navigation/commit.rs @@ -305,10 +305,11 @@ impl PageScriptHost { return None; } let attribute_bootstrap = entry_snapshot.attribute_bootstrap().clone(); - let target_snapshot = entry_snapshot + let attribute_navigation = entry_snapshot .pending_attribute_navigation() .cloned() .expect("an admitted attribute navigation retains its target snapshot"); + let target_snapshot = attribute_navigation.target_snapshot; let url = Self::child_browsing_context_navigation_entry_url(&attribute_bootstrap)?; let is_attribute_about_blank_noop = matches!( attribute_bootstrap, @@ -408,6 +409,7 @@ impl PageScriptHost { ChildDocumentNavigationInitiator::FrameOwnerElement, target_snapshot, ) + .with_blob_resource(attribute_navigation.blob_resource) .with_request_initiator( self.owner_dispatch_scope_for_node(handle) .and_then(|source| self.navigation_initiator_for_owner(source)), diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames.rs index db67f07b128..215172a6290 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frames.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames.rs @@ -70,7 +70,7 @@ pub(super) struct ChildBrowsingContextEntry { // navigation machinery. Keep them separate from an admitted document load, // which can still complete after the fragment change. same_document_attribute_bootstrap: Option, - pending_attribute_navigation: Option, + pending_attribute_navigation: Option, pending_live_navigation: Option, // Snapshot at navigation admission. The target's origin is checked again // when queued javascript: code is about to execute. @@ -98,11 +98,21 @@ pub(super) struct ChildBrowsingContextEntry { pending_service_worker_client_navigation: Option, } +/// The resource and target settings of an accepted frame attribute load. +/// Wrapper refreshes retain this request; a deferred commit must not resolve +/// its Blob URL again after the inserting script has revoked it. +#[derive(Debug, Clone)] +pub(super) struct PendingChildAttributeNavigation { + pub(super) target_snapshot: ChildNavigationTargetSnapshot, + pub(super) blob_resource: Option, +} + /// Provenance belongs to the queued navigation, including when an attribute /// setter reuses the history machinery for an already loaded child Document. #[derive(Debug, Clone)] pub(super) struct PendingChildFrameNavigation { pub(super) bootstrap: ChildBrowsingContextBootstrap, + pub(super) blob_resource: Option, pub(super) initiator_url: Option, pub(super) initiator: ChildDocumentNavigationInitiator, pub(super) target_snapshot: ChildNavigationTargetSnapshot, @@ -120,6 +130,9 @@ impl PendingChildFrameNavigation { target_snapshot: ChildNavigationTargetSnapshot, ) -> Self { Self { + blob_resource: PageScriptHost::child_browsing_context_bootstrap_url(&bootstrap) + .as_ref() + .and_then(crate::network_host::CapturedBlobResource::capture), bootstrap, initiator_url, initiator, @@ -137,6 +150,14 @@ impl PendingChildFrameNavigation { self } + pub(super) fn with_blob_resource( + mut self, + resource: Option, + ) -> Self { + self.blob_resource = resource; + self + } + pub(super) fn with_request_initiator( mut self, initiator: Option, @@ -168,7 +189,7 @@ impl ChildPerformanceTimeOrigin { pub(crate) struct ChildBrowsingContextNavigationSeedSnapshot { pub(crate) navigation_entry_seed: NavigationHistoryEntrySeed, pub(crate) committed_navigation_entry_seed: NavigationHistoryEntrySeed, - pending_attribute_navigation: Option, + pending_attribute_navigation: Option, pub(crate) pending_live_navigation_reflects_window_state: bool, } @@ -266,7 +287,7 @@ impl ChildBrowsingContextEntry { self.pending_attribute_navigation.is_some() } - pub(super) fn pending_attribute_navigation(&self) -> Option<&ChildNavigationTargetSnapshot> { + pub(super) fn pending_attribute_navigation(&self) -> Option<&PendingChildAttributeNavigation> { self.pending_attribute_navigation.as_ref() } 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 d47eb1f7ea6..90274d3a2c4 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 @@ -478,7 +478,12 @@ impl PageScriptHost { let target_url = Self::child_browsing_context_bootstrap_url(&attribute_bootstrap) .unwrap_or_else(|| self.document_url_for_child_context(handle)); - Some(self.snapshot_child_navigation_target(handle, &target_url)) + Some(PendingChildAttributeNavigation { + target_snapshot: self.snapshot_child_navigation_target(handle, &target_url), + blob_resource: crate::network_host::CapturedBlobResource::capture( + &target_url, + ), + }) }; let pending_attribute_permissions_policy = pending_attribute_bootstrap_commit.then(|| { diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs index 7da9f2c6505..3862456dacd 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs @@ -164,7 +164,11 @@ impl PageScriptHost { let scripting_enabled = self.child_browsing_context_scripting_enabled(handle); if entry.pending_attribute_bootstrap_commit() && let Some(snapshot) = self - .materialize_local_child_snapshot_for_bootstrap(handle, entry.attribute_bootstrap()) + .materialize_local_child_snapshot_for_bootstrap( + handle, + entry.attribute_bootstrap(), + None, + ) .into_snapshot() { if moli_url::is_about_blank(&snapshot.url) diff --git a/moli-renderer-v8/src/native_bridge/context_host/main_document_lifecycle.rs b/moli-renderer-v8/src/native_bridge/context_host/main_document_lifecycle.rs index 2237ced6742..2e3dd7dd222 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/main_document_lifecycle.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/main_document_lifecycle.rs @@ -270,9 +270,19 @@ impl PageScriptHost { } => { let binding = load_delay_binding .expect("runtime-script admission must own its load-delay lease"); - let admission = RuntimeScriptAdmission::from_boxed_payload(payload, binding); + let admission = RuntimeScriptAdmission::from_boxed_payload(payload, binding) + .capture_blob_resource(|| { + self.current_registered_window_execution_context_identity( + super::OwnerDispatchScope::Top, + ) + .and_then(|identity| { + crate::network_host::BlobUrlFetchEnvironment::for_identity( + self, identity, + ) + }) + }); if let Err(rejected) = runtime.publish_runtime_script_admission(admission) { - let (_, binding) = rejected.into_parts(); + let (_, binding, _) = rejected.into_parts(); let _ = self.release_main_document_script_load_delay(binding); runtime.cancel_runtime_script_start_admission(reservation); return Err( diff --git a/moli-renderer-v8/src/native_bridge/context_host/navigation.rs b/moli-renderer-v8/src/native_bridge/context_host/navigation.rs index ff7c71703d1..d23882a5f9e 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/navigation.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/navigation.rs @@ -60,6 +60,7 @@ pub(crate) struct PendingLocationNavigation { /// The complete browser request. URL-only callers use the GET wrapper; /// form POST callers retain raw bytes and their generated Content-Type. pub(crate) url: Url, + pub(crate) blob_resource: Option, pub(crate) request_method: String, pub(crate) request_body: Option, pub(crate) request_headers: Vec<(String, String)>, @@ -338,6 +339,7 @@ impl PageScriptHost { .as_ref() .map(RendererDocumentLifecycleJournalHandle::identity), runtime_command_cause: self.active_runtime_command_cause.clone(), + blob_resource: crate::network_host::CapturedBlobResource::capture(&url), url, request_method, request_body, @@ -372,6 +374,7 @@ impl PageScriptHost { .as_ref() .map(RendererDocumentLifecycleJournalHandle::identity), runtime_command_cause: None, + blob_resource: crate::network_host::CapturedBlobResource::capture(&url), url, request_method: "GET".to_owned(), request_body: None, diff --git a/moli-renderer-v8/src/network_host.rs b/moli-renderer-v8/src/network_host.rs index 1d34017e9d9..f090aacb48f 100644 --- a/moli-renderer-v8/src/network_host.rs +++ b/moli-renderer-v8/src/network_host.rs @@ -3,6 +3,7 @@ pub(crate) use async_fetch::next_redirect_url; mod async_fetch; mod beacon; mod bindings; +mod blob_resource; mod blob_url_entry; mod body; mod body_source; @@ -42,6 +43,8 @@ use moli_fetch::{Request, Response, observe_cookie_access_report_for_request}; pub(crate) use self::redirect_owner::{RedirectAdmission, RedirectOwner}; use crate::network::ResourceRequestClient; +pub use self::blob_resource::CapturedBlobResource; + pub(in crate::network_host) use self::blob_url_entry::BLOB_URL_ENTRY_SLOT; pub(crate) use self::blob_url_entry::{ BlobUrlFetchEnvironment, CapturedBlobUrl, blob_url_entry, set_blob_url_entry, diff --git a/moli-renderer-v8/src/network_host/blob_resource.rs b/moli-renderer-v8/src/network_host/blob_resource.rs new file mode 100644 index 00000000000..b0dc42efb3d --- /dev/null +++ b/moli-renderer-v8/src/network_host/blob_resource.rs @@ -0,0 +1,308 @@ +use std::sync::Arc; + +use moli_file_api::{ObjectUrlResourceData, ObjectUrlResourceEntry}; +use url::Url; + +use super::{BlobUrlFetchEnvironment, LocalUrlError}; + +/// A Blob URL resolved at resource initiation, including an unsuccessful lookup. +/// This value can cross an owner handoff without retaining a V8 object or +/// starting file/database I/O. It belongs to the request, not the URL store. +#[derive(Clone, Debug)] +pub struct CapturedBlobResource { + inner: Arc, +} + +#[derive(Debug)] +struct CapturedBlobResourceData { + url: Url, + entry: Option>, +} + +impl CapturedBlobResource { + pub(crate) fn capture(url: &Url) -> Option { + (url.scheme() == "blob").then(|| Self { + inner: Arc::new(CapturedBlobResourceData { + url: url.clone(), + entry: crate::blob::object_url_resource_entry(url.as_str()), + }), + }) + } + + pub(crate) fn capture_serialized(url: &str) -> Option { + if !url + .get(..5) + .is_some_and(|scheme| scheme.eq_ignore_ascii_case("blob:")) + { + return None; + } + Self::capture(&Url::parse(url).ok()?) + } + + pub(crate) fn for_fetch_environment( + self, + environment: Option<&BlobUrlFetchEnvironment>, + ) -> Self { + let allowed = self.inner.entry.as_ref().is_some_and(|entry| { + entry.access_key.as_ref().is_some_and(|key| { + environment.is_some_and(|environment| environment.allows(key, &self.inner.url)) + }) + }); + if allowed || self.inner.entry.is_none() { + return self; + } + Self { + inner: Arc::new(CapturedBlobResourceData { + url: self.inner.url.clone(), + entry: None, + }), + } + } + + pub(crate) fn matches(&self, url: &Url) -> bool { + blob_url_key(&self.inner.url) == blob_url_key(url) + } + + /// Consume this request's resolved backing, preserving method and range + /// checks. A matching failed resolution remains an error after revocation; + /// `None` means the caller is loading a different URL. + pub async fn response( + &self, + url: &Url, + method: &str, + headers: &[(String, String)], + ) -> Option> { + if !self.matches(url) { + return None; + } + Some( + self.read_response(url, method, headers) + .await + .map_err(Into::into), + ) + } + + pub(crate) fn response_blocking( + &self, + url: &Url, + ) -> Option> { + if !self.matches(url) { + return None; + } + Some((|| { + let entry = self + .inner + .entry + .as_ref() + .ok_or_else(|| LocalUrlError::BlobUnavailable { url: url.clone() })?; + let ObjectUrlResourceData::Blob { data, mime_type } = &entry.data else { + return Err(LocalUrlError::MediaSource); + }; + let bytes = data + .read_blocking() + .map_err(|_| LocalUrlError::BlobUnavailable { url: url.clone() })?; + super::browser_response::blob_response(url, &bytes, mime_type, &[]) + })()) + } + + async fn read_response( + &self, + url: &Url, + method: &str, + headers: &[(String, String)], + ) -> Result { + if method != "GET" { + return Err(LocalUrlError::BlobMethod { + method: method.to_owned(), + }); + } + let unavailable = || LocalUrlError::BlobUnavailable { url: url.clone() }; + let entry = self.inner.entry.as_ref().ok_or_else(unavailable)?; + let ObjectUrlResourceData::Blob { data, mime_type } = &entry.data else { + return Err(LocalUrlError::MediaSource); + }; + let bytes = match data.shared_bytes() { + Some(bytes) => bytes, + None => { + let (sender, receiver) = tokio::sync::oneshot::channel(); + data.read_all(Box::new(move |result| { + let _ = sender.send(result); + })); + Arc::from( + receiver + .await + .map_err(|_| unavailable())? + .map_err(|_| unavailable())?, + ) + } + }; + super::browser_response::blob_response(url, &bytes, mime_type, headers) + } +} + +fn blob_url_key(url: &Url) -> &str { + url.as_str() + .split_once('#') + .map_or(url.as_str(), |(url, _)| url) +} + +impl PartialEq for CapturedBlobResource { + fn eq(&self, other: &Self) -> bool { + if Arc::ptr_eq(&self.inner, &other.inner) { + return true; + } + if self.inner.url != other.inner.url { + return false; + } + // A Blob URL identifies one immutable store entry. Compare resolution + // and authority, never the potentially large or lazy byte source. + match (&self.inner.entry, &other.inner.entry) { + (None, None) => true, + (Some(left), Some(right)) if left.access_key == right.access_key => { + match (&left.data, &right.data) { + ( + ObjectUrlResourceData::Blob { + mime_type: left, .. + }, + ObjectUrlResourceData::Blob { + mime_type: right, .. + }, + ) => left == right, + (ObjectUrlResourceData::MediaSource, ObjectUrlResourceData::MediaSource) => { + true + } + _ => false, + } + } + _ => false, + } + } +} + +impl Eq for CapturedBlobResource {} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::{AtomicUsize, Ordering}; + + #[derive(Debug, Default)] + struct Source(AtomicUsize); + + impl moli_file_api::BlobSource for Source { + fn read(&self, range: std::ops::Range, complete: moli_file_api::BlobReadCompletion) { + self.0.fetch_add(1, Ordering::Relaxed); + complete(Ok([0, 128, 255, 65][range].to_vec())); + } + } + + fn resource(data: ObjectUrlResourceData) -> CapturedBlobResource { + CapturedBlobResource { + inner: Arc::new(CapturedBlobResourceData { + url: Url::parse("blob:https://example.test/captured").unwrap(), + entry: Some(ObjectUrlResourceEntry { + data, + access_key: None, + }), + }), + } + } + + #[tokio::test] + async fn lazy_blob_resources_preserve_method_range_mime_and_fragment_semantics() { + fn require_send_sync() {} + require_send_sync::(); + let source = Arc::new(Source::default()); + let captured = resource(ObjectUrlResourceData::Blob { + data: moli_file_api::BlobData::from_source(source.clone(), 4), + mime_type: "application/example".to_owned(), + }); + assert_eq!(captured, captured.clone()); + assert_eq!(source.0.load(Ordering::Relaxed), 0); + let url = &captured.inner.url; + for method in ["HEAD", "POST"] { + let error = captured + .response(url, method, &[]) + .await + .unwrap() + .unwrap_err(); + assert!(matches!( + error.downcast_ref::(), + Some(LocalUrlError::BlobMethod { .. }) + )); + } + let mut different = url.clone(); + different.set_query(Some("query")); + assert!(captured.response(&different, "GET", &[]).await.is_none()); + assert_eq!(source.0.load(Ordering::Relaxed), 0); + let mut fragment = url.clone(); + fragment.set_fragment(Some("fragment")); + let response = captured + .response( + &fragment, + "GET", + &[("Range".to_owned(), "bytes=1-2".to_owned())], + ) + .await + .unwrap() + .unwrap(); + assert_eq!(response.status, 206); + assert_eq!(response.body_bytes(), [128, 255]); + assert!( + response + .headers + .iter() + .any(|(name, value)| name.eq_ignore_ascii_case("content-type") + && value == b"application/example") + ); + assert_eq!(source.0.load(Ordering::Relaxed), 1); + } + + #[tokio::test] + async fn denied_or_unresolved_resources_fail_before_backing_io() { + let source = Arc::new(Source::default()); + let captured = resource(ObjectUrlResourceData::Blob { + data: moli_file_api::BlobData::from_source(source.clone(), 4), + mime_type: String::new(), + }) + .for_fetch_environment(None); + let url = &captured.inner.url; + let error = captured + .response(url, "GET", &[]) + .await + .unwrap() + .unwrap_err(); + assert!(matches!( + error.downcast_ref::(), + Some(LocalUrlError::BlobUnavailable { .. }) + )); + assert_eq!(source.0.load(Ordering::Relaxed), 0); + let missing = + CapturedBlobResource::capture(&Url::parse("blob:null/missing-resource").unwrap()) + .unwrap(); + assert!(missing.inner.entry.is_none()); + assert!( + missing + .response(&missing.inner.url, "GET", &[]) + .await + .unwrap() + .is_err() + ); + assert!(CapturedBlobResource::capture_serialized("https://example.test/blob").is_none()); + assert!(CapturedBlobResource::capture_serialized("data:text/plain,blob").is_none()); + } + + #[tokio::test] + async fn byte_resource_fetch_does_not_treat_media_source_as_a_blob() { + let captured = resource(ObjectUrlResourceData::MediaSource); + let error = captured + .response(&captured.inner.url, "GET", &[]) + .await + .unwrap() + .unwrap_err(); + assert!(matches!( + error.downcast_ref::(), + Some(LocalUrlError::MediaSource) + )); + } +} diff --git a/moli-renderer-v8/src/network_host/blob_url_entry.rs b/moli-renderer-v8/src/network_host/blob_url_entry.rs index e7381191981..24fcc2ce95b 100644 --- a/moli-renderer-v8/src/network_host/blob_url_entry.rs +++ b/moli-renderer-v8/src/network_host/blob_url_entry.rs @@ -16,11 +16,24 @@ pub(crate) struct BlobUrlFetchEnvironment { } impl BlobUrlFetchEnvironment { + pub(super) fn allows(&self, key: &crate::blob::ObjectUrlAccessKey, url: &url::Url) -> bool { + key == &self.access_key + || (key.same_partition(&self.access_key) + && self.top_level_creation_url.as_ref() == Some(url)) + } + pub(crate) fn for_window( host: &mut crate::native_bridge::PageScriptHost, binding: &crate::native_bridge::WindowExecutionContextBinding, ) -> Option { let identity = binding.resolve_identity(host)?; + Self::for_identity(host, identity) + } + + pub(crate) fn for_identity( + host: &mut crate::native_bridge::PageScriptHost, + identity: crate::native_bridge::WindowExecutionContextIdentity, + ) -> Option { let storage_context = host.storage_context_for_window_execution_context_identity(identity)?; let dispatch_scope = identity.dispatch_scope(); @@ -79,13 +92,10 @@ impl CapturedBlobUrl { pub(crate) fn for_environment(mut self, environment: Option<&BlobUrlFetchEnvironment>) -> Self { self.fetch_authorized = self.entry.as_ref().is_some_and(|entry| { environment.is_some_and(|environment| { - entry.access_key.as_ref().is_some_and(|key| { - key == &environment.access_key - // Fetch explicitly exempts a top-level document fetching - // its own creation URL from storage-key partition checks. - || (key.same_partition(&environment.access_key) - && environment.top_level_creation_url.as_ref() == Some(&self.url)) - }) + entry + .access_key + .as_ref() + .is_some_and(|key| environment.allows(key, &self.url)) }) }); if !self.fetch_authorized { diff --git a/moli-renderer-v8/src/planning.rs b/moli-renderer-v8/src/planning.rs index fa808dd5e68..866e0b84d95 100644 --- a/moli-renderer-v8/src/planning.rs +++ b/moli-renderer-v8/src/planning.rs @@ -576,6 +576,30 @@ pub(crate) async fn load_service_worker_aware_external_script_source_outcome_wit } } +pub(crate) async fn load_captured_blob_script_source_outcome( + script: &PreparedScript, + request_origin: &moli_url::WebOrigin, + resource: &crate::network_host::CapturedBlobResource, + document_character_set: Option<&str>, +) -> PreparedScriptSourceLoadOutcome { + match resource.response(&script.url, "GET", &[]).await { + Some(Ok(response)) => external_script_source_load_outcome_from_response( + script, + request_origin, + response.into(), + document_character_set, + ), + Some(Err(error)) => failed_external_script_source_load_outcome(format!( + "failed to fetch script `{}`: {error}", + script.url, + )), + None => failed_external_script_source_load_outcome(format!( + "failed to fetch script `{}`: captured Blob URL does not match", + script.url, + )), + } +} + fn local_or_unsupported_external_script_source_load_outcome( script: &PreparedScript, request_origin: &moli_url::WebOrigin, diff --git a/moli-renderer-v8/src/renderer_resource_scheduler.rs b/moli-renderer-v8/src/renderer_resource_scheduler.rs index 2d55bdf8c8d..79b85adf511 100644 --- a/moli-renderer-v8/src/renderer_resource_scheduler.rs +++ b/moli-renderer-v8/src/renderer_resource_scheduler.rs @@ -182,6 +182,7 @@ impl RendererResourceScheduler { let schedule = MainModuleFetchSchedule::Runtime(target); let csp_context = request.csp_context().cloned(); let fetch_context = request.fetch_context().cloned(); + let blob_resource = request.blob_resource().cloned(); let attribution = MainModuleFetchNetworkAttribution::new( service_worker.document_url.clone(), script.url.clone(), @@ -252,11 +253,19 @@ impl RendererResourceScheduler { // lookup. Keep its Service Worker response filtering and SRI checks, // but publish the result to the graph's single-module reservation. let outcome = tokio::select! { - outcome = crate::planning::load_service_worker_aware_external_script_source_outcome_with_request( - &script, &request_origin, &request_client, runner, None, request, - service_worker.browser_context_runtime, service_worker.client_id, - service_worker.document_url, None, - ) => outcome, + outcome = async { + if let Some(resource) = blob_resource { + crate::planning::load_captured_blob_script_source_outcome( + &script, &request_origin, &resource, None, + ).await + } else { + crate::planning::load_service_worker_aware_external_script_source_outcome_with_request( + &script, &request_origin, &request_client, runner, None, request, + service_worker.browser_context_runtime, service_worker.client_id, + service_worker.document_url, None, + ).await + } + } => outcome, _ = cancel_rx => { let error = "module root fetch cancelled".to_owned(); load.finish(); diff --git a/moli-renderer-v8/src/runtime/mod.rs b/moli-renderer-v8/src/runtime/mod.rs index af88082b051..095ea8d5107 100644 --- a/moli-renderer-v8/src/runtime/mod.rs +++ b/moli-renderer-v8/src/runtime/mod.rs @@ -67,6 +67,7 @@ pub use navigation_history::{ mod nested_main; mod owner; mod owner_deadline_index; +pub use crate::network_host::CapturedBlobResource as RendererBlobUrlResource; pub use auxiliary_page::RendererPendingAuxiliaryPage; pub(crate) use auxiliary_page::{ RendererAuxiliaryPageAllocator, RendererRelatedInitialEmptyPageInit, diff --git a/moli-renderer-v8/src/runtime/page_surface.rs b/moli-renderer-v8/src/runtime/page_surface.rs index 91d4fb608ea..5e81641482a 100644 --- a/moli-renderer-v8/src/runtime/page_surface.rs +++ b/moli-renderer-v8/src/runtime/page_surface.rs @@ -359,6 +359,7 @@ pub struct RendererDocumentSourcedTopLevelLocationNavigation { struct RendererTopLevelNavigationRequest { web_mcp_invocation: Option, url: String, + blob_resource: Option, request_method: String, request_body: Option, request_headers: Vec<(String, String)>, @@ -402,6 +403,7 @@ impl RendererDocumentSourcedTopLevelLocationNavigation { source_document, request: Box::new(RendererTopLevelNavigationRequest { web_mcp_invocation: None, + blob_resource: crate::network_host::CapturedBlobResource::capture_serialized(&url), url, request_method, request_body, @@ -431,6 +433,18 @@ impl RendererDocumentSourcedTopLevelLocationNavigation { self.navigation_initiator.as_ref() } + pub(crate) fn with_blob_resource( + mut self, + resource: Option, + ) -> Self { + self.request.blob_resource = resource; + self + } + + pub fn blob_resource(&self) -> Option<&super::RendererBlobUrlResource> { + self.request.blob_resource.as_ref() + } + pub fn initial_document_environment(&self) -> Option<&RendererCapturedDocumentEnvironment> { self.initial_document_environment.as_ref() } diff --git a/moli-renderer-v8/src/runtime/page_surface/popup_activation.rs b/moli-renderer-v8/src/runtime/page_surface/popup_activation.rs index d03342338f4..fc6b659e46a 100644 --- a/moli-renderer-v8/src/runtime/page_surface/popup_activation.rs +++ b/moli-renderer-v8/src/runtime/page_surface/popup_activation.rs @@ -212,6 +212,7 @@ pub struct RendererPopupActivationParts { pub initial_document_environment: Option, pub pending_auxiliary_page: Option, pub url: String, + pub blob_resource: Option, pub target_name: String, pub session_storage_store: Option, pub initial_empty_document_storage_key: Option, @@ -273,6 +274,7 @@ impl RendererPendingPopupActivation { document_response: None, initial_document_environment: None, pending_auxiliary_page: None, + blob_resource: crate::network_host::CapturedBlobResource::capture_serialized(&url), url, target_name, session_storage_store: None, @@ -304,6 +306,7 @@ impl RendererPendingPopupActivation { document_response: None, initial_document_environment: None, pending_auxiliary_page: None, + blob_resource: crate::network_host::CapturedBlobResource::capture_serialized(&url), url, target_name, session_storage_store: None, @@ -419,6 +422,7 @@ impl PartialEq for RendererPopupActivationParts { && self.initial_document_environment == other.initial_document_environment && self.pending_auxiliary_page == other.pending_auxiliary_page && self.url == other.url + && self.blob_resource == other.blob_resource && self.target_name == other.target_name && match (&self.session_storage_store, &other.session_storage_store) { (None, None) => true, diff --git a/moli-renderer-v8/src/runtime/page_vm/followed_navigation.rs b/moli-renderer-v8/src/runtime/page_vm/followed_navigation.rs index cd80c36a454..24cd846565b 100644 --- a/moli-renderer-v8/src/runtime/page_vm/followed_navigation.rs +++ b/moli-renderer-v8/src/runtime/page_vm/followed_navigation.rs @@ -70,11 +70,23 @@ pub(super) async fn load_followed_location_navigation( request_body: Option, request_headers: Vec<(String, String)>, browser_navigation_kind: BrowserNavigationRequestKind, + blob_resource: Option<&crate::network_host::CapturedBlobResource>, ) -> Result { debug!(%url, "starting pre-commit location navigation fetch"); - if let Some(response) = about_blank_navigation_response(&url).map(Ok).or_else(|| { + let local_response = match blob_resource { + Some(resource) => { + resource + .response(&url, &request_method, &request_headers) + .await + } + None => None, + } + .or_else(|| about_blank_navigation_response(&url).map(Ok)) + .or_else(|| { crate::network_host::local_url_response_result(&url, &request_method, &request_headers) - }) { + .map(|response| response.map_err(Into::into)) + }); + if let Some(response) = local_response { let response = response?; if matches!(response.status, 204 | 205) { return Ok(LoadedFollowedLocationNavigation::NoDocument); @@ -858,6 +870,7 @@ impl PageVm { request_body, request_headers, browser_navigation_kind, + pending.blob_resource.as_ref(), ) .await { @@ -1710,6 +1723,7 @@ mod tests { None, Vec::new(), BrowserNavigationRequestKind::Navigate, + None, ) .await; match result { @@ -1728,6 +1742,7 @@ mod tests { Some(bytes::Bytes::from_static(b"upload")), Vec::new(), BrowserNavigationRequestKind::Navigate, + None, ) .await .unwrap(); diff --git a/moli-renderer-v8/src/script_vm/child_frame_runtime.rs b/moli-renderer-v8/src/script_vm/child_frame_runtime.rs index 84b4fc773b6..355e072ad75 100644 --- a/moli-renderer-v8/src/script_vm/child_frame_runtime.rs +++ b/moli-renderer-v8/src/script_vm/child_frame_runtime.rs @@ -609,6 +609,7 @@ impl ScriptVm { pending.browser_navigation_kind, runtime_command_cause.clone(), ) + .with_blob_resource(pending.blob_resource) .with_navigation_history( pending.entry_seed.map(|seed| self.top_level_navigation_history().request(seed)), ) diff --git a/moli-renderer-v8/src/script_vm/native_module/document_scripts.rs b/moli-renderer-v8/src/script_vm/native_module/document_scripts.rs index ad343c75680..25e176cee06 100644 --- a/moli-renderer-v8/src/script_vm/native_module/document_scripts.rs +++ b/moli-renderer-v8/src/script_vm/native_module/document_scripts.rs @@ -980,6 +980,12 @@ impl ScriptVm { return RuntimeModuleScriptGraphStart::Started(actions); } }; + let resource = self + .document_runtime + .runtime_script_work_mut() + .dynamic_scripts + .take_blob_resource(dynamic_script_owner_id); + let job = job.with_blob_resource(resource); let continuation = continuation.with_resumed_graph_job(job); let advance = continuation.advance_graph(self); let actions = self.handle_runtime_module_script_graph_advance_for_dynamic_owner(advance); diff --git a/moli-renderer-v8/src/script_vm/post_parse_lifecycle.rs b/moli-renderer-v8/src/script_vm/post_parse_lifecycle.rs index c78a8aed728..625386bf62f 100644 --- a/moli-renderer-v8/src/script_vm/post_parse_lifecycle.rs +++ b/moli-renderer-v8/src/script_vm/post_parse_lifecycle.rs @@ -96,7 +96,7 @@ impl ScriptVm { ) { // Check CSP before starting the source fetch. A CORS or network failure // must not bypass CSP reporting by skipping the execution boundary. - let (payload, binding) = admission.into_parts(); + let (payload, binding, blob_resource) = admission.into_parts(); let payload = match payload { crate::host::RuntimeScriptAdmissionPayload::Script(script) if script.source_kind == crate::types::ScriptSourceKind::External @@ -141,7 +141,8 @@ impl ScriptVm { } payload => payload, }; - let admission = RuntimeScriptAdmission::from_boxed_payload(Box::new(payload), binding); + let admission = RuntimeScriptAdmission::from_boxed_payload(Box::new(payload), binding) + .with_blob_resource(blob_resource); let document_character_set = self.document_runtime.document_character_set().to_owned(); let service_worker_context = { let host = self._context_host.borrow();