fix(blob): revoke object URLs with window realms

This commit is contained in:
ldm0
2026-11-20 22:47:33 +08:00
parent e42620b7cf
commit a893c8f680
6 changed files with 158 additions and 5 deletions
@@ -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
@@ -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
+84 -2
View File
@@ -53,6 +53,7 @@ impl<OwnerId, PartitionId> Default for BlobEntries<OwnerId, PartitionId> {
#[derive(Debug)]
struct ObjectUrlState<OwnerId, AccessKey> {
owner_id: Option<OwnerId>,
lifetime_id: Option<u64>,
blob_id: BlobId,
access_key: Option<AccessKey>,
}
@@ -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<OwnerId>,
lifetime_id: Option<u64>,
blob_id: BlobId,
origin: &str,
) -> Option<String> {
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<OwnerId>,
blob_id: BlobId,
origin: &str,
access_key: Option<AccessKey>,
) -> Option<String> {
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<OwnerId>,
lifetime_id: Option<u64>,
blob_id: BlobId,
origin: &str,
access_key: Option<AccessKey>,
) -> Option<String> {
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::<u64, u64>::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());
}
}
+11 -1
View File
@@ -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);
}
@@ -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
@@ -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/",
"<!doctype html><html><body></body></html>",
);
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"
);
}