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 30623697849..0b715683708 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 @@ -694,7 +694,7 @@ impl PageScriptHost { info.request_cookie_report.clone(), ) .with_request_body_bytes(info.request_body_bytes.clone()) - .with_keepalive(disposition == ResourceLoadDisposition::Keepalive), + .with_keepalive(disposition.is_keepalive()), ); } @@ -749,20 +749,25 @@ impl PageScriptHost { policy_context: crate::types::SubresourcePolicyContext, blob_url_entry: Option, mut info: PendingSubresourceFetchInfo, - ) -> u64 { + ) -> Option { self.assign_pending_subresource_fetch_identity(&mut info); let internal_id = info.internal_id; let disposition = if keepalive { - ResourceLoadDisposition::Keepalive + ResourceLoadDisposition::Keepalive { + body_bytes: info + .request_body_bytes + .as_ref() + .map_or(0, bytes::Bytes::len), + } } else { ResourceLoadDisposition::Ordinary }; - let (load, _) = self.require_document_resource_load_for_dispatch_scope( + let (load, _) = self.register_document_resource_load_for_dispatch_scope( fetch_context.request_target().dispatch_scope(), info.resource_type, disposition, None, - ); + )?; let mut visible_info = info.clone(); visible_info.request_headers = request.interception_request_headers_with_web_security( load.request_client().web_security_disabled(), @@ -799,7 +804,7 @@ impl PageScriptHost { }, ); self.note_subresource_activity(); - internal_id + Some(internal_id) } pub(crate) fn record_async_subresource_fetch( @@ -821,20 +826,25 @@ impl PageScriptHost { policy_context: crate::types::SubresourcePolicyContext, mut info: PendingSubresourceFetchInfo, defer_request_started: bool, - ) -> u64 { + ) -> Option { self.assign_pending_subresource_fetch_identity(&mut info); let internal_id = info.internal_id; let disposition = if keepalive { - ResourceLoadDisposition::Keepalive + ResourceLoadDisposition::Keepalive { + body_bytes: info + .request_body_bytes + .as_ref() + .map_or(0, bytes::Bytes::len), + } } else { ResourceLoadDisposition::Ordinary }; - let (load, _) = self.require_document_resource_load_for_dispatch_scope( + let (load, _) = self.register_document_resource_load_for_dispatch_scope( fetch_context.request_target().dispatch_scope(), info.resource_type, disposition, cancel_handle.clone(), - ); + )?; if !defer_request_started { self.record_pending_subresource_request_started(&info, load.disposition()); } @@ -868,7 +878,7 @@ impl PageScriptHost { }, ); self.note_subresource_activity(); - internal_id + Some(internal_id) } #[allow(clippy::too_many_arguments)] @@ -1391,15 +1401,20 @@ impl PageScriptHost { cancel_handle: Option, network_partition_key: Option, mut info: PendingSubresourceFetchInfo, - ) -> u64 { + ) -> Option { self.assign_pending_subresource_fetch_identity(&mut info); let internal_id = info.internal_id; - let (load, request_origin) = self.require_document_resource_load_for_dispatch_scope( + let (load, request_origin) = self.register_document_resource_load_for_dispatch_scope( execution_context.dispatch_scope(), info.resource_type, - ResourceLoadDisposition::Keepalive, + ResourceLoadDisposition::Keepalive { + body_bytes: info + .request_body_bytes + .as_ref() + .map_or(0, bytes::Bytes::len), + }, cancel_handle.clone(), - ); + )?; self.record_pending_subresource_request_started(&info, load.disposition()); self.pending_subresource_fetches.insert( internal_id, @@ -1422,7 +1437,7 @@ impl PageScriptHost { }, ); self.note_subresource_activity(); - internal_id + Some(internal_id) } #[allow(clippy::too_many_arguments)] @@ -1444,7 +1459,7 @@ impl PageScriptHost { load.kind(), crate::network::loads::ResourceLoadKind::CspReport ); - debug_assert_eq!(load.disposition(), ResourceLoadDisposition::Keepalive); + debug_assert!(load.disposition().is_keepalive()); self.record_pending_subresource_request_started(&info, load.disposition()); self.pending_subresource_fetches.insert( internal_id, @@ -1478,15 +1493,20 @@ impl PageScriptHost { execution_context: super::WindowExecutionContextIdentity, network_partition_key: Option, mut info: PendingSubresourceFetchInfo, - ) -> u64 { + ) -> Option { self.assign_pending_subresource_fetch_identity(&mut info); let internal_id = info.internal_id; - let (load, request_origin) = self.require_document_resource_load_for_dispatch_scope( + let (load, request_origin) = self.register_document_resource_load_for_dispatch_scope( execution_context.dispatch_scope(), info.resource_type, - ResourceLoadDisposition::Keepalive, + ResourceLoadDisposition::Keepalive { + body_bytes: info + .request_body_bytes + .as_ref() + .map_or(0, bytes::Bytes::len), + }, None, - ); + )?; self.push_pending_subresource_fetch_info(info.clone()); self.pending_subresource_fetches.insert( internal_id, @@ -1509,7 +1529,7 @@ impl PageScriptHost { }, ); self.note_subresource_activity(); - internal_id + Some(internal_id) } #[allow(clippy::too_many_arguments)] @@ -1531,7 +1551,7 @@ impl PageScriptHost { load.kind(), crate::network::loads::ResourceLoadKind::CspReport ); - debug_assert_eq!(load.disposition(), ResourceLoadDisposition::Keepalive); + debug_assert!(load.disposition().is_keepalive()); self.push_pending_subresource_fetch_info(info.clone()); self.pending_subresource_fetches.insert( internal_id, @@ -2608,7 +2628,7 @@ impl PageScriptHost { let mut detached_window_fetches = 0; self.for_each_subresource_fetch_state_mut(|pending| { if pending.load.registry_id() == registry_id - && pending.load.disposition() == ResourceLoadDisposition::Keepalive + && pending.load.disposition().is_keepalive() { detached_window_fetches += usize::from(pending.detach_keepalive_window_fetch()); } diff --git a/moli-renderer-v8/src/network/context/document.rs b/moli-renderer-v8/src/network/context/document.rs index 4a73ffb6da8..01cf70796d9 100644 --- a/moli-renderer-v8/src/network/context/document.rs +++ b/moli-renderer-v8/src/network/context/document.rs @@ -522,26 +522,31 @@ impl DocumentResourceLoader { pub(crate) fn register_network_only_keepalive_load( &self, kind: ResourceLoadKind, + body_bytes: usize, request_client: ResourceRequestClient, cancel_handle: Option, ) -> Option { if let Some(load) = self.authority.loads.register( kind, - ResourceLoadDisposition::Keepalive, + ResourceLoadDisposition::Keepalive { body_bytes }, request_client.clone(), cancel_handle.clone(), ) { return Some(load); } - matches!( + if matches!( self.state(), DocumentResourceLoaderState::Detaching | DocumentResourceLoaderState::Detached - ) - .then(|| { - self.authority - .loads - .register_detached_keepalive(kind, request_client, cancel_handle) - }) + ) { + self.authority.loads.register_detached_keepalive( + kind, + body_bytes, + request_client, + cancel_handle, + ) + } else { + None + } } pub(crate) fn load_diagnostics(&self) -> ResourceLoadRegistryDiagnostics { diff --git a/moli-renderer-v8/src/network/context/tests.rs b/moli-renderer-v8/src/network/context/tests.rs index 1ebdad731f4..0a05ec33cd4 100644 --- a/moli-renderer-v8/src/network/context/tests.rs +++ b/moli-renderer-v8/src/network/context/tests.rs @@ -442,6 +442,7 @@ async fn detached_document_can_spawn_network_only_keepalive_from_captured_contex let load = loader .register_network_only_keepalive_load( ResourceLoadKind::CspReport, + 0, request_client, Some(cancel.clone()), ) @@ -511,7 +512,7 @@ async fn worker_retirement_cancels_ordinary_and_transfers_keepalive_loads() { let keepalive = loader .register_load( ResourceLoadKind::Fetch, - ResourceLoadDisposition::Keepalive, + ResourceLoadDisposition::Keepalive { body_bytes: 0 }, Some(keepalive_cancel.clone()), ) .expect("active worker keepalive load"); diff --git a/moli-renderer-v8/src/network/loads/keepalive.rs b/moli-renderer-v8/src/network/loads/keepalive.rs index c93d6b5a40a..f5616ca3ac8 100644 --- a/moli-renderer-v8/src/network/loads/keepalive.rs +++ b/moli-renderer-v8/src/network/loads/keepalive.rs @@ -1,10 +1,54 @@ -use std::collections::HashMap; +use std::{ + collections::HashMap, + sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, + }, +}; use moli_fetch::FetchCancelHandle; use parking_lot::Mutex; use super::registry::{ResourceLoadId, ResourceLoadKind}; +const KEEPALIVE_BODY_QUOTA: usize = 65536; + +/// In-flight body bytes belonging to one Fetch group, independent of its +/// current Document authority and of any JavaScript completion route. +#[derive(Clone, Default)] +pub(super) struct KeepaliveBodyBudget(Arc); + +impl KeepaliveBodyBudget { + pub(super) fn reserve(&self, bytes: usize) -> Option { + self.0 + .try_update(Ordering::AcqRel, Ordering::Acquire, |used| { + used.checked_add(bytes) + .filter(|total| *total <= KEEPALIVE_BODY_QUOTA) + }) + .ok()?; + Some(KeepaliveBodyReservation { + budget: self.clone(), + bytes, + }) + } + + pub(super) fn is_empty(&self) -> bool { + self.0.load(Ordering::Acquire) == 0 + } +} + +pub(super) struct KeepaliveBodyReservation { + budget: KeepaliveBodyBudget, + bytes: usize, +} + +impl Drop for KeepaliveBodyReservation { + fn drop(&mut self) { + let previous = self.budget.0.fetch_sub(self.bytes, Ordering::AcqRel); + debug_assert!(previous >= self.bytes); + } +} + #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Serialize)] #[serde(rename_all = "camelCase")] pub(crate) struct DetachedKeepaliveLoadDiagnostics { @@ -69,3 +113,26 @@ impl DetachedKeepaliveLoadRegistry { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn keepalive_body_budget_rejects_overflow_and_releases_only_its_own_bytes() { + let budget = KeepaliveBodyBudget::default(); + assert!(budget.reserve(usize::MAX).is_none()); + assert!(budget.reserve(65537).is_none()); + let first = budget.reserve(65535).unwrap(); + let second = budget.reserve(1).unwrap(); + assert!(budget.reserve(1).is_none()); + assert!(budget.reserve(0).is_some()); + drop(second); + assert!(budget.reserve(2).is_none()); + let third = budget.reserve(1).unwrap(); + drop(first); + assert!(budget.reserve(65536).is_none()); + drop(third); + assert!(budget.reserve(65536).is_some()); + } +} diff --git a/moli-renderer-v8/src/network/loads/registry.rs b/moli-renderer-v8/src/network/loads/registry.rs index da9f92ac72a..5de80caec54 100644 --- a/moli-renderer-v8/src/network/loads/registry.rs +++ b/moli-renderer-v8/src/network/loads/registry.rs @@ -10,6 +10,8 @@ use std::{ use moli_fetch::FetchCancelHandle; use parking_lot::Mutex; +use super::keepalive::{KeepaliveBodyBudget, KeepaliveBodyReservation}; + use crate::{ network::{BrowserResourceRuntime, RendererResourceTaskRunner, ResourceRequestClient}, types::SubresourceResourceType, @@ -35,7 +37,13 @@ impl ResourceLoadId { #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub(crate) enum ResourceLoadDisposition { Ordinary, - Keepalive, + Keepalive { body_bytes: usize }, +} + +impl ResourceLoadDisposition { + pub(crate) fn is_keepalive(self) -> bool { + matches!(self, Self::Keepalive { .. }) + } } #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -99,6 +107,7 @@ pub(crate) struct ResourceLoadRetirement { struct ResourceLoadRegistryState { detached: bool, loads: HashMap>, + keepalive_body_budget: KeepaliveBodyBudget, } struct ResourceLoadRegistryInner { @@ -132,6 +141,7 @@ impl ResourceLoadRegistry { state: Mutex::new(ResourceLoadRegistryState { detached: false, loads: HashMap::new(), + keepalive_body_budget: KeepaliveBodyBudget::default(), }), }), } @@ -152,12 +162,24 @@ impl ResourceLoadRegistry { request_client: ResourceRequestClient, cancel_handle: Option, ) -> Option { - let registration = - self.new_registration(kind, disposition, request_client, cancel_handle, false); let mut state = self.inner.state.lock(); if state.detached { return None; } + let body_reservation = match disposition { + ResourceLoadDisposition::Ordinary => None, + ResourceLoadDisposition::Keepalive { body_bytes } => { + Some(state.keepalive_body_budget.reserve(body_bytes)?) + } + }; + let registration = self.new_registration( + kind, + disposition, + request_client, + cancel_handle, + false, + body_reservation, + ); state .loads .insert(registration.id, Arc::downgrade(®istration)); @@ -173,21 +195,29 @@ impl ResourceLoadRegistry { pub(crate) fn register_detached_keepalive( &self, kind: ResourceLoadKind, + body_bytes: usize, request_client: ResourceRequestClient, cancel_handle: Option, - ) -> ResourceLoadLease { + ) -> Option { + let body_reservation = self + .inner + .state + .lock() + .keepalive_body_budget + .reserve(body_bytes)?; let registration = self.new_registration( kind, - ResourceLoadDisposition::Keepalive, + ResourceLoadDisposition::Keepalive { body_bytes }, request_client, cancel_handle, true, + Some(body_reservation), ); let cancel_handle = registration.lifecycle.lock().cancel_handle.clone(); registration .browser_runtime .register_detached_keepalive_load(registration.id, registration.kind, cancel_handle); - ResourceLoadLease { registration } + Some(ResourceLoadLease { registration }) } fn new_registration( @@ -197,6 +227,7 @@ impl ResourceLoadRegistry { request_client: ResourceRequestClient, cancel_handle: Option, detached_keepalive: bool, + body_reservation: Option, ) -> Arc { let id = ResourceLoadId::next(); let browser_runtime = request_client.browser_resource_runtime(); @@ -215,6 +246,7 @@ impl ResourceLoadRegistry { consumer_cancel: None, detached_keepalive, registry: Arc::downgrade(&self.inner), + body_reservation, }), cancelled: AtomicBool::new(false), finished: AtomicBool::new(false), @@ -243,18 +275,19 @@ impl ResourceLoadRegistry { "replacement registry must share the source resource task runner" ); - let registrations = { + let (registrations, keepalive_body_budget) = { let mut source = self.inner.state.lock(); assert!( !source.detached, "only an active resource registry can be superseded" ); source.detached = true; - source + let registrations = source .loads .drain() .filter_map(|(_, registration)| registration.upgrade()) - .collect::>() + .collect::>(); + (registrations, source.keepalive_body_budget.clone()) }; let replacement_registry = Arc::downgrade(&replacement.inner); @@ -286,6 +319,14 @@ impl ResourceLoadRegistry { !target.detached, "resource loads cannot transfer into a detached registry" ); + // document.open() keeps the LocalWindow's Fetch group. Existing + // reservations retain this same counter through completion, even + // though their lifecycle registration moves to the new authority. + assert!( + target.keepalive_body_budget.is_empty(), + "replacement authority must not already own a different keepalive budget" + ); + target.keepalive_body_budget = keepalive_body_budget; let mut published = 0; for registration in transferred { if registration.finished.load(Ordering::Acquire) @@ -324,7 +365,7 @@ impl ResourceLoadRegistry { retirement.ordinary_load_count += 1; registration.cancel(); } - ResourceLoadDisposition::Keepalive => { + ResourceLoadDisposition::Keepalive { .. } => { retirement.keepalive_load_count += 1; registration.detach_keepalive(); } @@ -348,7 +389,7 @@ impl ResourceLoadRegistry { ResourceLoadDisposition::Ordinary => { diagnostics.active_ordinary_load_count += 1; } - ResourceLoadDisposition::Keepalive => { + ResourceLoadDisposition::Keepalive { .. } => { diagnostics.active_keepalive_load_count += 1; } } @@ -358,6 +399,7 @@ impl ResourceLoadRegistry { } struct ResourceLoadLifecycle { + body_reservation: Option, #[cfg(any(test, feature = "devtools"))] auth_retry_budget: moli_fetch::AuthRetryBudget, cancel_handle: Option, @@ -383,15 +425,17 @@ impl ResourceLoadRegistration { if self.finished.load(Ordering::Acquire) || self.cancelled.swap(true, Ordering::AcqRel) { return; } - let (cancel_handle, consumer_cancel, detached_keepalive, registry) = { + let (cancel_handle, consumer_cancel, detached_keepalive, registry, body_reservation) = { let mut lifecycle = self.lifecycle.lock(); ( lifecycle.cancel_handle.take(), lifecycle.consumer_cancel.take(), lifecycle.detached_keepalive, lifecycle.registry.clone(), + lifecycle.body_reservation.take(), ) }; + drop(body_reservation); if let Some(cancel_handle) = cancel_handle { cancel_handle.cancel(); } @@ -433,11 +477,16 @@ impl ResourceLoadRegistration { if self.finished.swap(true, Ordering::AcqRel) { return; } - let (detached_keepalive, registry) = { + let (detached_keepalive, registry, body_reservation) = { let mut lifecycle = self.lifecycle.lock(); lifecycle.consumer_cancel.take(); - (lifecycle.detached_keepalive, lifecycle.registry.clone()) + ( + lifecycle.detached_keepalive, + lifecycle.registry.clone(), + lifecycle.body_reservation.take(), + ) }; + drop(body_reservation); if let Some(registry) = registry.upgrade() { registry.state.lock().loads.remove(&self.id); } @@ -660,6 +709,129 @@ mod tests { .expect("active registry should accept loads") } + fn keepalive_load( + registry: &ResourceLoadRegistry, + client: &crate::network::ResourceRequestClientOwner, + bytes: usize, + ) -> Option { + registry.register( + ResourceLoadKind::Fetch, + ResourceLoadDisposition::Keepalive { body_bytes: bytes }, + client.handle(), + None, + ) + } + + #[tokio::test(flavor = "current_thread")] + async fn keepalive_quota_tracks_logical_requests_across_clones_and_cancel() { + let registry = registry(); + let first = keepalive_load(®istry, ®istry.request_client_owner, 65535).unwrap(); + let alias = first.clone(); + let second = keepalive_load(®istry, ®istry.request_client_owner, 1).unwrap(); + assert!(keepalive_load(®istry, ®istry.request_client_owner, 1).is_none()); + second.finish(); + assert!(keepalive_load(®istry, ®istry.request_client_owner, 2).is_none()); + let third = keepalive_load(®istry, ®istry.request_client_owner, 1).unwrap(); + first.finish(); + alias.finish(); + assert!(keepalive_load(®istry, ®istry.request_client_owner, 65536).is_none()); + let fourth = keepalive_load(®istry, ®istry.request_client_owner, 65535).unwrap(); + third.cancel(); + third.finish(); + assert!(keepalive_load(®istry, ®istry.request_client_owner, 2).is_none()); + let last = keepalive_load(®istry, ®istry.request_client_owner, 1).unwrap(); + fourth.finish(); + drop(last); + assert!(keepalive_load(®istry, ®istry.request_client_owner, 65536).is_some()); + } + + #[tokio::test(flavor = "current_thread")] + async fn keepalive_quota_isolates_fetch_groups_sharing_a_browser_client() { + let registry = registry(); + let other = ResourceLoadRegistry::new(registry.task_runner()); + let _first = keepalive_load(®istry, ®istry.request_client_owner, 65536).unwrap(); + assert!(keepalive_load(®istry, ®istry.request_client_owner, 1).is_none()); + let _other = keepalive_load(&other, ®istry.request_client_owner, 65536).unwrap(); + assert!(keepalive_load(&other, ®istry.request_client_owner, 1).is_none()); + } + + #[tokio::test(flavor = "current_thread")] + async fn keepalive_quota_survives_document_authority_transfer() { + let registry = registry(); + let replacement = ResourceLoadRegistry::new(registry.task_runner()); + let first = keepalive_load(®istry, ®istry.request_client_owner, 65536).unwrap(); + assert_eq!(registry.transfer_existing_loads_to(&replacement), 1); + assert!(keepalive_load(&replacement, ®istry.request_client_owner, 1).is_none()); + first.finish(); + assert!(keepalive_load(&replacement, ®istry.request_client_owner, 65536).is_some()); + } + + #[tokio::test(flavor = "current_thread")] + async fn detached_keepalive_reports_share_the_outgoing_fetch_group_quota() { + let registry = registry(); + let first = keepalive_load(®istry, ®istry.request_client_owner, 65536).unwrap(); + registry.begin_detach(); + assert!( + registry + .register_detached_keepalive( + ResourceLoadKind::CspReport, + 1, + registry.request_client_owner.handle(), + None + ) + .is_none() + ); + first.finish(); + let report = registry + .register_detached_keepalive( + ResourceLoadKind::CspReport, + 65536, + registry.request_client_owner.handle(), + None, + ) + .unwrap(); + assert!(report.is_detached_keepalive()); + report.finish(); + } + + #[tokio::test(flavor = "current_thread")] + async fn keepalive_quota_admission_is_atomic_across_network_threads() { + let registry = registry(); + let barrier = std::sync::Barrier::new(33); + let accepted = std::sync::atomic::AtomicUsize::new(0); + let mut admitted = 0; + let mut extra = None; + std::thread::scope(|scope| { + for _ in 0..32 { + let loads = ®istry.registry; + let client = registry.request_client_owner.handle(); + let barrier = &barrier; + let accepted = &accepted; + scope.spawn(move || { + let load = loads.register( + ResourceLoadKind::Fetch, + ResourceLoadDisposition::Keepalive { body_bytes: 4096 }, + client, + None, + ); + if load.is_some() { + accepted.fetch_add(1, Ordering::AcqRel); + } + barrier.wait(); + barrier.wait(); + drop(load); + }); + } + barrier.wait(); + admitted = accepted.load(Ordering::Acquire); + extra = keepalive_load(®istry, ®istry.request_client_owner, 1); + barrier.wait(); + }); + assert_eq!(admitted, 16); + assert!(extra.is_none()); + assert!(keepalive_load(®istry, ®istry.request_client_owner, 65536).is_some()); + } + #[tokio::test(flavor = "current_thread")] async fn authentication_budget_follows_lease_clones_but_not_other_requests() { let registry = registry(); @@ -776,7 +948,7 @@ mod tests { ); let keepalive = lease( &source, - ResourceLoadDisposition::Keepalive, + ResourceLoadDisposition::Keepalive { body_bytes: 0 }, keepalive_cancel.clone(), ); let source_registry_id = source.id(); @@ -823,7 +995,7 @@ mod tests { let cancel_handle = FetchCancelHandle::new(); let lease = lease( ®istry, - ResourceLoadDisposition::Keepalive, + ResourceLoadDisposition::Keepalive { body_bytes: 65536 }, cancel_handle.clone(), ); let runtime = lease.request_client().browser_resource_runtime(); @@ -841,6 +1013,9 @@ mod tests { runtime.detached_keepalive_diagnostics().active_load_count, 1 ); + let weak_registry = Arc::downgrade(®istry.inner); + drop(registry); + assert!(weak_registry.upgrade().is_none()); lease.finish(); assert_eq!( runtime.detached_keepalive_diagnostics().active_load_count, @@ -854,7 +1029,7 @@ mod tests { let cancel_handle = FetchCancelHandle::new(); let lease = lease( ®istry, - ResourceLoadDisposition::Keepalive, + ResourceLoadDisposition::Keepalive { body_bytes: 0 }, cancel_handle.clone(), ); registry.begin_detach(); @@ -878,7 +1053,7 @@ mod tests { let initial_cancel = FetchCancelHandle::new(); let lease = lease( ®istry, - ResourceLoadDisposition::Keepalive, + ResourceLoadDisposition::Keepalive { body_bytes: 0 }, initial_cancel, ); let runtime = lease.request_client().browser_resource_runtime(); diff --git a/moli-renderer-v8/src/network_host/beacon.rs b/moli-renderer-v8/src/network_host/beacon.rs index 575c5b203e6..78476f24202 100644 --- a/moli-renderer-v8/src/network_host/beacon.rs +++ b/moli-renderer-v8/src/network_host/beacon.rs @@ -126,12 +126,14 @@ pub(crate) fn navigator_send_beacon_callback<'s>( }; if host.should_intercept_subresource(SubresourceResourceType::Ping) { - host.record_pending_subresource_beacon( - execution_context, - network_partition_key.clone(), - info, - ); - rv.set(v8::Boolean::new(scope, true).into()); + let admitted = host + .record_pending_subresource_beacon( + execution_context, + network_partition_key.clone(), + info, + ) + .is_some(); + rv.set_bool(admitted); return; } @@ -181,12 +183,15 @@ pub(crate) fn navigator_send_beacon_callback<'s>( resource_type: info.resource_type, policy_context: Default::default(), }; - let internal_id = host.record_async_subresource_beacon( + let Some(internal_id) = host.record_async_subresource_beacon( execution_context, Some(cancel_handle.clone()), network_partition_key, info, - ); + ) else { + rv.set_bool(false); + return; + }; spawn_async_subresource_fetch( resource_loader.task_runner(), host.resource_completion_sender(), @@ -273,7 +278,7 @@ pub(crate) fn send_link_audit_ping( }; if host.should_intercept_subresource(SubresourceResourceType::Ping) { - host.record_pending_subresource_beacon( + let _ = host.record_pending_subresource_beacon( execution_context, network_partition_key.clone(), info, @@ -329,12 +334,14 @@ pub(crate) fn send_link_audit_ping( resource_type: info.resource_type, policy_context: Default::default(), }; - let internal_id = host.record_async_subresource_beacon( + let Some(internal_id) = host.record_async_subresource_beacon( execution_context, Some(cancel_handle.clone()), network_partition_key, info, - ); + ) else { + return; + }; spawn_async_subresource_fetch( resource_loader.task_runner(), host.resource_completion_sender(), diff --git a/moli-renderer-v8/src/network_host/csp_reports.rs b/moli-renderer-v8/src/network_host/csp_reports.rs index 6c236d302d5..5272ea2b5a6 100644 --- a/moli-renderer-v8/src/network_host/csp_reports.rs +++ b/moli-renderer-v8/src/network_host/csp_reports.rs @@ -140,14 +140,14 @@ impl WindowCspReportRequestContext { fn register_report_load( &self, cancel_handle: Option, - ) -> crate::network::loads::ResourceLoadLease { - self.resource_loader - .register_network_only_keepalive_load( - crate::network::loads::ResourceLoadKind::CspReport, - self.request_client.clone(), - cancel_handle, - ) - .expect("captured CSP report context must retain a resource authority") + body_bytes: usize, + ) -> Option { + self.resource_loader.register_network_only_keepalive_load( + crate::network::loads::ResourceLoadKind::CspReport, + body_bytes, + self.request_client.clone(), + cancel_handle, + ) } } @@ -258,7 +258,16 @@ fn send_content_security_policy_report_request( } if host.should_intercept_subresource(SubresourceResourceType::CspReport) { - let load = request_context.register_report_load(None); + let Some(load) = request_context + .register_report_load(None, request.body.as_ref().map_or(0, bytes::Bytes::len)) + else { + record_content_security_policy_report_failure( + host, + info, + "csp report: request could not be admitted".to_owned(), + ); + return; + }; host.record_pending_subresource_csp_report( request_context.identity, request_context.request_origin.clone(), @@ -316,7 +325,17 @@ fn dispatch_service_worker_content_security_policy_report( } let cancel_handle = FetchCancelHandle::new(); - let load = request_context.register_report_load(Some(cancel_handle.clone())); + let Some(load) = request_context.register_report_load( + Some(cancel_handle.clone()), + request.body.as_ref().map_or(0, bytes::Bytes::len), + ) else { + record_content_security_policy_report_failure( + host, + info, + "csp report: request could not be admitted".to_owned(), + ); + return true; + }; let internal_id = host.record_async_subresource_csp_report( request_context.identity, request_context.request_origin.clone(), @@ -387,7 +406,17 @@ fn spawn_content_security_policy_report_fetch( request: Request, ) { let cancel_handle = FetchCancelHandle::new(); - let load = request_context.register_report_load(Some(cancel_handle.clone())); + let Some(load) = request_context.register_report_load( + Some(cancel_handle.clone()), + request.body.as_ref().map_or(0, bytes::Bytes::len), + ) else { + record_content_security_policy_report_failure( + host, + info, + "csp report: request could not be admitted".to_owned(), + ); + return; + }; let task_runner = load.task_runner(); let internal_id = host.record_async_subresource_csp_report( request_context.identity, diff --git a/moli-renderer-v8/src/network_host/fetch/bindings.rs b/moli-renderer-v8/src/network_host/fetch/bindings.rs index 7b07a624797..c735f984baa 100644 --- a/moli-renderer-v8/src/network_host/fetch/bindings.rs +++ b/moli-renderer-v8/src/network_host/fetch/bindings.rs @@ -358,12 +358,19 @@ fn window_fetch_callback_in_relevant_realm<'s>( return; } - if let Some(internal_id) = dispatch_service_worker_fetch(scope, host, resolver, &prepared) { - if let Some(signal) = signal { - install_window_fetch_abort_listener(scope, signal, internal_id); + match dispatch_service_worker_fetch(scope, host, resolver, &prepared) { + Ok(Some(internal_id)) => { + if let Some(signal) = signal { + install_window_fetch_abort_listener(scope, signal, internal_id); + } + rv.set(promise.into()); + return; + } + Ok(None) => {} + Err(error) => { + rv.set(make_rejected_promise(scope, &error.to_string()).into()); + return; } - rv.set(promise.into()); - return; } let internal_id = match spawn_network_fetch(scope, host, resolver, prepared) { 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 fbdcbd1fc29..8e946a456b1 100644 --- a/moli-renderer-v8/src/network_host/fetch/bindings/paths.rs +++ b/moli-renderer-v8/src/network_host/fetch/bindings/paths.rs @@ -1,7 +1,7 @@ use super::super::*; use super::request::PreparedWindowFetchRequest; use moli_fetch::{ - BrowserRequestMetadata, FetchCancelHandle, ScriptFetchRequestMetadata, + BrowserRequestMetadata, FetchCancelHandle, SubresourceRequestMetadata, should_request_be_blocked_due_to_bad_port, }; @@ -24,7 +24,7 @@ pub(super) fn record_intercepted_fetch( &prepared.method, prepared.credentials_mode, ); - Ok(host.record_pending_subresource_fetch( + host.record_pending_subresource_fetch( prepared.fetch_context, v8::Global::new(scope, resolver), prepared.resource_timing, @@ -55,7 +55,8 @@ pub(super) fn record_intercepted_fetch( resource_type: SubresourceResourceType::Fetch, request_cookie_report, }, - )) + ) + .ok_or_else(|| "Failed to fetch".to_owned()) } pub(super) fn reject_offline_fetch( @@ -246,7 +247,7 @@ pub(super) fn spawn_network_fetch( ) -> anyhow::Result { let cache_mode = prepared.cache_mode(); let loader = prepared.resource_loader.request_client().clone(); - let request = native_window_fetch_request(&prepared).map_err(anyhow::Error::msg)?; + let mut request = native_window_fetch_request(&prepared).map_err(anyhow::Error::msg)?; let request_cookie_report = observe_subresource_request_cookie_report( prepared.resource_loader.request_client(), @@ -265,38 +266,40 @@ pub(super) fn spawn_network_fetch( }; let cancel_handle = FetchCancelHandle::new(); let requires_preflight = prepared.requires_initial_preflight(); - let internal_id = host.record_async_subresource_fetch( - prepared.fetch_context, - v8::Global::new(scope, resolver), - prepared.resource_timing, - prepared.keepalive, - prepared.integrity.clone(), - prepared.connect_policy, - prepared.csp_report_context, - Some(cancel_handle.clone()), - prepared.credentials_mode, - prepared.request_mode, - cache_mode, - prepared.request_origin.clone(), - prepared.redirect_mode, - prepared.network_partition_key.clone(), - prepared.policy_context, - PendingSubresourceFetchInfo { - internal_id: 0, - network_request_handle: None, - frame_id: prepared.frame_id.clone(), - document_url: prepared.document_url.clone(), - url: prepared.resolved_url.clone(), - websocket_socket_id: None, - method: prepared.method.clone(), - request_headers: prepared.request_headers.clone(), - request_body: request_body_text(&prepared.body), - request_body_bytes: prepared.body.clone(), - resource_type: SubresourceResourceType::Fetch, - request_cookie_report, - }, - requires_preflight, - ); + let internal_id = host + .record_async_subresource_fetch( + prepared.fetch_context, + v8::Global::new(scope, resolver), + prepared.resource_timing, + prepared.keepalive, + prepared.integrity.clone(), + prepared.connect_policy, + prepared.csp_report_context, + Some(cancel_handle.clone()), + prepared.credentials_mode, + prepared.request_mode, + cache_mode, + prepared.request_origin.clone(), + prepared.redirect_mode, + prepared.network_partition_key.clone(), + prepared.policy_context, + PendingSubresourceFetchInfo { + internal_id: 0, + network_request_handle: None, + frame_id: prepared.frame_id.clone(), + document_url: prepared.document_url.clone(), + url: prepared.resolved_url.clone(), + websocket_socket_id: None, + method: prepared.method.clone(), + request_headers: prepared.request_headers.clone(), + request_body: request_body_text(&prepared.body), + request_body_bytes: prepared.body.clone(), + resource_type: SubresourceResourceType::Fetch, + request_cookie_report, + }, + requires_preflight, + ) + .ok_or_else(|| anyhow::anyhow!("Failed to fetch"))?; if let Some(check) = host.window_fetch_redirect_check(internal_id) { request = request.with_redirect_check(check); } @@ -336,8 +339,8 @@ pub(super) fn native_window_fetch_request( .with_fetch_priority_hint(prepared.priority) .with_fetch_referrer(&prepared.referrer) .map_err(|error| error.to_string())?; - if let Some(metadata) = window_fetch_script_metadata(prepared) { - request = request.with_script_fetch_metadata(metadata); + if let Some(metadata) = window_fetch_subresource_metadata(prepared) { + request = request.with_subresource_request_metadata(metadata); } request = request .with_browser_request_metadata(BrowserRequestMetadata::Fetch) diff --git a/moli-renderer-v8/src/network_host/fetch/bindings/service_worker.rs b/moli-renderer-v8/src/network_host/fetch/bindings/service_worker.rs index 641828f2be3..be83cc76206 100644 --- a/moli-renderer-v8/src/network_host/fetch/bindings/service_worker.rs +++ b/moli-renderer-v8/src/network_host/fetch/bindings/service_worker.rs @@ -10,16 +10,21 @@ pub(super) fn dispatch_service_worker_fetch( host: &mut PageScriptHost, resolver: v8::Local<'_, v8::PromiseResolver>, prepared: &PreparedWindowFetchRequest, -) -> Option { +) -> anyhow::Result> { if !matches!(prepared.resolved_url.scheme(), "http" | "https") { - return None; + return Ok(None); } let client_id = host.service_worker_client_id_for_subresource_owner(prepared.request_scope()); - host.service_worker_controller_for_fetch( - client_id, - &prepared.document_url, - &prepared.resolved_url, - )?; + if host + .service_worker_controller_for_fetch( + client_id, + &prepared.document_url, + &prepared.resolved_url, + ) + .is_none() + { + return Ok(None); + } let native_request = super::paths::native_window_fetch_request(prepared).map_err(anyhow::Error::msg)?; @@ -42,38 +47,40 @@ pub(super) fn dispatch_service_worker_fetch( }; let requires_preflight = prepared.requires_initial_preflight(); let request_body_text = request_body_text(&prepared.body); - let internal_id = host.record_async_subresource_fetch( - prepared.fetch_context.duplicate(scope), - v8::Global::new(scope, resolver), - prepared.resource_timing.clone(), - prepared.keepalive, - prepared.integrity.clone(), - prepared.connect_policy.clone(), - prepared.csp_report_context.clone(), - Some(cancel_handle.clone()), - prepared.credentials_mode, - prepared.request_mode, - prepared.cache_mode(), - prepared.request_origin.clone(), - prepared.redirect_mode, - prepared.network_partition_key.clone(), - prepared.policy_context, - PendingSubresourceFetchInfo { - internal_id: 0, - network_request_handle: None, - frame_id: prepared.frame_id.clone(), - document_url: prepared.document_url.clone(), - url: prepared.resolved_url.clone(), - websocket_socket_id: None, - method: prepared.method.clone(), - request_headers: prepared.request_headers.clone(), - request_body: request_body_text.clone(), - request_body_bytes: prepared.body.clone(), - resource_type: SubresourceResourceType::Fetch, - request_cookie_report: request_cookie_report.clone(), - }, - requires_preflight, - ); + let internal_id = host + .record_async_subresource_fetch( + prepared.fetch_context.duplicate(scope), + v8::Global::new(scope, resolver), + prepared.resource_timing.clone(), + prepared.keepalive, + prepared.integrity.clone(), + prepared.connect_policy.clone(), + prepared.csp_report_context.clone(), + Some(cancel_handle.clone()), + prepared.credentials_mode, + prepared.request_mode, + prepared.cache_mode(), + prepared.request_origin.clone(), + prepared.redirect_mode, + prepared.network_partition_key.clone(), + prepared.policy_context, + PendingSubresourceFetchInfo { + internal_id: 0, + network_request_handle: None, + frame_id: prepared.frame_id.clone(), + document_url: prepared.document_url.clone(), + url: prepared.resolved_url.clone(), + websocket_socket_id: None, + method: prepared.method.clone(), + request_headers: prepared.request_headers.clone(), + request_body: request_body_text.clone(), + request_body_bytes: prepared.body.clone(), + resource_type: SubresourceResourceType::Fetch, + request_cookie_report: request_cookie_report.clone(), + }, + requires_preflight, + ) + .ok_or_else(|| anyhow::anyhow!("Failed to fetch"))?; let request = host.service_worker_fetch_request( client_id, prepared.resolved_url.clone(), @@ -104,7 +111,7 @@ pub(super) fn dispatch_service_worker_fetch( direct_completion_tx: None, }; if host.dispatch_service_worker_fetch(dispatch) { - return Some(internal_id); + return Ok(Some(internal_id)); } let _ = @@ -121,7 +128,7 @@ pub(super) fn dispatch_service_worker_fetch( network_error_text: None, result: Err("service worker fetch dispatch failed".to_owned()).into(), }); - Some(internal_id) + Ok(Some(internal_id)) } fn request_body_text(body: &Option) -> Option { diff --git a/moli-renderer-v8/src/network_host/fetch/input.rs b/moli-renderer-v8/src/network_host/fetch/input.rs index 0be2e0d3d78..62503b7b4bb 100644 --- a/moli-renderer-v8/src/network_host/fetch/input.rs +++ b/moli-renderer-v8/src/network_host/fetch/input.rs @@ -58,7 +58,7 @@ pub(super) fn parse_window_fetch_input<'s>( let body = if init.body_present { init.body.clone() } else { - inherited.body.clone() + inherited.body_bytes_for_fetch(scope, req_obj)? }; let mut headers = if init.headers_present { init.headers.clone() diff --git a/moli-renderer-v8/src/network_host/request/input.rs b/moli-renderer-v8/src/network_host/request/input.rs index 196d2da648f..469dab09235 100644 --- a/moli-renderer-v8/src/network_host/request/input.rs +++ b/moli-renderer-v8/src/network_host/request/input.rs @@ -246,6 +246,33 @@ pub(crate) struct RequestInputSnapshot { pub(crate) signal: Option>, } +impl RequestInputSnapshot { + /// Constructor snapshots leave native bodies in their stream for transfer. + /// Fetch must recover a byte-backed source after RequestInit conversion, + /// only when init did not override that body. Genuine streaming bodies + /// remain on the separately captured stream path. + pub(crate) fn body_bytes_for_fetch<'s>( + &self, + scope: &mut v8::PinScope<'s, '_>, + source: v8::Local<'s, v8::Object>, + ) -> Result, webidl::WebIdlError> { + if self.has_stream_body || !is_branded_request_object(scope, source) { + return Ok(self.body.clone()); + } + if request_body_unusable(scope, source) { + return Err(request_body_already_used_error()); + } + if request_slot_value(scope, source, REQUEST_BODY_SLOT) + .is_none_or(|body| body.is_null_or_undefined()) + { + return Ok(None); + } + try_network_body_bytes_from_object(scope, source) + .map(|body| body.map(bytes::Bytes::from)) + .map_err(|_| webidl::WebIdlError::custom_message("Failed to materialize request body")) + } +} + pub(crate) fn mark_request_input_body_used_for_fetch<'s>( scope: &mut v8::PinScope<'s, '_>, object: v8::Local<'s, v8::Object>, diff --git a/moli-renderer-v8/src/script_vm/tests/fetch_keepalive_quota.rs b/moli-renderer-v8/src/script_vm/tests/fetch_keepalive_quota.rs new file mode 100644 index 00000000000..bf99473be78 --- /dev/null +++ b/moli-renderer-v8/src/script_vm/tests/fetch_keepalive_quota.rs @@ -0,0 +1,179 @@ +use super::*; + +#[tokio::test(flavor = "current_thread")] +async fn fetch_keepalive_quota_uses_encoded_body_bytes_after_request_construction() { + let server = StaticHttpServer::spawn(2).await; + let loader = static_http_loader(std::iter::empty::()); + let mut vm = + new_storage_page_task_executor_test_vm_with_loader(server.base_url().as_str(), &loader); + vm.eval( + r#" +globalThis.__quotaResult = null; +(async () => { + const form = new FormData(); + form.set('file', new Blob(['a'.repeat(65536)]), 'file'); + const bodies = ['a'.repeat(65537), 'é'.repeat(32769), new Uint8Array(65537), + new Blob(['a'.repeat(65537)]), new URLSearchParams({q:' '.repeat(65535)}), form]; + const errors = []; + const used = []; + for (const body of bodies) { + const request = new Request('/too-large', {method:'POST',body,keepalive:true}); + used.push(request.bodyUsed); + errors.push(await fetch(request).then(() => false, error => error instanceof TypeError)); + used.push(request.bodyUsed); + } + const exact = new Request('/exact', {method:'POST',body:'é'.repeat(32768),keepalive:true}); + const response = await fetch(exact); + await response.text(); + const overridden = new Request('/override', {method:'POST',body:'old',keepalive:true}); + await overridden.text(); + const overrideResponse = await fetch(overridden, {body:'é'.repeat(32768)}); + await overrideResponse.text(); + __quotaResult = {errors,used,ok:response.ok && overrideResponse.ok,exactUsed:exact.bodyUsed,overrideUsed:overridden.bodyUsed}; +})().catch(error => { __quotaResult = {error:String(error)}; }); +"#, + ) + .unwrap(); + advance_page_task_executor_until_eval_equals( + &mut vm, + &loader, + "String(__quotaResult !== null)", + "true", + "keepalive encoded body quota", + ) + .await; + let result: serde_json::Value = + serde_json::from_str(&vm.eval("JSON.stringify(__quotaResult)").unwrap()).unwrap(); + assert_eq!( + result, + serde_json::json!({"errors":vec![true;6],"used":[false,true,false,true,false,true,false,true,false,true,false,true],"ok":true,"exactUsed":true,"overrideUsed":true}) + ); + let requests = server.finish().await; + assert_eq!(requests.len(), 2); + assert_eq!(requests[0].target, "/exact"); + assert_eq!(requests[1].target, "/override"); + for request in requests { + assert_eq!(request.method, "POST"); + assert_eq!(request.body.len(), 65536); + assert_eq!(request.body, "é".repeat(32768).as_bytes()); + } +} + +#[tokio::test(flavor = "current_thread")] +async fn fetch_keepalive_quota_is_shared_with_beacons_and_released_after_completion() { + let server = StaticHttpServer::spawn(2).await; + let loader = static_http_loader(std::iter::empty::()); + let mut vm = + new_storage_page_task_executor_test_vm_with_loader(server.base_url().as_str(), &loader); + vm.eval( + r#" +globalThis.__quotaResult = null; +const first = fetch('/fetch-full', {method:'POST',body:'a'.repeat(65536),keepalive:true}); +const fetchOver = fetch('/fetch-over', {method:'POST',body:'b',keepalive:true}) + .then(() => false,error => error instanceof TypeError); +const beaconOver = navigator.sendBeacon('/beacon-over','b'); +Promise.all([first.then(response => response.text()),fetchOver]).then(([body,rejected]) => { + __quotaResult = {body:body.length>0,rejected,beaconOver}; +}); +"#, + ) + .unwrap(); + advance_page_task_executor_until_eval_equals( + &mut vm, + &loader, + "String(__quotaResult !== null)", + "true", + "fetch and beacon shared quota", + ) + .await; + assert_eq!( + vm.eval("JSON.stringify(__quotaResult)").unwrap(), + r#"{"body":true,"rejected":true,"beaconOver":false}"# + ); + vm.eval( + r#" +globalThis.__beaconQuotaResult = null; +const accepted = navigator.sendBeacon('/beacon-full','a'.repeat(65536)); +fetch('/over-beacon', {method:'POST',body:'b',keepalive:true}) + .then(() => {__beaconQuotaResult={accepted,rejected:false}}, + error => {__beaconQuotaResult={accepted,rejected:error instanceof TypeError}}); +"#, + ) + .unwrap(); + advance_page_task_executor_until_eval_equals( + &mut vm, + &loader, + "String(__beaconQuotaResult !== null)", + "true", + "beacon blocks over-quota fetch", + ) + .await; + assert_eq!( + vm.eval("JSON.stringify(__beaconQuotaResult)").unwrap(), + r#"{"accepted":true,"rejected":true}"# + ); + let requests = tokio::time::timeout(std::time::Duration::from_secs(8), server.finish()) + .await + .expect("both admitted requests must reach the real transport"); + assert_eq!( + requests + .iter() + .map(|r| r.target.as_str()) + .collect::>(), + ["/fetch-full", "/beacon-full"] + ); + assert!( + requests + .iter() + .all(|r| r.method == "POST" && r.body.len() == 65536) + ); +} + +#[tokio::test(flavor = "current_thread")] +async fn worker_fetch_keepalive_quota_rejects_before_transport() { + let server = StaticHttpServer::spawn_with_responder( + 2, + "Content-Type: text/javascript\r\nCache-Control: no-store\r\n", + |_, request| { + if request.target == "/worker.js" { + r#" +const first=fetch(new Request('/worker-full',{method:'POST',body:'a'.repeat(65536),keepalive:true})); +const over=fetch(new Request('/worker-over',{method:'POST',body:'b',keepalive:true})) + .then(()=>false,error=>error instanceof TypeError); +Promise.all([first.then(response=>response.text()),over]).then(([body,rejected])=>{ + postMessage({body:body==='ok',rejected});close(); +}); +"# + .to_owned() + } else { + "ok".to_owned() + } + }, + ) + .await; + let loader = static_http_loader(std::iter::empty::()); + let mut vm = + new_storage_page_task_executor_test_vm_with_loader(server.base_url().as_str(), &loader); + vm.eval("globalThis.__workerQuotaResult=null;const worker=new Worker('/worker.js');worker.onmessage=event=>{__workerQuotaResult=event.data};").unwrap(); + advance_page_task_executor_until_eval_equals( + &mut vm, + &loader, + "String(__workerQuotaResult !== null)", + "true", + "real worker keepalive quota", + ) + .await; + assert_eq!( + vm.eval("JSON.stringify(__workerQuotaResult)").unwrap(), + r#"{"body":true,"rejected":true}"# + ); + let requests = server.finish().await; + assert_eq!( + requests + .iter() + .map(|r| r.target.as_str()) + .collect::>(), + ["/worker.js", "/worker-full"] + ); + assert_eq!(requests[1].body.len(), 65536); +} diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs index 786ce47a8b8..fd34a3e232d 100644 --- a/moli-renderer-v8/src/script_vm/tests/mod.rs +++ b/moli-renderer-v8/src/script_vm/tests/mod.rs @@ -339,38 +339,40 @@ fn register_pending_window_fetch_for_test( let resolver = v8::PromiseResolver::new(scope).expect("test Fetch resolver"); let cancel_handle = moli_fetch::FetchCancelHandle::new(); let url = Url::parse("https://fetch-execution-context.test/pending").unwrap(); - let internal_id = host.record_async_subresource_fetch( - fetch_context, - v8::Global::new(scope, resolver), - crate::context_bootstrap::FetchResourceTiming::capture(scope, &(&url).into()), - keepalive, - String::new(), - connect_policy, - csp_report_context, - Some(cancel_handle.clone()), - moli_fetch::RequestCredentialsMode::SameOrigin, - moli_fetch::RequestMode::Cors, - moli_fetch::RequestCacheMode::Default, - (&url).into(), - moli_fetch::RequestRedirectMode::Follow, - None, - Default::default(), - crate::types::PendingSubresourceFetchInfo { - internal_id: 0, - network_request_handle: None, - frame_id: None, - document_url: url.clone(), - url: url.clone(), - websocket_socket_id: None, - method: "GET".to_owned(), - request_headers: Vec::new().into(), - request_body: None, - request_body_bytes: None, - resource_type: crate::types::SubresourceResourceType::Fetch, - request_cookie_report: None, - }, - false, - ); + let internal_id = host + .record_async_subresource_fetch( + fetch_context, + v8::Global::new(scope, resolver), + crate::context_bootstrap::FetchResourceTiming::capture(scope, &(&url).into()), + keepalive, + String::new(), + connect_policy, + csp_report_context, + Some(cancel_handle.clone()), + moli_fetch::RequestCredentialsMode::SameOrigin, + moli_fetch::RequestMode::Cors, + moli_fetch::RequestCacheMode::Default, + (&url).into(), + moli_fetch::RequestRedirectMode::Follow, + None, + Default::default(), + crate::types::PendingSubresourceFetchInfo { + internal_id: 0, + network_request_handle: None, + frame_id: None, + document_url: url.clone(), + url: url.clone(), + websocket_socket_id: None, + method: "GET".to_owned(), + request_headers: Vec::new().into(), + request_body: None, + request_body_bytes: None, + resource_type: crate::types::SubresourceResourceType::Fetch, + request_cookie_report: None, + }, + false, + ) + .expect("test Fetch should be admitted"); if !matches!(stage, PendingWindowFetchTestStage::Pending) { let mut pending = host @@ -521,38 +523,40 @@ fn register_pending_window_fetch_with_connect_policy_for_test( panic!("this focused Fetch policy helper only supports frame Documents") } }; - let internal_id = host.record_async_subresource_fetch( - fetch_context, - v8::Global::new(scope, resolver), - crate::context_bootstrap::FetchResourceTiming::capture(scope, &(&document_url).into()), - keepalive, - String::new(), - crate::document_runtime::DocumentConnectPolicySnapshot::from_policy_container(&policy), - csp_report_context, - Some(cancel_handle.clone()), - moli_fetch::RequestCredentialsMode::SameOrigin, - moli_fetch::RequestMode::Cors, - moli_fetch::RequestCacheMode::Default, - (&document_url).into(), - moli_fetch::RequestRedirectMode::Follow, - None, - Default::default(), - crate::types::PendingSubresourceFetchInfo { - internal_id: 0, - network_request_handle: None, - frame_id: None, - document_url, - url: request_url.clone(), - websocket_socket_id: None, - method: "GET".to_owned(), - request_headers: Vec::new().into(), - request_body: None, - request_body_bytes: None, - resource_type: crate::types::SubresourceResourceType::Fetch, - request_cookie_report: None, - }, - false, - ); + let internal_id = host + .record_async_subresource_fetch( + fetch_context, + v8::Global::new(scope, resolver), + crate::context_bootstrap::FetchResourceTiming::capture(scope, &(&document_url).into()), + keepalive, + String::new(), + crate::document_runtime::DocumentConnectPolicySnapshot::from_policy_container(&policy), + csp_report_context, + Some(cancel_handle.clone()), + moli_fetch::RequestCredentialsMode::SameOrigin, + moli_fetch::RequestMode::Cors, + moli_fetch::RequestCacheMode::Default, + (&document_url).into(), + moli_fetch::RequestRedirectMode::Follow, + None, + Default::default(), + crate::types::PendingSubresourceFetchInfo { + internal_id: 0, + network_request_handle: None, + frame_id: None, + document_url, + url: request_url.clone(), + websocket_socket_id: None, + method: "GET".to_owned(), + request_headers: Vec::new().into(), + request_body: None, + request_body_bytes: None, + resource_type: crate::types::SubresourceResourceType::Fetch, + request_cookie_report: None, + }, + false, + ) + .expect("test Fetch should be admitted"); ( internal_id, owner, @@ -2377,6 +2381,7 @@ mod encoded_video_chunk; mod event_receivers; mod extracted; mod fetch_integrity; +mod fetch_keepalive_quota; mod fetch_referrer; mod fetch_request_guard; mod fetch_resource_timing; diff --git a/moli-renderer-v8/src/worker/global_scope/content_security_policy.rs b/moli-renderer-v8/src/worker/global_scope/content_security_policy.rs index cee331b6a85..335122e336b 100644 --- a/moli-renderer-v8/src/worker/global_scope/content_security_policy.rs +++ b/moli-renderer-v8/src/worker/global_scope/content_security_policy.rs @@ -286,9 +286,10 @@ fn send_worker_content_security_policy_report_for_state( let cancel = moli_fetch::FetchCancelHandle::new(); let Some(load) = loader.register_load( ResourceLoadKind::CspReport, - ResourceLoadDisposition::Keepalive, + ResourceLoadDisposition::Keepalive { body_bytes: request.body.as_ref().map_or(0, bytes::Bytes::len) }, Some(cancel.clone()), ) else { + return; }; let info = PendingSubresourceFetchInfo { diff --git a/moli-renderer-v8/src/worker/global_scope/fetch.rs b/moli-renderer-v8/src/worker/global_scope/fetch.rs index a35f13ba79b..88b62abc399 100644 --- a/moli-renderer-v8/src/worker/global_scope/fetch.rs +++ b/moli-renderer-v8/src/worker/global_scope/fetch.rs @@ -725,6 +725,11 @@ pub(in crate::worker) fn resolve_worker_fetch_input<'s>( if inherited.body_unusable && !init.body_present { return Err(crate::network_host::request_body_already_used_error().into()); } + let body = if init.body_present { + init.body.clone() + } else { + inherited.body_bytes_for_fetch(scope, req_obj)? + }; signal = init.signal.unwrap_or(inherited.signal); body_stream = if init.body_present { init.body_stream.clone() @@ -741,11 +746,6 @@ pub(in crate::worker) fn resolve_worker_fetch_input<'s>( } else { inherited.method.clone() }; - let body = if init.body_present { - init.body.clone() - } else { - inherited.body.clone() - }; let mut headers = if init.headers_present { init.headers.clone() } else { @@ -1202,6 +1202,7 @@ pub(super) fn start_worker_resource_fetch<'s>( &state, native_load::NativeWorkerFetchRequest { request, blob_url_entry, kind, font_face, timing }, + document_url, signal_id, request_metadata, @@ -1209,6 +1210,7 @@ pub(super) fn start_worker_resource_fetch<'s>( ) } + } pub(in crate::worker) fn reject_worker_fetches_for_signal<'s>( diff --git a/moli-renderer-v8/src/worker/global_scope/fetch/native_load.rs b/moli-renderer-v8/src/worker/global_scope/fetch/native_load.rs index 9cad78531be..98997fec2dd 100644 --- a/moli-renderer-v8/src/worker/global_scope/fetch/native_load.rs +++ b/moli-renderer-v8/src/worker/global_scope/fetch/native_load.rs @@ -25,7 +25,9 @@ pub(super) fn start<'s>( let load = state.borrow().loader.register_load( kind.resource_type().into(), if metadata.keepalive { - ResourceLoadDisposition::Keepalive + ResourceLoadDisposition::Keepalive { + body_bytes: if matches!(request.url.scheme(), "http" | "https") { request.body.as_ref().map_or(0, bytes::Bytes::len) } else { 0 }, + } } else { ResourceLoadDisposition::Ordinary }, @@ -34,7 +36,7 @@ pub(super) fn start<'s>( let Some(load) = load else { return Some(make_rejected_promise( scope, - "fetch: worker global is shutting down", + "Failed to fetch", )); }; let handle = crate::network::interception_control::next_subresource_network_handle();