Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(renderer): bind WebStorage to receiver window

ldm03212b02cac6a
diff --git a/moli-renderer-v8/src/context_bootstrap/web_storage/helpers.rs b/moli-renderer-v8/src/context_bootstrap/web_storage/helpers.rs
index abfd80eeb..fa234e596 100644
--- a/moli-renderer-v8/src/context_bootstrap/web_storage/helpers.rs
+++ b/moli-renderer-v8/src/context_bootstrap/web_storage/helpers.rs
@@ -118,8 +118,7 @@ pub(super) fn storage_access_allows_web_storage_for_window<'s>(
super::super::navigation_window::child_browsing_context_handle_for_runtime_owner(
scope, owner,
)
- })
- .or_else(|| crate::native_bridge::active_child_window_handle(scope));
+ });
let runtime = unsafe { &mut *host_ptr };
let context = if let Some(handle) = child_handle {
let Some(context) = runtime.storage_context_for_child_browsing_context(handle) else {
@@ -141,12 +140,9 @@ pub(super) fn current_storage_host_for_owner(
let host_ptr = context_host_ptr_from_global_bridge(scope)?;
let runtime = unsafe { &mut *host_ptr };
let (origin, area_key) = match owner {
- WebStorageOwner::ActiveDocument => {
- let active_child_handle = crate::native_bridge::active_child_window_handle(scope);
- runtime
- .active_storage_context(scope, active_child_handle)
- .into_origin_and_area_key()
- }
+ WebStorageOwner::ActiveDocument => runtime
+ .top_document_storage_context()
+ .into_origin_and_area_key(),
WebStorageOwner::Child(handle) => runtime
.storage_context_for_child_browsing_context(handle)?
.into_origin_and_area_key(),
@@ -341,43 +337,12 @@ fn queue_storage_event(
)
}
WebStorageOwner::ActiveDocument => {
- let active_child_handle = crate::native_bridge::active_child_window_handle(scope)
- .or_else(|| {
- host.active_child_subresource_request_scope()
- .map(|(handle, _, _)| handle)
- });
- let storage_context = host.active_storage_context(scope, active_child_handle);
- let source_scope = match storage_context.owner() {
- crate::native_bridge::ActiveStorageContextOwner::TopDocument => {
- OwnerDispatchScope::Top
- }
- crate::native_bridge::ActiveStorageContextOwner::Child(handle) => {
- OwnerDispatchScope::Child(handle)
- }
- crate::native_bridge::ActiveStorageContextOwner::LightweightPopup(popup_id) => {
- OwnerDispatchScope::LightweightPopup(popup_id)
- }
- };
- let url = match storage_context.owner() {
- crate::native_bridge::ActiveStorageContextOwner::TopDocument => {
- host.document_url().to_string()
- }
- crate::native_bridge::ActiveStorageContextOwner::Child(handle) => host
- .child_browsing_context_current_url(handle)
- .map(|url| url.to_string())
- .unwrap_or_else(|| host.document_url().to_string()),
- crate::native_bridge::ActiveStorageContextOwner::LightweightPopup(popup_id) => {
- let Some(document_url) = host.lightweight_popup_document_url(popup_id) else {
- return;
- };
- document_url.to_string()
- }
- };
+ let storage_context = host.top_document_storage_context();
(
- source_scope,
+ OwnerDispatchScope::Top,
storage_context.origin().to_owned(),
storage_context.web_storage_area_key().to_owned(),
- url,
+ host.document_url().to_string(),
)
}
};
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames.rs
index 634a37149..7959820aa 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frames.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames.rs
@@ -33,7 +33,7 @@ mod stylesheets;
pub(in crate::native_bridge::context_host) use classic_scripts::ChildParserClassicScriptCandidate;
pub(in crate::native_bridge::context_host) use classic_scripts::PendingChildExternalClassicDocumentScriptLoad;
-pub(crate) use request_scope::{ActiveStorageContextOwner, WebStorageScope};
+pub(crate) use request_scope::WebStorageScope;
pub(in crate::native_bridge::context_host) use request_scope::{
document_sandbox_policy_from_attribute, sandbox_attribute_forces_opaque_origin,
};
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs
index 5e0bbb78d..e4e0be65d 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs
@@ -84,26 +84,14 @@ impl WebStorageScope {
}
}
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub(crate) enum ActiveStorageContextOwner {
- TopDocument,
- Child(DomHandle),
- LightweightPopup(u64),
-}
-
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ActiveStorageContext {
- owner: ActiveStorageContextOwner,
scope: StorageContextScope,
}
impl ActiveStorageContext {
- fn new(owner: ActiveStorageContextOwner, scope: StorageContextScope) -> Self {
- Self { owner, scope }
- }
-
- pub(crate) fn owner(&self) -> ActiveStorageContextOwner {
- self.owner
+ fn new(scope: StorageContextScope) -> Self {
+ Self { scope }
}
pub(crate) fn origin(&self) -> &str {
@@ -459,12 +447,7 @@ impl JsContextHost {
) -> Option<ActiveStorageContext> {
let top_origin = moli_url::origin_ascii_serialization(self.document_url());
self.child_browsing_context_web_storage_scope(handle, &top_origin)
- .map(|scope| {
- ActiveStorageContext::new(
- ActiveStorageContextOwner::Child(handle),
- scope.into_storage_context(),
- )
- })
+ .map(|scope| ActiveStorageContext::new(scope.into_storage_context()))
}
pub(crate) fn storage_context_for_lightweight_popup(
@@ -472,18 +455,12 @@ impl JsContextHost {
popup_id: u64,
) -> Option<ActiveStorageContext> {
self.lightweight_popup_bound_web_storage_scope(popup_id)
- .map(|scope| {
- ActiveStorageContext::new(
- ActiveStorageContextOwner::LightweightPopup(popup_id),
- scope.into_storage_context(),
- )
- })
+ .map(|scope| ActiveStorageContext::new(scope.into_storage_context()))
}
pub(crate) fn top_document_storage_context(&mut self) -> ActiveStorageContext {
let top_origin = moli_url::origin_ascii_serialization(self.document_url());
ActiveStorageContext::new(
- ActiveStorageContextOwner::TopDocument,
self.web_storage_scope_for_top_document_origin(&top_origin)
.into_storage_context(),
)
diff --git a/moli-renderer-v8/src/native_bridge/context_host/mod.rs b/moli-renderer-v8/src/native_bridge/context_host/mod.rs
index 7516aff75..c5c535d89 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/mod.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/mod.rs
@@ -183,7 +183,6 @@ pub(crate) use child_frame_snapshots::{
ChildBrowsingContextDocumentSnapshot, ChildBrowsingContextFrameSnapshot,
ChildBrowsingContextSnapshot, DetachedChildBrowsingContextDocumentSnapshot,
};
-pub(crate) use child_frames::ActiveStorageContextOwner;
use child_frames::ChildBrowsingContextEntry;
pub(in crate::native_bridge::context_host) use child_frames::ChildParserClassicScriptCandidate;
pub(crate) use event_callbacks::{EventCallbackId, PreparedEventCallback};
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage.rs
index d3947b10c..d83bc754c 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage.rs
@@ -6228,6 +6228,55 @@ async fn opaque_origin_frames_reject_web_storage_access() {
);
}
+#[test]
+fn top_web_storage_receiver_ignores_ambient_opaque_child_owner() {
+ let mut vm = new_storage_test_vm("https://top-web-storage-owner.test/page.html");
+ vm.eval(
+ r#"
+(() => {
+ localStorage.clear();
+ localStorage.setItem("top-key", "top-value");
+ const frame = document.createElement("iframe");
+ frame.setAttribute("sandbox", "allow-scripts");
+ frame.srcdoc = "<!doctype html><script>void localStorage;<\/script>";
+ (document.body || document.documentElement || document).appendChild(frame);
+})()
+"#,
+ )
+ .expect("top WebStorage owner setup should evaluate");
+
+ let child_handle = vm
+ ._context_host
+ .borrow()
+ .child_browsing_context_handles_in_document_order()[0];
+ assert!(
+ vm._context_host
+ .borrow()
+ .child_browsing_context_has_opaque_origin(child_handle),
+ "sandboxed child should have an opaque storage origin"
+ );
+
+ let top_context_ptr = &vm.page_default_context as *const v8::Global<v8::Context>;
+ vm.with_context_scope_by_ptr(top_context_ptr, |scope, _host_ptr| {
+ let _previous =
+ crate::native_bridge::enter_active_child_window_scope(scope, Some(child_handle));
+ Ok(())
+ })
+ .expect("ambient opaque child owner should install");
+
+ let result = vm.eval("localStorage.getItem('top-key')");
+ vm.with_context_scope_by_ptr(top_context_ptr, |scope, _host_ptr| {
+ let _previous = crate::native_bridge::enter_active_child_window_scope(scope, None);
+ Ok(())
+ })
+ .expect("ambient opaque child owner should clear");
+
+ assert_eq!(
+ result.expect("top localStorage should use the top Window receiver"),
+ "top-value"
+ );
+}
+
#[tokio::test]
async fn third_party_web_storage_is_partitioned_by_top_level_site() {
let (child_origin, server) = spawn_web_storage_partition_child_server(3).await;