Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(performance): keep worker performance cache private

ldm0946ecbd01ff2
diff --git a/moli-renderer-v8/src/context_bootstrap.rs b/moli-renderer-v8/src/context_bootstrap.rs
index e9d42543b..eeed7a17a 100644
--- a/moli-renderer-v8/src/context_bootstrap.rs
+++ b/moli-renderer-v8/src/context_bootstrap.rs
@@ -843,7 +843,7 @@ fn install_worker_performance_global_attribute<'s>(
scope: &mut v8::PinScope<'s, '_>,
global: v8::Local<'s, v8::Object>,
) -> Result<()> {
- let performance = get_own_static_property(scope, global, WINDOW_PERFORMANCE_SLOT)
+ let performance = get_private_value(scope, global, WINDOW_PERFORMANCE_SLOT)
.filter(|value| !value.is_undefined())
.ok_or_else(|| anyhow!("worker performance runtime state did not install performance"))?;
let Some(prototype) = global_constructor_prototype(scope, "WorkerGlobalScope") else {
@@ -863,7 +863,7 @@ fn worker_performance_getter_callback<'s>(
return;
}
let global = scope.get_current_context().global(scope);
- match get_own_static_property(scope, global, WINDOW_PERFORMANCE_SLOT)
+ match get_private_value(scope, global, WINDOW_PERFORMANCE_SLOT)
.filter(|value| !value.is_undefined())
{
Some(value) => rv.set(value),
diff --git a/moli-renderer-v8/src/context_bootstrap/performance_runtime/install.rs b/moli-renderer-v8/src/context_bootstrap/performance_runtime/install.rs
index f165f3a48..a76f3c5da 100644
--- a/moli-renderer-v8/src/context_bootstrap/performance_runtime/install.rs
+++ b/moli-renderer-v8/src/context_bootstrap/performance_runtime/install.rs
@@ -378,7 +378,8 @@ pub(in crate::context_bootstrap) fn install_worker_performance_runtime_state<'s>
.map_err(|error| anyhow!("failed to create worker performance: {error}"))?;
install_simple_event_target_methods(scope, performance);
- define_global_value(scope, global, WINDOW_PERFORMANCE_SLOT, performance.into())
+ set_private_value(scope, global, WINDOW_PERFORMANCE_SLOT, performance.into());
+ Ok(())
}
pub(super) fn install_performance_observer_runtime_state<'s>(
diff --git a/moli-renderer-v8/src/worker/thread/tests/lifecycle/worker_and_fetch_lifecycle.rs b/moli-renderer-v8/src/worker/thread/tests/lifecycle/worker_and_fetch_lifecycle.rs
index 5ecb75425..4cd346f31 100644
--- a/moli-renderer-v8/src/worker/thread/tests/lifecycle/worker_and_fetch_lifecycle.rs
+++ b/moli-renderer-v8/src/worker/thread/tests/lifecycle/worker_and_fetch_lifecycle.rs
@@ -1942,3 +1942,39 @@ async fn service_worker_fetch_event_handled_follows_respond_with_result() {
));
handle.terminate_and_join();
}
+
+#[tokio::test]
+async fn worker_performance_cache_is_private_and_cannot_be_spoofed_by_author_properties() {
+ ensure_v8();
+ let mut handle = spawn_worker(
+ r#"
+ const original = performance;
+ const leaked = Reflect.ownKeys(globalThis).includes("__moliWindowPerformance");
+ const descriptorAbsent = Object.getOwnPropertyDescriptor(
+ globalThis, "__moliWindowPerformance") === undefined;
+ Reflect.set(globalThis, "__moliWindowPerformance", { poisoned: true });
+ const entry = performance.mark("private-cache");
+ postMessage({
+ leaked,
+ descriptorAbsent,
+ samePerformance: performance === original,
+ hasEntryId: Number.isSafeInteger(entry.id) && entry.id > 0,
+ serializedEntryId: entry.toJSON().id === entry.id
+ });
+ "#
+ .into(),
+ "test://worker-private-performance-cache".into(),
+ );
+ let message = timeout(TIMEOUT, handle.recv())
+ .await
+ .expect("timed out waiting for worker performance cache probe")
+ .expect("worker closed before performance cache probe completed");
+ match message {
+ WorkerToParentMessage::Post(payload) => assert_eq!(
+ stringify_payload(&payload),
+ r#"{"leaked":false,"descriptorAbsent":true,"samePerformance":true,"hasEntryId":true,"serializedEntryId":true}"#,
+ ),
+ other => panic!("unexpected worker performance cache result: {other:?}"),
+ }
+ handle.terminate_and_join();
+}