diff --git a/Cargo.lock b/Cargo.lock index c4d498a4add..eb978e8b606 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3611,6 +3611,7 @@ dependencies = [ "fastrand", "flate2", "futures-util", + "h2", "half", "hashlink", "html5ever 0.40.1", @@ -3697,6 +3698,7 @@ dependencies = [ "percent-encoding", "png", "precomputed-hash", + "rcgen", "read-fonts", "selectors", "serde", @@ -3711,6 +3713,7 @@ dependencies = [ "tempfile", "thiserror", "tokio", + "tokio-rustls", "tracing", "typed-num", "url", diff --git a/moli-fetch/src/blocking/collectors.rs b/moli-fetch/src/blocking/collectors.rs index b58ff8ee7b4..d4225374e87 100644 --- a/moli-fetch/src/blocking/collectors.rs +++ b/moli-fetch/src/blocking/collectors.rs @@ -269,6 +269,7 @@ pub struct RawStreamingResponseCollector { started: bool, headers_finalized: bool, header_terminated: bool, + streaming_upload: bool, start_tx: Option>>, body_tx: Option>>, request_cookie_report: Option, @@ -747,6 +748,7 @@ impl RawStreamingResponseCollector { cancel_handle, response_progress, redirect_handling: RequestRedirectHandling::Follow, + streaming_upload: false, negotiated_http_version: None, network_request_extra_info: None, } @@ -774,6 +776,7 @@ impl RawStreamingResponseCollector { self.declared_identity_body_length = None; self.response_progress = response_progress; self.redirect_handling = RequestRedirectHandling::Follow; + self.streaming_upload = false; self.cache_body_writer = None; self.cache_plan = None; self.started = false; @@ -820,6 +823,10 @@ impl RawStreamingResponseCollector { self.redirect_handling = redirect_handling; } + pub(crate) fn set_streaming_upload(&mut self, streaming_upload: bool) { + self.streaming_upload = streaming_upload; + } + pub(crate) fn status_text(&self) -> Option<&str> { self.status_text.as_deref() } @@ -965,10 +972,9 @@ impl RawStreamingResponseCollector { self.maybe_emit_start(is_redirect_response); if self.started && (response_body_ends_at_headers(self.status) || is_redirect_response) { self.finish_streaming_body(); - // Preserve naturally complete connections. Only abort redirects - // with an unfinished body, or a protocol switch, instead of - // waiting for bytes that the head-only API discards. - if !body_complete_at_headers || self.status == 101 { + // Keep a single-use upload alive after early redirect headers. + // Its owner retires the unused response after transmission ends. + if (!body_complete_at_headers && !self.streaming_upload) || self.status == 101 { self.header_terminated = true; return false; } diff --git a/moli-fetch/src/http_authentication.rs b/moli-fetch/src/http_authentication.rs index d71997230e4..3771845f060 100644 --- a/moli-fetch/src/http_authentication.rs +++ b/moli-fetch/src/http_authentication.rs @@ -15,6 +15,9 @@ use crate::{FetchConfig, Request, RequestAuth, RequestAuthScheme, RequestAuthTar const MAX_ENTRIES: usize = 256; const MAX_PATHS: usize = 10; +pub(crate) const NON_REPLAYABLE_AUTHENTICATION_ERROR: &str = + "Cannot replay a streaming request body for HTTP authentication"; + pub(crate) type SharedHttpAuthenticationCache = Arc>; #[derive(Default)] @@ -400,7 +403,7 @@ impl HttpAuthenticationResponse { /// A comma can separate challenges or auth-params. Tokenize params using the /// shared HTTP field parser, preserving quoted commas/escapes and rejecting /// malformed Basic challenges. Chromium accepts a missing realm as empty. -fn basic_realm(headers: &[(String, Vec)]) -> Option { +pub(crate) fn basic_realm(headers: &[(String, Vec)]) -> Option { for (_, value) in headers .iter() .filter(|(name, _)| name.eq_ignore_ascii_case("www-authenticate")) diff --git a/moli-fetch/src/request.rs b/moli-fetch/src/request.rs index 71f272ddb36..3119b4ed4d7 100644 --- a/moli-fetch/src/request.rs +++ b/moli-fetch/src/request.rs @@ -1071,6 +1071,11 @@ impl Request { self.body_source_is_null || self.upload_body.is_some() } + /// The single-use transport body, when this request streams its upload. + pub fn upload_body(&self) -> Option<&crate::UploadBody> { + self.upload_body.as_ref() + } + /// Attach a single-use streaming body. Cloning a Request does not make /// this source replayable; redirects and retries must retain that fact. pub fn with_upload_body(mut self, body: crate::UploadBody) -> Self { @@ -1329,6 +1334,20 @@ impl Request { } } + /// A supported authentication challenge cannot restart an author stream. + /// Ordinary 401 responses and challenges outside the credentials mode + /// remain visible to the caller. + pub fn validate_authentication_response(&self, response: &crate::ResponseHead) -> Result<()> { + if self.body_source_is_null() + && response.status == 401 + && self.allows_credentials_for_url(&response.final_url) + && crate::http_authentication::basic_realm(&response.headers).is_some() + { + anyhow::bail!(crate::http_authentication::NON_REPLAYABLE_AUTHENTICATION_ERROR); + } + Ok(()) + } + /// Cookie policy uses the environment origin, independently of the URL used /// to produce Referer. Adapt tuple origins to the cookie layer's URL-based /// initiator API without replacing the request's referrer context. diff --git a/moli-fetch/src/runtime.rs b/moli-fetch/src/runtime.rs index 430d22245b4..c846d61c4c1 100644 --- a/moli-fetch/src/runtime.rs +++ b/moli-fetch/src/runtime.rs @@ -1509,6 +1509,7 @@ impl RuntimeOwner { job.response_progress.clone(), job.request.redirect_handling(), ); + collector.set_streaming_upload(job.request.upload_body.is_some()); collector.set_corp_origin(job.request.cross_origin_resource_policy_origin()); collector.set_client_hint_response_policy(prepared_request.response_policy); @@ -1636,12 +1637,19 @@ impl RuntimeOwner { }; if let Some(retry) = authentication_retry { let (request, url, cookie_context) = completion.context.request_context(); - let cookies = if request.allows_credentials_for_url(url) { + let restart = if request.allows_credentials_for_url(url) { store_response_cookies(&self.cookie_store, url, &retry.headers, cookie_context) } else { Ok(Vec::new()) - }; - match cookies { + } + .and_then(|cookies| { + anyhow::ensure!( + !request.body_source_is_null(), + crate::http_authentication::NON_REPLAYABLE_AUTHENTICATION_ERROR + ); + Ok(cookies) + }); + match restart { Ok(_) => { state.active_transfers = state.active_transfers.saturating_sub(1); // No collector received this private 401. Keep the same diff --git a/moli-fetch/src/tests/http_authentication.rs b/moli-fetch/src/tests/http_authentication.rs index 9364b7d22bc..62b1054bac2 100644 --- a/moli-fetch/src/tests/http_authentication.rs +++ b/moli-fetch/src/tests/http_authentication.rs @@ -108,6 +108,29 @@ async fn url_basic_authentication_challenges_then_reuses_session_on_all_transpor Ok(()) } +#[tokio::test] +async fn basic_authentication_cannot_replay_a_stream_source_snapshot() -> Result<()> { + for transport in ["buffered", "html", "raw"] { + let server = + ScriptedHttpServer::spawn(vec![challenge(), ScriptedResponse::ok("must not replay")]); + let client = FetchClient::new(&FetchConfig::default(), new_shared_browser_cookie_store()); + let mut request = + browser_request(&credentials(&server.url()))?.with_body_source_is_null(true); + request.method = "POST".into(); + request.body = Some(bytes::Bytes::from_static(b"payload")); + let error = tokio::time::timeout(Duration::from_secs(5), body(&client, request, transport)) + .await? + .expect_err("author source must not be replayed for authentication"); + assert!( + format!("{error:#}").contains("streaming request body"), + "{error:#}" + ); + assert_eq!(server.requests().len(), 1, "{transport}"); + server.shutdown(); + } + Ok(()) +} + #[tokio::test] async fn browser_url_credentials_respect_cors_and_credentials_mode() -> Result<()> { for (mode, credentials_mode, cross_origin) in [ diff --git a/moli-fetch/src/upload_body.rs b/moli-fetch/src/upload_body.rs index 26de79fdbfa..6f98a5ebf6b 100644 --- a/moli-fetch/src/upload_body.rs +++ b/moli-fetch/src/upload_body.rs @@ -109,6 +109,13 @@ impl fmt::Display for UploadBodyError { impl std::error::Error for UploadBodyError {} impl UploadBody { + /// Wait until the source reached EOF and curl transmitted its bytes. + pub fn transmission( + &self, + ) -> impl Future> + Send + 'static + use<> { + transmission(&self.0.shared) + } + /// Queue at most one chunk ahead of curl's current chunk. The producer /// awaits `send()` before pulling another chunk from its source. pub fn channel() -> (UploadBodySender, Self) { @@ -183,20 +190,7 @@ impl UploadBodySender { pub fn transmission( &self, ) -> impl Future> + Send + 'static + use<> { - let mut receiver = self.shared.transmission.subscribe(); - async move { - loop { - match *receiver.borrow_and_update() { - Transmission::Sent => return Ok(()), - Transmission::Failed => return Err(UploadBodyError::Closed), - Transmission::Pending => {} - } - receiver - .changed() - .await - .map_err(|_| UploadBodyError::Closed)?; - } - } + transmission(&self.shared) } pub fn send( @@ -234,6 +228,25 @@ impl UploadBodySender { } } +fn transmission( + shared: &Shared, +) -> impl Future> + Send + 'static + use<> { + let mut receiver = shared.transmission.subscribe(); + async move { + loop { + match *receiver.borrow_and_update() { + Transmission::Sent => return Ok(()), + Transmission::Failed => return Err(UploadBodyError::Closed), + Transmission::Pending => {} + } + receiver + .changed() + .await + .map_err(|_| UploadBodyError::Closed)?; + } + } +} + impl Drop for UploadBodySender { fn drop(&mut self) { if self.sender.take().is_some() { diff --git a/moli-renderer-v8/Cargo.toml b/moli-renderer-v8/Cargo.toml index 22112576e1e..9da26fd3421 100644 --- a/moli-renderer-v8/Cargo.toml +++ b/moli-renderer-v8/Cargo.toml @@ -151,6 +151,9 @@ v8 = { version = "152.2.0", features = ["v8_enable_pointer_compression"] } v8 = "152.2.0" [dev-dependencies] +h2 = "0.4" +rcgen = { version = "0.13", default-features = false, features = ["aws_lc_rs", "pem"] } +tokio-rustls = { version = "0.26", default-features = false, features = ["aws_lc_rs", "tls12"] } tempfile = "3.23.0" lopdf = { version = "0.45.0", default-features = false } fastrand = "2.3.0" diff --git a/moli-renderer-v8/src/native_bridge/context_host/resource_loading.rs b/moli-renderer-v8/src/native_bridge/context_host/resource_loading.rs index 0ce779efc21..99d65a41b6e 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/resource_loading.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/resource_loading.rs @@ -1868,7 +1868,8 @@ impl JsContextHost { AsyncSubresourceFetchEventTarget::Upload { internal_id } => { self.pending_window_xhr(internal_id).is_some() } - AsyncSubresourceFetchEventTarget::Completion { internal_id } + AsyncSubresourceFetchEventTarget::RequestUpload { internal_id } + | AsyncSubresourceFetchEventTarget::Completion { internal_id } | AsyncSubresourceFetchEventTarget::RedirectAdmission { internal_id } => { self.pending_subresource_fetches.contains_key(&internal_id) || self.running_subresource_fetches.contains_key(&internal_id) @@ -1893,6 +1894,53 @@ impl JsContextHost { } } + pub(crate) fn set_window_request_upload( + &mut self, + internal_id: u64, + upload: crate::network_host::RequestUpload, + ) { + let Some(pending) = self.pending_subresource_fetches.get_mut(&internal_id) else { + return; + }; + if let PendingSubresourceContinuation::Fetch(fetch) = &mut pending.continuation { + fetch.upload = Some(upload); + } + } + + pub(crate) fn window_request_upload( + &self, + internal_id: u64, + ) -> Option { + self.subresource_fetch_states() + .find(|pending| pending.info.internal_id == internal_id)? + .continuation + .window_fetch()? + .upload + .clone() + } + + pub(crate) fn window_request_upload_delivery( + &self, + internal_id: u64, + ) -> Option<( + crate::network_host::RequestUpload, + super::WindowExecutionContextBinding, + super::OwnerDispatchScope, + )> { + let pending = self + .subresource_fetch_states() + .find(|pending| pending.info.internal_id == internal_id)?; + let binding = pending.execution_context.window_realm_binding()?; + if pending.load.is_cancelled() || !binding.is_current(self) { + return None; + } + Some(( + pending.continuation.window_fetch()?.upload.clone()?, + binding.clone(), + pending.execution_context.dispatch_scope(), + )) + } + pub(crate) fn window_redirect_owner( &self, internal_id: u64, diff --git a/moli-renderer-v8/src/network_host.rs b/moli-renderer-v8/src/network_host.rs index 82ea382db9b..dac159b1f1a 100644 --- a/moli-renderer-v8/src/network_host.rs +++ b/moli-renderer-v8/src/network_host.rs @@ -22,6 +22,7 @@ mod preload; mod redirect_owner; mod request; mod request_scope; +mod request_upload; mod response; mod stylesheet_subresource; mod subresource; @@ -36,6 +37,7 @@ pub(crate) use headers::normalized_header_entry_or_throw; use moli_fetch::{Request, Response, observe_cookie_access_report_for_request}; pub(crate) use self::redirect_owner::{RedirectAdmission, RedirectOwner}; +pub(crate) use self::request_upload::RequestUpload; use crate::network::ResourceRequestClient; pub use self::blob_resource::CapturedBlobResource; diff --git a/moli-renderer-v8/src/network_host/async_fetch.rs b/moli-renderer-v8/src/network_host/async_fetch.rs index cb2827b1f4d..222f68022d3 100644 --- a/moli-renderer-v8/src/network_host/async_fetch.rs +++ b/moli-renderer-v8/src/network_host/async_fetch.rs @@ -267,6 +267,9 @@ impl ManualCorsRedirectState { previous.request_extra_info = request_extra_info.cloned(); } validate_actual_cors_response_head(&self.request, &head)?; + self.request + .validate_authentication_response(&head) + .map_err(format_network_error)?; if !is_redirect_status(head.status) { return Ok(ManualCorsRedirectTransition::FinalResponse); } @@ -375,6 +378,19 @@ impl ManualCorsRedirectState { } } +async fn wait_for_request_upload( + request: &Request, + cancel: &FetchCancelHandle, +) -> Result<(), String> { + if let Some(body) = request.upload_body() { + tokio::select! { + _ = cancel.cancelled() => return Err("Request upload was cancelled".to_owned()), + result = body.transmission() => result.map_err(|error| error.to_string())?, + } + } + Ok(()) +} + async fn fetch_browser_subresource_with_cors_redirect_checks( loader: ResourceRequestClient, request: Request, @@ -436,6 +452,7 @@ async fn fetch_browser_subresource_with_cors_redirect_checks( let network_extra_info_available = observed.request_observation().is_some(); let (mut response, hop_journal) = observed.into_parts_with_observation_journal(); journal.append(hop_journal); + wait_for_request_upload(redirects.request(), &cancel_handle).await?; let transition = redirects.advance( response.head(), network_extra_info_available, @@ -494,6 +511,7 @@ async fn fetch_browser_subresource_raw_stream_with_cors_redirect_checks( let network_extra_info_available = observed.request_observation().is_some(); let (mut response, hop_journal) = observed.into_parts_with_observation_journal(); journal.append(hop_journal); + wait_for_request_upload(redirects.request(), &cancel_handle).await?; let transition = redirects.advance( response.head(), network_extra_info_available, diff --git a/moli-renderer-v8/src/network_host/fetch/bindings.rs b/moli-renderer-v8/src/network_host/fetch/bindings.rs index bbc30c650dc..156699c86f8 100644 --- a/moli-renderer-v8/src/network_host/fetch/bindings.rs +++ b/moli-renderer-v8/src/network_host/fetch/bindings.rs @@ -55,7 +55,11 @@ fn window_fetch_abort_signal_callback<'s>( .and_then(|signal| host.abort_signal_reason(scope, signal)) .unwrap_or_else(|| abort_error_value(scope)); let reason_payload = structured_serialize_fetch_abort_reason(scope, reason); + let upload = host.window_request_upload(internal_id); let timing = host.abort_fetch_promise(scope, internal_id, reason, reason_payload); + if let Some(upload) = upload { + upload.stop(scope, reason); + } // The signal can belong to another realm. Report in the fetch's client // realm, after releasing the mutable request-host borrow. if let Some((context, entry)) = timing { @@ -195,7 +199,7 @@ pub(crate) fn window_fetch_callback<'s>( &blob_environment, ) }; - let prepared = match prepared { + let mut prepared = match prepared { Ok(prepared) => prepared, Err(error) => { // Request construction uses the receiver's settings, but Blink's @@ -207,7 +211,13 @@ pub(crate) fn window_fetch_callback<'s>( let scope = &mut v8::ContextScope::new(scope, relevant_context); if let Some(request_body_owner) = request_body_owner { let request_body_owner = v8::Local::new(scope, request_body_owner); - mark_request_input_body_used_for_fetch(scope, request_body_owner); + match mark_request_input_body_used_for_fetch(scope, request_body_owner) { + Ok(locked) => prepared.body_stream_locked = locked, + Err(error) => { + rv.set(make_rejected_promise(scope, &error.to_string()).into()); + return; + } + } } window_fetch_callback_in_relevant_realm(scope, prepared, signal, rv); } diff --git a/moli-renderer-v8/src/network_host/fetch/bindings/paths.rs b/moli-renderer-v8/src/network_host/fetch/bindings/paths.rs index 092226323ba..0a89e42768a 100644 --- a/moli-renderer-v8/src/network_host/fetch/bindings/paths.rs +++ b/moli-renderer-v8/src/network_host/fetch/bindings/paths.rs @@ -245,10 +245,10 @@ pub(super) fn resolve_local_fetch( Ok(Some(response)) } -pub(super) fn spawn_network_fetch( - scope: &mut v8::PinScope<'_, '_>, +pub(super) fn spawn_network_fetch<'s>( + scope: &mut v8::PinScope<'s, '_>, host: &mut JsContextHost, - resolver: v8::Local<'_, v8::PromiseResolver>, + resolver: v8::Local<'s, v8::PromiseResolver>, prepared: PreparedWindowFetchRequest, ) -> anyhow::Result { let loader = prepared.resource_loader.request_client().clone(); @@ -307,7 +307,7 @@ pub(super) fn spawn_network_fetch( &prepared.resolved_url, &prepared.method, &prepared.cors_preflight_request_headers, - false, + prepared.body_source_is_null, ) .is_some(); let internal_id = host @@ -342,6 +342,32 @@ pub(super) fn spawn_network_fetch( requires_preflight, ) .ok_or_else(|| anyhow::anyhow!("Failed to fetch"))?; + if prepared.body_source_is_null + && let Some(stream) = prepared.body_stream.as_ref() + { + let stream = v8::Local::new(scope, stream); + let completion = host.resource_completion_sender(); + let (upload, body) = crate::network_host::RequestUpload::new( + scope, + stream, + resolver, + prepared.body_stream_locked, + prepared.resource_loader.task_runner(), + cancel_handle.clone(), + move |result| { + let _ = completion.send_async_subresource_event( + AsyncSubresourceFetchEvent::RequestUploadReady { + internal_id, + result, + }, + ); + }, + ) + .map_err(|_| anyhow::anyhow!("Body stream is locked"))?; + request = request.with_upload_body(body); + host.set_window_request_upload(internal_id, upload.clone()); + upload.start(scope); + } if let Some(check) = host.window_fetch_redirect_check(internal_id) { request = request.with_redirect_check(check); } diff --git a/moli-renderer-v8/src/network_host/fetch/bindings/request.rs b/moli-renderer-v8/src/network_host/fetch/bindings/request.rs index 9249c87d4d2..ec9226e8c16 100644 --- a/moli-renderer-v8/src/network_host/fetch/bindings/request.rs +++ b/moli-renderer-v8/src/network_host/fetch/bindings/request.rs @@ -80,6 +80,7 @@ pub(super) struct PreparedWindowFetchRequest { pub(super) body: Option, pub(super) body_source_is_null: bool, pub(super) body_stream: Option>, + pub(super) body_stream_locked: bool, pub(super) request_mode: moli_fetch::RequestMode, pub(super) credentials_mode: moli_fetch::RequestCredentialsMode, pub(super) redirect_mode: moli_fetch::RequestRedirectMode, @@ -189,6 +190,7 @@ pub(super) fn prepare_window_fetch_request<'s>( body: parsed.body, body_source_is_null: parsed.body_source_is_null, body_stream: parsed.body_stream, + body_stream_locked: false, request_mode: parsed.request_mode, credentials_mode: parsed.credentials_mode, redirect_mode: parsed.redirect_mode, diff --git a/moli-renderer-v8/src/network_host/request/input.rs b/moli-renderer-v8/src/network_host/request/input.rs index 8f3b018a4a9..d3ed57630c7 100644 --- a/moli-renderer-v8/src/network_host/request/input.rs +++ b/moli-renderer-v8/src/network_host/request/input.rs @@ -192,18 +192,23 @@ impl RequestInputSnapshot { pub(crate) fn mark_request_input_body_used_for_fetch<'s>( scope: &mut v8::PinScope<'s, '_>, object: v8::Local<'s, v8::Object>, -) { +) -> Result { if !is_branded_request_object(scope, object) { - return; + return Ok(false); + } + if body_is_unusable(scope, object) { + return Err(request_body_already_used_error().into()); } let has_body = request_slot_value(scope, object, REQUEST_BODY_SLOT) .is_some_and(|body| !body.is_null_or_undefined()); if has_body { - if let Some(stream) = body_stream_object(scope, object) { - crate::context_bootstrap::begin_readable_stream_body_consumption(scope, stream); - } + let locked = body_stream_object(scope, object).is_some_and(|stream| { + crate::context_bootstrap::begin_readable_stream_body_consumption(scope, stream) + }); set_request_slot_bool(scope, object, REQUEST_BODY_USED_SLOT, true); + return Ok(locked); } + Ok(false) } pub(super) fn request_input_snapshot_for_constructor<'s>( diff --git a/moli-renderer-v8/src/network_host/request_upload.rs b/moli-renderer-v8/src/network_host/request_upload.rs new file mode 100644 index 00000000000..773451cd750 --- /dev/null +++ b/moli-renderer-v8/src/network_host/request_upload.rs @@ -0,0 +1,288 @@ +//! Author streams are read on their script owner, one snapshotted chunk at a +//! time. Only bytes and readiness notifications cross to the resource runner. + +use std::{ + cell::{Cell, RefCell}, + rc::{Rc, Weak}, + sync::Arc, +}; + +use moli_fetch::{FetchCancelHandle, UploadBody, UploadBodyError, UploadBodySender}; + +use crate::{ + context_bootstrap::{ + begin_readable_stream_body_consumption, cancel_readable_stream_for_fetch, + maybe_pull_stream, prepare_readable_stream_read_with_steps, + }, + network::RendererResourceTaskRunner, + util::{get_private_value, set_null_prototype, set_private_value, v8str}, +}; + +const NATIVE: &str = "__moliRequestUploadNative"; +const STREAM: &str = "__moliRequestUploadStream"; +const RESOLVER: &str = "__moliRequestUploadResolver"; +const STEPS: &str = "__moliRequestUploadSteps"; + +#[derive(Clone)] +pub(crate) struct RequestUpload(Rc); + +struct Upload { + consumer: v8::Global, + sender: RefCell>, + stopped: Cell, + reading: Cell, + runner: RendererResourceTaskRunner, + ready: Arc) + Send + Sync>, + cancel: FetchCancelHandle, +} + +impl RequestUpload { + pub(crate) fn new<'s>( + scope: &mut v8::PinScope<'s, '_>, + stream: v8::Local<'s, v8::Object>, + resolver: v8::Local<'s, v8::PromiseResolver>, + already_locked: bool, + runner: RendererResourceTaskRunner, + cancel: FetchCancelHandle, + ready: impl Fn(Result<(), UploadBodyError>) + Send + Sync + 'static, + ) -> Result<(Self, UploadBody), v8::Local<'s, v8::Value>> { + if !already_locked && !begin_readable_stream_body_consumption(scope, stream) { + return Err(v8::Exception::type_error( + scope, + v8str(scope, "Body stream is locked"), + )); + } + let consumer = v8::Object::new(scope); + set_null_prototype(scope, consumer); + set_private_value(scope, consumer, STREAM, stream.into()); + set_private_value(scope, consumer, RESOLVER, resolver.into()); + let steps = [ + v8::Function::builder(chunk) + .data(consumer.into()) + .build(scope), + v8::Function::builder(close) + .data(consumer.into()) + .build(scope), + v8::Function::builder(error) + .data(consumer.into()) + .build(scope), + ] + .map(|step| step.expect("request upload read step").into()); + let steps = v8::Array::new_with_elements(scope, &steps); + set_null_prototype(scope, steps.into()); + set_private_value(scope, consumer, STEPS, steps.into()); + let (sender, body) = UploadBody::channel(); + let upload = Self(Rc::new(Upload { + consumer: v8::Global::new(scope, consumer), + sender: RefCell::new(Some(sender)), + stopped: Cell::new(false), + reading: Cell::new(false), + runner, + ready: Arc::new(ready), + cancel, + })); + // V8 owns only a weak edge to the request. Removing its pending native + // continuation immediately drops the producer, including at retirement. + let mut weak = Box::new(Rc::downgrade(&upload.0)); + let external = v8::External::new(scope, (&mut *weak as *mut Weak).cast()); + set_private_value(scope, consumer, NATIVE, external.into()); + crate::v8_finalizer::track_context_owned_v8_finalizer(scope, consumer, move || drop(weak)); + Ok((upload, body)) + } + + /// Lock synchronously, but start reads after fetch has installed its abort + /// listener and released its mutable host/Worker borrow. + pub(crate) fn start(&self, scope: &mut v8::PinScope<'_, '_>) { + let consumer = v8::Local::new(scope, &self.0.consumer); + let callback = v8::Function::builder(read) + .data(consumer.into()) + .build(scope) + .expect("request upload start"); + scope.enqueue_microtask(callback); + } + + pub(crate) fn ready( + &self, + scope: &mut v8::PinScope<'_, '_>, + result: Result<(), UploadBodyError>, + ) { + if result.is_err() { + self.0.stopped.set(true); + self.0.sender.borrow_mut().take(); + // The network owner selects the terminal outcome. In particular, + // retiring the original upload for a 303 must not reject fetch. + return; + } + self.0.reading.set(false); + let consumer = v8::Local::new(scope, &self.0.consumer); + read_next(scope, consumer, &self.0); + } + + pub(crate) fn finish(&self, scope: &mut v8::PinScope<'_, '_>) { + let reason = v8::undefined(scope).into(); + self.stop(scope, reason); + } + + pub(crate) fn stop<'s>( + &self, + scope: &mut v8::PinScope<'s, '_>, + reason: v8::Local<'s, v8::Value>, + ) { + self.0.stopped.set(true); + self.0.sender.borrow_mut().take(); + let consumer = v8::Local::new(scope, &self.0.consumer); + let stream = v8::Local::::try_from( + get_private_value(scope, consumer, STREAM).expect("request upload stream"), + ) + .expect("request upload stream object"); + cancel_readable_stream_for_fetch(scope, stream, reason); + } +} + +fn native<'s>( + scope: &mut v8::PinScope<'s, '_>, + data: v8::Local<'s, v8::Value>, +) -> Option<(v8::Local<'s, v8::Object>, Rc)> { + // Teardown can free the context-owned weak allocation while author code + // still retains the stream and its internal read callbacks. + let (binding, _) = crate::common_runtime::CommonRuntimeBinding::from_scope(scope)?; + binding.callback_execution_context(scope)?; + let consumer = v8::Local::::try_from(data).ok()?; + let external = + v8::Local::::try_from(get_private_value(scope, consumer, NATIVE)?).ok()?; + // SAFETY: only new() installs this private typed weak allocation. Its + // guaranteed finalizer lives with this context; it owns no V8 globals. + let state = unsafe { &*external.value().cast::>() }.upgrade()?; + Some((consumer, state)) +} + +fn read_next<'s>( + scope: &mut v8::PinScope<'s, '_>, + consumer: v8::Local<'s, v8::Object>, + state: &Upload, +) { + if state.stopped.get() || state.cancel.is_cancelled() || state.reading.replace(true) { + return; + } + let stream = v8::Local::::try_from( + get_private_value(scope, consumer, STREAM).expect("request upload stream"), + ) + .expect("request upload stream object"); + let steps = v8::Local::::try_from( + get_private_value(scope, consumer, STEPS).expect("request upload read steps"), + ) + .expect("request upload read step array"); + let mut callbacks = Vec::with_capacity(3); + for index in 0..3 { + callbacks.push( + v8::Local::::try_from( + steps + .get_index(scope, index) + .expect("request upload read callback"), + ) + .expect("request upload read function"), + ); + } + let pull = prepare_readable_stream_read_with_steps( + scope, + stream, + callbacks[0], + callbacks[1], + callbacks[2], + ); + if pull && !state.stopped.get() { + maybe_pull_stream(scope, stream); + } +} + +fn read<'s>( + scope: &mut v8::PinScope<'s, '_>, + args: v8::FunctionCallbackArguments<'s>, + _: v8::ReturnValue<'_, v8::Value>, +) { + if let Some((consumer, state)) = native(scope, args.data()) { + read_next(scope, consumer, &state); + } +} + +fn fail<'s>( + scope: &mut v8::PinScope<'s, '_>, + consumer: v8::Local<'s, v8::Object>, + state: Rc, + reason: v8::Local<'s, v8::Value>, +) { + if state.stopped.replace(true) { + return; + } + state.cancel.cancel(); + state.sender.borrow_mut().take(); + let value = + get_private_value(scope, consumer, RESOLVER).expect("request upload fetch resolver"); + let object = v8::Local::::try_from(value).expect("request upload resolver object"); + // SAFETY: new() stores the intrinsic PromiseResolver in this private slot. + let resolver = unsafe { v8::Local::::cast_unchecked(object) }; + resolver.reject(scope, reason); + RequestUpload(state).stop(scope, reason); +} + +fn chunk<'s>( + scope: &mut v8::PinScope<'s, '_>, + args: v8::FunctionCallbackArguments<'s>, + _: v8::ReturnValue<'_, v8::Value>, +) { + let Some((consumer, state)) = native(scope, args.data()) else { + return; + }; + if state.stopped.get() { + return; + } + let Ok(chunk) = v8::Local::::try_from(args.get(0)) else { + let reason = v8::Exception::type_error(scope, v8str(scope, "Failed to fetch")); + fail(scope, consumer, state, reason); + return; + }; + // Copy the represented view before another pull can mutate or detach it. + let mut bytes = vec![0; chunk.byte_length()]; + let count = chunk.copy_contents(&mut bytes); + bytes.truncate(count); + let Some(send) = state + .sender + .borrow() + .as_ref() + .map(|sender| sender.send(bytes.into())) + else { + return; + }; + let ready = state.ready.clone(); + state.runner.spawn(async move { + ready(send.await); + }); +} + +fn close<'s>( + scope: &mut v8::PinScope<'s, '_>, + args: v8::FunctionCallbackArguments<'s>, + _: v8::ReturnValue<'_, v8::Value>, +) { + let Some((_, state)) = native(scope, args.data()) else { + return; + }; + if state.stopped.replace(true) { + return; + } + let sender = state.sender.borrow_mut().take(); + if let Some(sender) = sender { + sender.finish(); + } +} + +fn error<'s>( + scope: &mut v8::PinScope<'s, '_>, + args: v8::FunctionCallbackArguments<'s>, + _: v8::ReturnValue<'_, v8::Value>, +) { + if let Some((consumer, state)) = native(scope, args.data()) { + let reason = v8::Exception::type_error(scope, v8str(scope, "Failed to fetch")); + fail(scope, consumer, state, reason); + } +} diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/fetch_xhr.rs b/moli-renderer-v8/src/runtime/page_vm/tests/fetch_xhr.rs index a3ae4e0d383..65c61b1e770 100644 --- a/moli-renderer-v8/src/runtime/page_vm/tests/fetch_xhr.rs +++ b/moli-renderer-v8/src/runtime/page_vm/tests/fetch_xhr.rs @@ -698,5 +698,6 @@ fn check_blob_fetch_and_xhr_methods_in_window_and_worker( mod event_source; mod fetch; mod header_names; +mod request_upload; mod workers; mod xhr; diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/fetch_xhr/request_upload.rs b/moli-renderer-v8/src/runtime/page_vm/tests/fetch_xhr/request_upload.rs new file mode 100644 index 00000000000..05f683ab48f --- /dev/null +++ b/moli-renderer-v8/src/runtime/page_vm/tests/fetch_xhr/request_upload.rs @@ -0,0 +1,413 @@ +use super::*; +use crate::runtime::ResourceRequestClient; +use std::sync::Arc; +use tokio::sync::watch; +use tokio_rustls::rustls::{ + ServerConfig, + pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer}, +}; + +#[derive(Default)] +struct Wire { + bytes: Vec, + headers: http::HeaderMap, + ended: bool, +} + +#[derive(Clone)] +struct Signals { + headers: watch::Sender, + first: watch::Sender, + finished: watch::Sender, + release: watch::Sender, + stop: watch::Sender, +} + +async fn wait(signal: &watch::Sender) -> anyhow::Result<()> { + let mut receiver = signal.subscribe(); + while !*receiver.borrow_and_update() { + receiver.changed().await?; + } + Ok(()) +} + +struct UploadServer { + url: Url, + signals: Signals, + wire: Arc>, + task: Option>>, +} + +impl UploadServer { + async fn new(blocked: bool, status: u16) -> anyhow::Result { + let listener = TcpListener::bind("127.0.0.1:0").await?; + let url = Url::parse(&format!("https://{}/", listener.local_addr()?))?; + let cert = rcgen::generate_simple_self_signed(vec!["localhost".to_owned()])?; + let mut tls = ServerConfig::builder() + .with_no_client_auth() + .with_single_cert( + vec![CertificateDer::from(cert.cert.der().to_vec())], + PrivateKeyDer::from(PrivatePkcs8KeyDer::from(cert.key_pair.serialize_der())), + )?; + tls.alpn_protocols = vec![b"h2".to_vec()]; + let acceptor = tokio_rustls::TlsAcceptor::from(Arc::new(tls)); + let signals = Signals { + headers: watch::channel(false).0, + first: watch::channel(false).0, + finished: watch::channel(false).0, + release: watch::channel(!blocked).0, + stop: watch::channel(false).0, + }; + let wire = Arc::new(parking_lot::Mutex::new(Wire::default())); + let shared = wire.clone(); + let events = signals.clone(); + let task = tokio::spawn(async move { + let mut stop = events.stop.subscribe(); + let mut connections = tokio::task::JoinSet::new(); + while !*stop.borrow_and_update() { + let socket = tokio::select! { + _ = stop.changed() => break, + accepted = listener.accept() => accepted?.0, + }; + let events = events.clone(); + let wire = shared.clone(); + let acceptor = acceptor.clone(); + connections.spawn(async move { + let socket = acceptor.accept(socket).await?; + let mut release = events.release.subscribe(); + let mut blocked = !*release.borrow_and_update(); + let mut builder = h2::server::Builder::new(); + builder.initial_window_size(if blocked { 0 } else { 65_535 }); + let mut connection = builder.handshake(socket).await?; + let mut stop = events.stop.subscribe(); + let mut requests = tokio::task::JoinSet::new(); + while !*stop.borrow_and_update() { + let request = tokio::select! { + _ = stop.changed() => break, + _ = release.changed(), if blocked => { + connection.set_initial_window_size(1_048_576)?; + blocked = false; + continue; + } + request = connection.accept() => request, + }; + let Some(Ok((request, mut response))) = request else { + break; + }; + let events = events.clone(); + let wire = wire.clone(); + requests.spawn(async move { + let path = request.uri().path(); + let origin = request + .headers() + .get("origin") + .cloned() + .unwrap_or_else(|| http::HeaderValue::from_static("*")); + let head = http::Response::builder() + .header("cache-control", "no-store") + .header("access-control-allow-origin", origin) + .header("access-control-allow-credentials", "true") + .header("access-control-allow-methods", "POST") + .header("access-control-allow-headers", "authorization"); + if request.method() == http::Method::OPTIONS { + response.send_response(head.status(204).body(())?, true)?; + } else if path == "/upload" { + wire.lock().headers = request.headers().clone(); + events.headers.send_replace(true); + let mut head = head.status(status); + if matches!(status, 301 | 302 | 303 | 307 | 308) { + head = head.header("location", "/done"); + } + if status == 401 { + head = match request.uri().query() { + Some("basic") => head + .header("www-authenticate", "Basic realm=\"upload\""), + Some("bearer") => head + .header("www-authenticate", "Bearer realm=\"upload\""), + _ => head, + }; + } + let mut send = response.send_response(head.body(())?, false)?; + // End the response before the request ends. The browser + // must keep uploading and enforce its half-duplex barrier. + send.send_data(bytes::Bytes::from_static(b"uploaded"), true)?; + let mut body = request.into_body(); + while let Some(chunk) = body.data().await { + let chunk = chunk?; + wire.lock().bytes.extend_from_slice(&chunk); + if !chunk.is_empty() { + events.first.send_replace(true); + } + body.flow_control().release_capacity(chunk.len())?; + } + wire.lock().ended = true; + events.finished.send_replace(true); + } else { + match path { + "/first" => wait(&events.first).await?, + "/headers" => wait(&events.headers).await?, + "/finished" => wait(&events.finished).await?, + "/release" => { + events.release.send_replace(true); + } + _ => {} + } + let head = head.status(200).body(())?; + let mut send = response.send_response(head, false)?; + send.send_data(bytes::Bytes::from_static(b"uploaded"), true)?; + } + Ok::<_, anyhow::Error>(()) + }); + } + requests.abort_all(); + while requests.join_next().await.is_some() {} + Ok::<_, anyhow::Error>(()) + }); + } + connections.abort_all(); + while connections.join_next().await.is_some() {} + Ok(()) + }); + Ok(Self { + url, + signals, + wire, + task: Some(task), + }) + } + + async fn shutdown(mut self) { + self.signals.stop.send_replace(true); + self.task.take().unwrap().await.unwrap().unwrap(); + } +} + +impl Drop for UploadServer { + fn drop(&mut self) { + self.signals.stop.send_replace(true); + } +} + +async fn window_probe(server: &UploadServer, script: &str) -> serde_json::Value { + let mut config = FetchConfig::default(); + // This fixture uses a fresh self-signed loopback certificate. + config.set_tls_verify_host(false); + config.set_http_no_proxy(Some("*".to_owned())); + let owner = ResourceRequestClient::new(&config).unwrap(); + let mut page = test_page_vm_with_loader_and_document_url( + &owner, + Vec::new(), + server.url.join("page").unwrap(), + ); + page.retain_standalone_request_client_owner_for_test(owner); + let executor = page.local_executor.clone(); + let source = format!( + r#" + (async () => {{ {script} }})().then( + result => globalThis.__uploadResult = result, + error => globalThis.__uploadResult = {{failure:String(error)}} + ).finally(() => globalThis.__uploadDone = true); + "# + ); + let result = executor + .run(async move { + page.vm_mut().eval(&source)?; + drive_websocket_until_done( + &mut page, + "String(globalThis.__uploadDone === true)", + "H2 author stream upload should complete", + ) + .await?; + page.vm_mut() + .eval("JSON.stringify(globalThis.__uploadResult)") + }) + .await + .unwrap(); + serde_json::from_str(&result).unwrap() +} + +#[tokio::test] +async fn local_responses_do_not_consume_author_streams_in_window_or_worker() { + run_page_vm_async_test(async { + let mut page = test_page_vm_with_document_url(Url::parse("https://example.com/").unwrap()); + let executor = page.local_executor.clone(); + let result = executor.run(async move { + page.vm_mut().eval(r#" + (async()=>{ + async function check(){ + const rows=[]; + for(const stage of ['start','pull']){ + const body=new ReadableStream({[stage](c){c.error(new Error('source'));}}); + const response=await fetch('data:text/plain,local',{method:'POST',body,duplex:'half'}); + rows.push({stage,status:response.status,text:await response.text(),locked:body.locked}); + } + return rows; + } + const window=await check(); + const url=URL.createObjectURL(new Blob(['onmessage=async()=>postMessage(await ('+check.toString()+')());'])); + const worker=new Worker(url); + try { + const dedicated=await new Promise((resolve,reject)=>{ + worker.onmessage=e=>resolve(e.data); + worker.onerror=e=>reject(new Error(e.message)); + worker.postMessage(1); + }); + return {window,dedicated}; + } finally { + worker.terminate();URL.revokeObjectURL(url); + } + })().then(value=>globalThis.__uploadResult=value, + error=>globalThis.__uploadResult={failure:String(error)}) + .finally(()=>globalThis.__uploadDone=true); + "#)?; + drive_websocket_until_done(&mut page,"String(globalThis.__uploadDone===true)","local responses should ignore author upload streams").await?; + page.vm_mut().eval("JSON.stringify(globalThis.__uploadResult)") + }).await.unwrap(); + let rows = serde_json::json!([ + {"stage":"start","status":200,"text":"local","locked":false}, + {"stage":"pull","status":200,"text":"local","locked":false} + ]); + assert_eq!(serde_json::from_str::(&result).unwrap(),serde_json::json!({"window":rows,"dedicated":rows})); + }).await; +} + +#[tokio::test] +async fn window_upload_sends_binary_chunks_before_eof_and_waits_for_transmission() { + run_page_vm_async_test(async { + for status in [200, 301, 302, 303, 307, 308] { + let server = UploadServer::new(false, status).await.unwrap(); + let result = window_probe(&server, r#" + const backing = new Uint8Array([88,0,255,65,66,99]); + const view = backing.subarray(1,5); + let controller; + const stream = new ReadableStream({ + start(c){controller=c;c.enqueue(view);}, + pull(){backing.fill(123);} + }); + ReadableStream.prototype.getReader = () => {throw new Error('public getReader');}; + ReadableStreamDefaultReader.prototype.read = () => {throw new Error('public read');}; + const request = new Request('/upload',{method:'POST',body:stream,duplex:'half'}); + let settled = false; + const promise = fetch(request).then( + async r => {settled=true;return {ok:true,status:r.status,text:await r.text()};}, + e => {settled=true;return {ok:false,type:e.name};} + ); + const locked = stream.locked && request.bodyUsed; + await (await fetch('/first')).text(); + if(settled) throw new Error('Response settled before upload EOF: '+JSON.stringify(await promise)); + controller.enqueue(new Uint8Array()); + controller.enqueue(new Uint8Array(196613).fill(7)); + controller.close(); + const result = await promise; + await (await fetch('/finished')).text(); + return {locked,result}; + "#).await; + let expected = if matches!(status, 200 | 303) { + serde_json::json!({"locked":true,"result":{"ok":true,"status":200,"text":"uploaded"}}) + } else { + serde_json::json!({"locked":true,"result":{"ok":false,"type":"TypeError"}}) + }; + assert_eq!(result, expected, "early status {status}"); + { + let wire = server.wire.lock(); + assert!(wire.ended); + assert_eq!(&wire.bytes[..4], &[0,255,65,66]); + assert_eq!(wire.bytes.len(), 4 + 196_613); + assert!(wire.bytes[4..].iter().all(|byte| *byte == 7)); + assert!(wire.headers.get("content-length").is_none()); + assert!(wire.headers.get("content-type").is_none()); + } + server.shutdown().await; + } + }).await; +} + +#[tokio::test] +async fn window_upload_applies_transport_backpressure_to_internal_stream_reads() { + run_page_vm_async_test(async { + let server = UploadServer::new(true, 200).await.unwrap(); + let result = window_probe( + &server, + r#" + let pulls=0, settled=false; + const body = new ReadableStream({pull(c){ + c.enqueue(new Uint8Array(65536).fill(1)); + if(++pulls===64)c.close(); + }},{highWaterMark:0}); + const promise = fetch('/upload',{method:'POST',body,duplex:'half'}) + .then(async r => {settled=true;return r.text();}); + await (await fetch('/headers')).text(); + const bounded = pulls < 64 && !settled; + await (await fetch('/release')).text(); + const text = await promise; + await (await fetch('/finished')).text(); + return {bounded,pulls,text}; + "#, + ) + .await; + assert_eq!( + result, + serde_json::json!({"bounded":true,"pulls":64,"text":"uploaded"}) + ); + assert_eq!(server.wire.lock().bytes.len(), 64 * 65536); + assert!(server.wire.lock().ended); + server.shutdown().await; + }) + .await; +} + +#[tokio::test] +async fn window_upload_authentication_depends_on_challenge_and_credentials() { + run_page_vm_async_test(async { + let server = UploadServer::new(false, 401).await.unwrap(); + let result = window_probe(&server, r#" + const failures=[]; + for(const cross of [false,true])for(const challenge of ['basic','bearer','none']) + for(const credentials of ['omit','same-origin','include']) + for(const streaming of [false,true])for(const authorization of [false,true]){ + const url=new URL('/upload?'+challenge,location.href); + if(cross)url.hostname='localhost'; + const bytes=new Uint8Array([0,255,65,66]); + const body=streaming?new ReadableStream({start(c){c.enqueue(bytes);c.close();}}):bytes; + const blocked=streaming && challenge==='basic' && + (credentials==='include' || credentials==='same-origin'&&!cross); + let correct; + try { + const response=await fetch(url,{method:'POST',body,duplex:'half',credentials, + headers:authorization?{Authorization:'Basic dXNlcjpwYXNz'}:{}}); + correct=!blocked && response.status===401 && await response.text()==='uploaded'; + } catch(e){correct=blocked && e instanceof TypeError;} + if(!correct)failures.push({cross,challenge,credentials,streaming,authorization}); + } + return failures; + "#).await; + assert_eq!(result, serde_json::json!([])); + server.shutdown().await; + }).await; +} + +#[tokio::test] +async fn window_upload_maps_stream_failures_and_preserves_abort_identity() { + run_page_vm_async_test(async { + let server = UploadServer::new(false, 200).await.unwrap(); + let result = window_probe(&server, r#" + const checks=[]; + for(const stage of ['start','pull'])for(const reason of [new Error('source'),{source:true},null,undefined,new DOMException('source','AbortError')]){ + const stream = new ReadableStream({[stage](c){c.error(reason);}}); + try {await fetch('/upload',{method:'POST',body:stream,duplex:'half'});checks.push(false);} + catch(e){checks.push(e instanceof TypeError && e!==reason && stream.locked);} + } + for(const reason of [{abort:true},'abort marker',null]){ + let cancelled, calls=0; + const body=new ReadableStream({cancel(value){cancelled=value;calls++;}}); + const controller=new AbortController(); + const promise=fetch('/upload',{method:'POST',body,duplex:'half',signal:controller.signal}).catch(e=>e); + controller.abort(reason); + checks.push(await promise===reason && cancelled===reason && calls===1); + } + return checks; + "#).await; + assert_eq!(result, serde_json::json!(vec![true; 13])); + server.shutdown().await; + }).await; +} diff --git a/moli-renderer-v8/src/script_vm/subresource_fetch.rs b/moli-renderer-v8/src/script_vm/subresource_fetch.rs index 1cbd97c3127..25a3c9d537a 100644 --- a/moli-renderer-v8/src/script_vm/subresource_fetch.rs +++ b/moli-renderer-v8/src/script_vm/subresource_fetch.rs @@ -774,6 +774,12 @@ fn async_subresource_trace_fields_for_event( event: &AsyncSubresourceFetchEvent, ) -> AsyncSubresourceTraceFields { match event { + AsyncSubresourceFetchEvent::RequestUploadReady { internal_id, .. } => { + AsyncSubresourceTraceFields { + internal_id: Some(*internal_id), + ..Default::default() + } + } AsyncSubresourceFetchEvent::RedirectAdmission { internal_id, .. } => { AsyncSubresourceTraceFields { event_kind: Some("redirect_admission"), diff --git a/moli-renderer-v8/src/script_vm/subresource_fetch/fetch_dispatch.rs b/moli-renderer-v8/src/script_vm/subresource_fetch/fetch_dispatch.rs index 618eed9105a..9ce1f993f1d 100644 --- a/moli-renderer-v8/src/script_vm/subresource_fetch/fetch_dispatch.rs +++ b/moli-renderer-v8/src/script_vm/subresource_fetch/fetch_dispatch.rs @@ -736,6 +736,7 @@ impl ScriptVm { if let Some(status_text) = response_status_text { head.status_text = Some(status_text); } + let upload = fetch.upload.clone(); let resolver = fetch .into_resolver() .expect("detached keepalive completion is handled before V8 entry"); @@ -759,6 +760,7 @@ impl ScriptVm { response_filter, ); resolver.resolve(scope, response_obj.into()); + if let Some(upload) = upload { upload.finish(scope); } } PendingSubresourceContinuation::Xhr { xhr, timing, .. } => { let body_size = observable_response.body_bytes().len(); @@ -975,6 +977,7 @@ impl ScriptVm { } match pending.continuation { PendingSubresourceContinuation::Fetch(fetch) => { + let upload = fetch.upload.clone(); let resolver = fetch .into_resolver() .expect("detached keepalive failure is handled before V8 entry"); @@ -983,6 +986,7 @@ impl ScriptVm { .map(|message| v8::Exception::type_error(scope, message)) .unwrap_or_else(|| v8::undefined(scope).into()); resolver.reject(scope, exception); + if let Some(upload) = upload { upload.stop(scope, exception); } } PendingSubresourceContinuation::Xhr { xhr, .. } => { let xhr = v8::Local::new(scope, &xhr); diff --git a/moli-renderer-v8/src/script_vm/subresource_fetch/streaming.rs b/moli-renderer-v8/src/script_vm/subresource_fetch/streaming.rs index 3ab51b5f4ae..e45e443dd16 100644 --- a/moli-renderer-v8/src/script_vm/subresource_fetch/streaming.rs +++ b/moli-renderer-v8/src/script_vm/subresource_fetch/streaming.rs @@ -1,6 +1,7 @@ use super::*; use crate::page_task_queue::ContentSecurityPolicyViolationEventTask; use crate::script_vm::security_policy::ContentSecurityPolicyViolationBodyExecution; +use crate::types::AsyncSubresourceFetchEventTarget; // Header policy checks have accepted this response. A later body error // retains its HTTP status and body information for both Fetch and XHR timing. @@ -423,6 +424,10 @@ impl ScriptVm { let trace_fields = async_subresource_trace_fields_for_event(&event); trace_async_subresource_stage("async_subresource_event_start", trace_fields, trace_started); let result = match event { + AsyncSubresourceFetchEvent::RequestUploadReady { + internal_id, + result, + } => self.apply_request_upload_ready(internal_id, result), AsyncSubresourceFetchEvent::RedirectAdmission { internal_id, admission, @@ -481,6 +486,31 @@ impl ScriptVm { trace_async_subresource_stage("async_subresource_event_done", trace_fields, trace_started); result } + fn apply_request_upload_ready( + &mut self, + internal_id: u64, + result: Result<(), moli_fetch::UploadBodyError>, + ) -> Result { + let context_host = self._context_host.clone(); + self.renderer_document_isolate + .with_entered_renderer_document_isolate(|isolate| { + let scope = pin!(v8::HandleScope::new(isolate)); + let scope = &mut scope.init(); + let Some((upload, binding, dispatch)) = context_host + .borrow() + .window_request_upload_delivery(internal_id) + else { + return Ok(AsyncSubresourceFetchBodyActivity::NoWindowRealmEntered); + }; + let context = v8::Local::new(scope, binding.context_global()); + let scope = &mut v8::ContextScope::new(scope, context); + let previous = enter_subresource_owner_async_scope(&context_host, scope, dispatch); + upload.ready(scope, result); + defer_subresource_owner_async_scope(&context_host, scope, dispatch, previous); + Ok(AsyncSubresourceFetchBodyActivity::WindowRealmEntered) + }) + } + fn complete_window_request_document_abort( &mut self, internal_id: u64, @@ -960,6 +990,7 @@ impl ScriptVm { .map(|message| v8::Exception::type_error(scope, message)) .unwrap_or_else(|| v8::undefined(scope).into()); resolver.reject(scope, exception); + if let Some(upload) = fetch.upload.as_ref() { upload.stop(scope, exception); } } PendingSubresourceContinuation::Xhr { xhr, .. } => { let xhr = v8::Local::new(scope, xhr); @@ -1207,6 +1238,7 @@ impl ScriptVm { let mut event_source_parser = None; let mut event_source_to_open = None; + let mut fetch_to_resolve = None; match &pending.continuation { PendingSubresourceContinuation::Fetch(fetch) => { let resolver = fetch @@ -1231,7 +1263,7 @@ impl ScriptVm { started.body_source_id, response_filter, ); - resolver.resolve(scope, response_obj.into()); + fetch_to_resolve = Some((resolver, response_obj, fetch.upload.clone())); } PendingSubresourceContinuation::Image { image_handle, @@ -1352,6 +1384,19 @@ impl ScriptVm { xhr_response: None, }, ); + if let Some((resolver, response, upload)) = fetch_to_resolve { + if let Some(upload) = upload { + upload.finish(scope); + } + if self._context_host.borrow().async_subresource_fetch_event_target_is_current( + AsyncSubresourceFetchEventTarget::StreamingFinish { + internal_id: started.internal_id, + body_source_id: started.body_source_id, + } + ) { + resolver.resolve(scope, response.into()); + } + } if let Some(event_source) = event_source_to_open { crate::network_host::open_event_source_connection( scope, diff --git a/moli-renderer-v8/src/types.rs b/moli-renderer-v8/src/types.rs index b81f8c378e3..5b4650b2ce4 100644 --- a/moli-renderer-v8/src/types.rs +++ b/moli-renderer-v8/src/types.rs @@ -378,6 +378,7 @@ impl PendingSubresourceContinuation { pub(super) struct PendingWindowFetchContinuation { pub(super) timing: crate::context_bootstrap::FetchResourceTiming, + pub(crate) upload: Option, promise: PendingWindowFetchPromise, keepalive: bool, connect_policy: crate::document_runtime::DocumentConnectPolicySnapshot, @@ -403,6 +404,7 @@ impl PendingWindowFetchContinuation { ) -> Box { Box::new(Self { promise: PendingWindowFetchPromise::Active(resolver), + upload: None, timing: crate::context_bootstrap::FetchResourceTiming::new(), keepalive, redirect_csp_state: crate::network_host::FetchCspRedirectState::new(&connect_policy), @@ -996,6 +998,9 @@ pub(super) struct AsyncSubresourceStreamingFinished { /// another kind of resident. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub(crate) enum AsyncSubresourceFetchEventTarget { + RequestUpload { + internal_id: u64, + }, RedirectAdmission { internal_id: u64, }, @@ -1033,6 +1038,10 @@ pub(crate) enum AsyncSubresourceFetchEventTarget { #[derive(Debug)] pub(super) enum AsyncSubresourceFetchEvent { + RequestUploadReady { + internal_id: u64, + result: Result<(), moli_fetch::UploadBodyError>, + }, RedirectAdmission { internal_id: u64, admission: crate::network_host::RedirectAdmission, @@ -1059,6 +1068,11 @@ pub(super) enum AsyncSubresourceFetchEvent { impl AsyncSubresourceFetchEvent { pub(crate) fn target(&self) -> AsyncSubresourceFetchEventTarget { match self { + Self::RequestUploadReady { internal_id, .. } => { + AsyncSubresourceFetchEventTarget::RequestUpload { + internal_id: *internal_id, + } + } Self::RedirectAdmission { internal_id, .. } => { AsyncSubresourceFetchEventTarget::RedirectAdmission { internal_id: *internal_id, diff --git a/moli-renderer-v8/src/worker/global_scope/fetch.rs b/moli-renderer-v8/src/worker/global_scope/fetch.rs index c0d13bd5b4d..4ba212fbc67 100644 --- a/moli-renderer-v8/src/worker/global_scope/fetch.rs +++ b/moli-renderer-v8/src/worker/global_scope/fetch.rs @@ -257,6 +257,7 @@ pub(in crate::worker) fn spawn_worker_fetch_network( redirect_headers: Option, redirect_chain: Vec, redirect_check: Option, + upload_body: Option, ) { timing.begin_transport(cancel_handle.clone()); // Resolve local URLs before scheduling: fetch(url) already parsed and @@ -324,6 +325,9 @@ pub(in crate::worker) fn spawn_worker_fetch_network( .with_fetch_priority_hint(priority) .with_network_partition_key(network_partition_key.clone()) .with_browser_request_metadata(kind.browser_metadata()); + if let Some(body) = upload_body { + request = request.with_upload_body(body); + } if kind == WorkerFetchKind::Font { request = request.with_resource_type(moli_fetch::RequestResourceType::Font); } @@ -622,6 +626,7 @@ fn spawn_worker_fetch_service_worker( None, Vec::new(), redirect_check, + None, ); return; } @@ -961,6 +966,7 @@ pub(in crate::worker) fn continue_pending_worker_fetch( redirect_headers, redirect_chain, worker_fetch_redirect_check(&state.borrow(), fetch_id), + None, ); } @@ -2111,6 +2117,7 @@ pub(in crate::worker) struct ResolvedWorkerFetchInput<'s> { method: String, body: Option, body_stream: Option>, + body_stream_locked: bool, headers: Vec<(String, String)>, request_mode: moli_fetch::RequestMode, credentials_mode: RequestCredentialsMode, @@ -2128,6 +2135,7 @@ impl ResolvedWorkerFetchInput<'_> { method: "GET".to_owned(), body: None, body_stream: None, + body_stream_locked: false, headers: Vec::new(), request_mode: RequestMode::Cors, credentials_mode: RequestCredentialsMode::SameOrigin, @@ -2326,15 +2334,18 @@ pub(in crate::worker) fn resolve_worker_fetch_input<'s>( } let signal = signal.as_ref().map(|signal| v8::Local::new(scope, signal)); let blob_url_entry = blob_url_entry.or_else(|| CapturedBlobUrl::capture(&resolved_url)); - if consumes_request_body && let Some(request_like) = request_like { - crate::network_host::mark_request_input_body_used_for_fetch(scope, request_like); - } + let body_stream_locked = if consumes_request_body && let Some(request_like) = request_like { + crate::network_host::mark_request_input_body_used_for_fetch(scope, request_like)? + } else { + false + }; Ok(ResolvedWorkerFetchInput { resolved_url, blob_url_entry, method, body, body_stream, + body_stream_locked, headers, request_mode, credentials_mode, @@ -2448,6 +2459,7 @@ pub(super) fn start_worker_resource_fetch<'s>( method, body, body_stream, + body_stream_locked, headers: request_headers, request_mode, credentials_mode, @@ -2651,6 +2663,7 @@ pub(super) fn start_worker_resource_fetch<'s>( state.pending_fetches.insert( fetch_id, PendingWorkerFetch { + upload: None, timing: timing.clone(), kind, font_face: font_face.map(|face| v8::Global::new(scope, face)), @@ -2757,6 +2770,7 @@ pub(super) fn start_worker_resource_fetch<'s>( state.pending_fetches.insert( fetch_id, PendingWorkerFetch { + upload: None, timing: timing.clone(), kind, font_face: font_face.map(|face| v8::Global::new(scope, face)), @@ -2799,6 +2813,47 @@ pub(super) fn start_worker_resource_fetch<'s>( }; let redirect_check = worker_fetch_redirect_check(&state.borrow(), fetch_id); + let upload_body = if service_worker_controller.is_none() + && request_metadata.body_source_is_null + && matches!(resolved_url.scheme(), "http" | "https") + { + if let Some(stream) = body_stream.as_ref() { + let stream = v8::Local::new(scope, stream); + let completion = completion_tx.clone(); + match crate::network_host::RequestUpload::new( + scope, + stream, + resolver, + body_stream_locked, + load.task_runner(), + cancel_handle.clone(), + move |result| { + let _ = completion + .send(WorkerFetchEvent::RequestUploadReady { fetch_id, result }); + }, + ) { + Ok((upload, body)) => { + state + .borrow_mut() + .pending_fetches + .get_mut(&fetch_id) + .expect("new worker fetch") + .upload = Some(upload.clone()); + upload.start(scope); + Some(body) + } + Err(reason) => { + state.borrow_mut().pending_fetches.remove(&fetch_id); + resolver.reject(scope, reason); + return Some(promise); + } + } + } else { + None + } + } else { + None + }; if let Some((service_worker_runtime, service_worker_client_id)) = service_worker_controller { spawn_worker_fetch_service_worker( @@ -2851,6 +2906,7 @@ pub(super) fn start_worker_resource_fetch<'s>( None, Vec::new(), redirect_check, + upload_body, ); } @@ -2888,6 +2944,7 @@ pub(in crate::worker) fn reject_worker_fetches_for_signal<'s>( pending.network_record, pending.network_request_handle, pending.streaming_body_source_id, + pending.upload, )); } } @@ -2902,6 +2959,7 @@ pub(in crate::worker) fn reject_worker_fetches_for_signal<'s>( network_record, network_request_handle, body_source_id, + upload, ) in rejected { timing.abort(scope); @@ -2943,6 +3001,9 @@ pub(in crate::worker) fn reject_worker_fetches_for_signal<'s>( let resolver = v8::Local::new(scope, &resolver); let _ = resolver.reject(scope, reason); } + if let Some(upload) = upload { + upload.stop(scope, reason); + } } } @@ -2975,6 +3036,16 @@ pub(in crate::worker) fn drain_worker_fetch_completion( event: WorkerFetchEvent, ) { match event { + WorkerFetchEvent::RequestUploadReady { fetch_id, result } => { + let upload = state + .borrow() + .pending_fetches + .get(&fetch_id) + .and_then(|pending| pending.upload.clone()); + if let Some(upload) = upload { + upload.ready(scope, result); + } + } WorkerFetchEvent::RedirectAdmission { fetch_id, admission, @@ -3190,6 +3261,14 @@ pub(in crate::worker) fn start_worker_streaming_fetch( Some(response_filter), ); let resolver = v8::Local::new(scope, &resolver); + let upload = state + .borrow() + .pending_fetches + .get(&started.fetch_id) + .and_then(|pending| pending.upload.clone()); + if let Some(upload) = upload { + upload.finish(scope); + } let _ = resolver.resolve(scope, response_obj.into()); } } @@ -3666,14 +3745,19 @@ pub(in crate::worker) fn drain_worker_fetch_completion_result( } }; let _ = resolver.resolve(scope, response_obj.into()); + if let Some(upload) = pending.upload.as_ref() { + upload.finish(scope); + } } Err(message) => { pending.timing.publish(scope); record_worker_fetch_failure(&state.borrow(), &pending, message.clone()); - if let Some(message) = v8_string(scope, &message) { - let _ = resolver.reject(scope, v8::Exception::type_error(scope, message)); - } else { - let _ = resolver.reject(scope, v8::undefined(scope).into()); + let reason = v8_string(scope, &message) + .map(|message| v8::Exception::type_error(scope, message)) + .unwrap_or_else(|| v8::undefined(scope).into()); + let _ = resolver.reject(scope, reason); + if let Some(upload) = pending.upload.as_ref() { + upload.stop(scope, reason); } } } diff --git a/moli-renderer-v8/src/worker/global_scope/network_state.rs b/moli-renderer-v8/src/worker/global_scope/network_state.rs index 5e43079f4a8..af5da57af01 100644 --- a/moli-renderer-v8/src/worker/global_scope/network_state.rs +++ b/moli-renderer-v8/src/worker/global_scope/network_state.rs @@ -3,6 +3,7 @@ use super::*; pub(in crate::worker) struct PendingWorkerFetch { + pub(in crate::worker) upload: Option, pub(super) timing: WorkerResourceTiming, pub(in crate::worker) kind: fetch::WorkerFetchKind, pub(in crate::worker) font_face: Option>, @@ -34,6 +35,10 @@ pub(in crate::worker) struct PendingWorkerFetch { } pub(in crate::worker) enum WorkerFetchEvent { + RequestUploadReady { + fetch_id: u32, + result: Result<(), moli_fetch::UploadBodyError>, + }, RedirectAdmission { fetch_id: u32, admission: crate::network_host::RedirectAdmission,