From a893c8f680d3652f1dfb5b4100dee901fb2edfce Mon Sep 17 00:00:00 2001 From: ldm0 Date: Sun, 30 Aug 2026 06:29:11 +0800 Subject: [PATCH] fix(blob): revoke object URLs with window realms --- .../wpt-cross-current/failed-cases.txt | 2 + .../wpt-cross-current/passed-cases.txt | 1 + moli-file-api/src/blob_store.rs | 86 ++++++++++++++++++- moli-renderer-v8/src/blob.rs | 12 ++- .../context_host/runtime_observable.rs | 11 ++- .../misc/extracted/blob_file_reader.rs | 51 +++++++++++ 6 files changed, 158 insertions(+), 5 deletions(-) diff --git a/moli-benchmark/wpt-cross-current/failed-cases.txt b/moli-benchmark/wpt-cross-current/failed-cases.txt index 46cf18e927d..fc5a097cdc0 100644 --- a/moli-benchmark/wpt-cross-current/failed-cases.txt +++ b/moli-benchmark/wpt-cross-current/failed-cases.txt @@ -32,6 +32,7 @@ content-security-policy/inheritance/document-write-iframe.html content-security-policy/inheritance/location-reload.html content-security-policy/inheritance/window.html content-security-policy/inside-worker/dedicatedworker-report-only.html +content-security-policy/inside-worker/dedicatedworker-worker-src.html content-security-policy/inside-worker/serviceworker-report-only.https.sub.html content-security-policy/meta/sandbox-iframe.html content-security-policy/navigation/javascript-url-navigation-evaluated-to-string-inherits-csp.html @@ -44,6 +45,7 @@ content-security-policy/resource-hints/prefetch-generate-directives.html content-security-policy/resource-hints/prefetch-no-csp.html content-security-policy/sandbox/autoplay-disabled-by-csp.html content-security-policy/script-src/non-nonceable-elements.html +content-security-policy/script-src/script-src-trusted_types_eval_DedicatedWorker.html content-security-policy/securitypolicyviolation/blockeduri-inline.html content-security-policy/unsafe-eval/eval-blocked-in-about-blank-iframe.html content-security-policy/webrtc/webrtc-allowed-default-src-none.html diff --git a/moli-benchmark/wpt-cross-current/passed-cases.txt b/moli-benchmark/wpt-cross-current/passed-cases.txt index 5dee09c5236..e373b67f572 100644 --- a/moli-benchmark/wpt-cross-current/passed-cases.txt +++ b/moli-benchmark/wpt-cross-current/passed-cases.txt @@ -8,6 +8,7 @@ FileAPI/blob/Blob-stream-sync-xhr-crash.html FileAPI/file/File-constructor-endings.html FileAPI/filelist-section/filelist.html FileAPI/idlharness.html +FileAPI/url/url-lifetime.html IndexedDB/abort-in-initial-upgradeneeded.any.js?moli-wpt-any=dedicatedworker IndexedDB/abort-in-initial-upgradeneeded.any.js?moli-wpt-any=window IndexedDB/bindings-inject-keys-bypass.any.js?moli-wpt-any=dedicatedworker diff --git a/moli-file-api/src/blob_store.rs b/moli-file-api/src/blob_store.rs index 0a8a9045c0f..f0ac243a58f 100644 --- a/moli-file-api/src/blob_store.rs +++ b/moli-file-api/src/blob_store.rs @@ -53,6 +53,7 @@ impl Default for BlobEntries { #[derive(Debug)] struct ObjectUrlState { owner_id: Option, + lifetime_id: Option, blob_id: BlobId, access_key: Option, } @@ -222,14 +223,44 @@ where self.create_object_url_with_access_key(owner_id, blob_id, origin, None) } - /// Create an object URL with its creator environment’s access key. - /// The key belongs to the URL, independently of the backing Blob. + /// Create an object URL tied to a more specific execution-context lifetime. + pub fn create_object_url_with_lifetime( + &self, + owner_id: Option, + lifetime_id: Option, + blob_id: BlobId, + origin: &str, + ) -> Option { + self.create_object_url_with_lifetime_and_access_key( + owner_id, + lifetime_id, + blob_id, + origin, + None, + ) + } + + /// Create an object URL with its creator environment's access key. pub fn create_object_url_with_access_key( &self, owner_id: Option, blob_id: BlobId, origin: &str, access_key: Option, + ) -> Option { + self.create_object_url_with_lifetime_and_access_key( + owner_id, None, blob_id, origin, access_key, + ) + } + + /// Associate the URL's independent creator key and execution-context lifetime. + pub fn create_object_url_with_lifetime_and_access_key( + &self, + owner_id: Option, + lifetime_id: Option, + blob_id: BlobId, + origin: &str, + access_key: Option, ) -> Option { self.retain_blob_object_url_ref(blob_id)?; let mut object_urls = self.object_urls.lock(); @@ -243,6 +274,7 @@ where object_url.clone(), ObjectUrlState { owner_id, + lifetime_id, blob_id, access_key, }, @@ -358,6 +390,28 @@ where } } + /// Revoke every object URL created by one execution-context lifetime. + pub fn cleanup_object_url_lifetime(&self, lifetime_id: u64) -> usize { + let removed_blob_ids = { + let mut object_urls = self.object_urls.lock(); + let mut removed_blob_ids = Vec::new(); + object_urls.retain(|_, state| { + if state.lifetime_id == Some(lifetime_id) { + removed_blob_ids.push(state.blob_id); + false + } else { + true + } + }); + removed_blob_ids + }; + let removed_count = removed_blob_ids.len(); + for blob_id in removed_blob_ids { + self.release_blob_object_url_ref(blob_id); + } + removed_count + } + /// Remove Blob/object URL entries owned by a context. pub fn cleanup_owner_resources(&self, owner_id: OwnerId) { let removed_blob_ids = { @@ -833,4 +887,32 @@ mod tests { Some((b"other".to_vec(), "text/plain".to_owned())) ); } + + #[test] + fn cleanup_object_url_lifetime_revokes_only_matching_urls() { + let store = BlobStore::::default(); + let blob = store.create_blob( + Some(1), + Some(10), + b"shared".to_vec(), + "text/plain".to_owned(), + ); + let first_url = store + .create_object_url_with_lifetime(Some(1), Some(101), blob, "https://example.test") + .expect("first object URL"); + let second_url = store + .create_object_url_with_lifetime(Some(1), Some(202), blob, "https://example.test") + .expect("second object URL"); + + assert_eq!(store.cleanup_object_url_lifetime(101), 1); + assert!(store.object_url_bytes_and_type(&first_url).is_none()); + assert_eq!( + store.object_url_bytes_and_type(&second_url), + Some((b"shared".to_vec(), "text/plain".to_owned())) + ); + + store.release_blob_wrapper_ref(blob); + assert_eq!(store.cleanup_object_url_lifetime(202), 1); + assert!(store.blob_bytes(blob).is_none()); + } } diff --git a/moli-renderer-v8/src/blob.rs b/moli-renderer-v8/src/blob.rs index 01c4cacab45..ff5cea52e0d 100644 --- a/moli-renderer-v8/src/blob.rs +++ b/moli-renderer-v8/src/blob.rs @@ -7,6 +7,7 @@ pub(super) use moli_webapi::blob::{ blob_size_from_object, build_blob_object, build_blob_object_from_data, build_blob_object_unpublished, install_blob_template_bindings, is_blob_object, }; +pub(crate) use moli_webapi::blob::{blob_id_from_object, world_wrapper}; use std::sync::{Arc, OnceLock}; use super::{ @@ -95,8 +96,11 @@ pub(super) fn create_object_url_for_object<'s>( let partition = current_blob_storage_partition_identity(scope)?; let owner_id = current_resource_owner_id(scope); let origin = storage_key.origin().to_owned(); - blob_store().create_object_url_with_access_key( + let lifetime_id = native_bridge::current_runtime_observable_context_token(scope) + .map(native_bridge::RuntimeObservableContextToken::as_u64); + blob_store().create_object_url_with_lifetime_and_access_key( owner_id, + lifetime_id, blob_id, &origin, Some(ObjectUrlAccessKey { @@ -165,6 +169,12 @@ pub(crate) fn cleanup_owner_resources(owner_id: ResourceOwnerId) { blob_store().retire_owner_resources(owner_id); } +pub(crate) fn cleanup_object_urls_for_context( + context_token: native_bridge::RuntimeObservableContextToken, +) -> usize { + blob_store().cleanup_object_url_lifetime(context_token.as_u64()) +} + pub(super) fn release_blob_wrapper_ref(blob_id: BlobId) { blob_store().release_blob_wrapper_ref(blob_id); } diff --git a/moli-renderer-v8/src/native_bridge/context_host/runtime_observable.rs b/moli-renderer-v8/src/native_bridge/context_host/runtime_observable.rs index 0cf58ad9aea..7b52ceb3a3e 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/runtime_observable.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/runtime_observable.rs @@ -567,6 +567,8 @@ impl PageScriptHost { context_token: RuntimeObservableContextToken, ) -> usize { self.retire_font_face_loads_for_context_token(context_token); + let revoked_blob_object_url_count = + crate::blob::cleanup_object_urls_for_context(context_token); crate::observer_runtime::retire_context_token(self, context_token); let retired_indexed_db_connections = self.retire_indexed_db_context(context_token); let owners = self @@ -591,13 +593,15 @@ impl PageScriptHost { ?context_token, retired_count, retired_indexed_db_connections, + revoked_blob_object_url_count, "retired LocalWindow bindings with destroyed V8 execution context" ); - } else if retired_indexed_db_connections > 0 { + } else if retired_indexed_db_connections > 0 || revoked_blob_object_url_count > 0 { tracing::debug!( ?context_token, retired_indexed_db_connections, - "retired IndexedDB state with destroyed V8 execution context" + revoked_blob_object_url_count, + "retired context-owned state with destroyed V8 execution context" ); } retired_count @@ -616,6 +620,8 @@ impl PageScriptHost { context_token: RuntimeObservableContextToken, ) -> usize { self.retire_font_face_loads_for_context_token(context_token); + let revoked_blob_object_url_count = + crate::blob::cleanup_object_urls_for_context(context_token); crate::observer_runtime::retire_context_token(self, context_token); self.retire_indexed_db_context(context_token); let retired_realm_count = self @@ -624,6 +630,7 @@ impl PageScriptHost { tracing::debug!( ?context_token, retired_realm_count, + revoked_blob_object_url_count, "retired isolated Window realm registration" ); retired_realm_count diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/blob_file_reader.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/blob_file_reader.rs index cce97edf18e..532f624bf7b 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/blob_file_reader.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/blob_file_reader.rs @@ -1170,3 +1170,54 @@ fn blob_members_enforce_webidl_receiver_contracts() { r#"{"syncThrows":[true,true,true,true],"promiseRejections":["TypeError","TypeError","TypeError"]}"# ); } + +#[test] +fn removing_child_frame_revokes_only_its_blob_object_urls() { + let mut vm = new_parsed_test_vm( + "https://blob-url-child-lifetime.test/", + "", + ); + + let urls = vm + .eval( + r#" +(() => { + const parentUrl = URL.createObjectURL(new Blob(["parent"])); + const frame = document.createElement("iframe"); + document.body.appendChild(frame); + const childUrl = frame.contentWindow.URL.createObjectURL( + new frame.contentWindow.Blob(["child"]) + ); + globalThis.__blobUrlLifetimeFrame = frame; + return `${parentUrl}|${childUrl}`; +})() +"#, + ) + .expect("child Blob object URL setup should evaluate"); + let (parent_url, child_url) = urls + .split_once('|') + .expect("setup should return both object URLs"); + + assert_eq!( + crate::blob::object_url_body_and_type(parent_url), + Some(("parent".to_owned(), String::new())) + ); + assert_eq!( + crate::blob::object_url_body_and_type(child_url), + Some(("child".to_owned(), String::new())) + ); + + vm.eval("globalThis.__blobUrlLifetimeFrame.remove()") + .expect("child frame removal should evaluate"); + vm.drain_pending_child_frame_work_for_test(); + + assert_eq!( + crate::blob::object_url_body_and_type(parent_url), + Some(("parent".to_owned(), String::new())), + "removing a child frame must preserve the parent realm's object URLs" + ); + assert!( + crate::blob::object_url_body_and_type(child_url).is_none(), + "removing a child frame must revoke object URLs created by its realm" + ); +}