Skip to content

armgit / moli

Public Imported from GitHub
0

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

refactor(webmcp): share committed document security origin queries

ldm035755220e9fa
diff --git a/moli-renderer-v8/src/context_bootstrap/web_mcp/bindings.rs b/moli-renderer-v8/src/context_bootstrap/web_mcp/bindings.rs
index 918c891aa..23f4bf13a 100644
--- a/moli-renderer-v8/src/context_bootstrap/web_mcp/bindings.rs
+++ b/moli-renderer-v8/src/context_bootstrap/web_mcp/bindings.rs
@@ -148,7 +148,7 @@ fn bind_document_target<'s>(
owner,
target: v8::Global::new(scope, target),
window: v8::Global::new(scope, window),
- origin: document_origin(host, document),
+ origin: host.document_security_origin(document),
frame_id: document_frame_id(host, document),
frame_tree: document_frame_tree(host, document),
tools: BTreeMap::new(),
@@ -210,27 +210,6 @@ fn document_window<'s>(
.unwrap_or_else(|| context.global(scope))
}
-pub(super) fn document_origin(host: &JsContextHost, document: DomHandle) -> url::Origin {
- if document == host.document_handle() && host.document_sandbox_policy().forces_opaque_origin
- || host
- .child_browsing_context_host_for_document_handle(document)
- .is_some_and(|child| host.child_browsing_context_has_opaque_origin(child))
- {
- return url::Origin::new_opaque();
- }
- host.lightweight_popup_id_for_document_handle(document)
- .and_then(|popup| host.lightweight_popup_origin(popup))
- .or_else(|| {
- host.child_browsing_context_host_for_document_handle(document)
- .and_then(|child| host.child_browsing_context_target_origin(child))
- })
- .and_then(|origin| url::Url::parse(&origin).ok())
- .map_or_else(
- || host.document_url_for_handle(document).origin(),
- |url| url.origin(),
- )
-}
-
pub(super) fn document_for_target<'s>(
scope: &mut v8::PinScope<'s, '_>,
target: v8::Local<'s, v8::Object>,
diff --git a/moli-renderer-v8/src/context_bootstrap/web_mcp/navigation.rs b/moli-renderer-v8/src/context_bootstrap/web_mcp/navigation.rs
index 80362a0b4..0dac510e7 100644
--- a/moli-renderer-v8/src/context_bootstrap/web_mcp/navigation.rs
+++ b/moli-renderer-v8/src/context_bootstrap/web_mcp/navigation.rs
@@ -107,13 +107,9 @@ pub(crate) fn commit_child_navigation(
if binding == Some(expected) {
// The owner store has committed, but the adapter's Document-handle
// map still points at the retired Document at this boundary.
- let origin = if host.child_browsing_context_has_opaque_origin(child) {
- url::Origin::new_opaque()
- } else {
- host.child_browsing_context_target_origin(child)
- .and_then(|origin| url::Url::parse(&origin).ok())
- .map_or_else(url::Origin::new_opaque, |url| url.origin())
- };
+ let origin = host
+ .child_document_security_origin(child)
+ .unwrap_or_else(url::Origin::new_opaque);
receive_navigation_from_origin(host, owner, id, origin);
} else {
fail_navigation(host, id);
@@ -126,7 +122,7 @@ pub(crate) fn receive_navigation(
owner: WindowDocumentOwner,
id: RendererWebMcpNavigation,
) {
- let origin = super::bindings::document_origin(host, host.document_handle());
+ let origin = host.document_security_origin(host.document_handle());
receive_navigation_from_origin(host, owner, id, origin);
}
@@ -188,7 +184,7 @@ pub(crate) fn complete_navigation(
.documents
.get(&document)
.map(|entry| entry.origin.clone())
- .unwrap_or_else(|| super::bindings::document_origin(host, document));
+ .unwrap_or_else(|| host.document_security_origin(document));
if origin != id.origin {
devtools::emit(host, cross_origin_failure_event(id));
return;
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 737e6a1b6..0ec562753 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
@@ -710,7 +710,7 @@ impl JsContextHost {
.node(handle)
.and_then(crate::dom::native::Node::owner_document)
.unwrap_or_else(|| self.document_handle());
- let parent_origin = self.permissions_policy_origin_for_document(parent_document);
+ let parent_origin = self.document_security_origin(parent_document);
let parent_policy = self
.document_permissions_policy_for_document_handle(parent_document)
.unwrap_or_else(|| self.document_policy_container().permissions_policy.clone());
diff --git a/moli-renderer-v8/src/native_bridge/context_host/security_policy.rs b/moli-renderer-v8/src/native_bridge/context_host/security_policy.rs
index 11531273a..5b94a742b 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/security_policy.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/security_policy.rs
@@ -35,6 +35,10 @@ struct OwnerDocumentPolicySnapshot {
const JAVASCRIPT_URL_TRUSTED_TYPES_SINK: &str = "Location href";
+fn security_origin_from_serialized(origin: &str) -> url::Origin {
+ url::Url::parse(origin).map_or_else(|_| url::Origin::new_opaque(), |url| url.origin())
+}
+
fn policy_child_window_handle(scope: &mut v8::PinScope<'_, '_>) -> Option<DomHandle> {
if let Some(handle) = active_child_window_handle(scope) {
return Some(handle);
@@ -168,14 +172,18 @@ impl JsContextHost {
)
}
- pub(in crate::native_bridge::context_host) fn permissions_policy_origin_for_document(
- &self,
- document: crate::document_runtime::DomHandle,
- ) -> url::Origin {
+ // Read the committed owner's security origin, including inherited blank
+ // documents and sandboxing. A pending navigation's URL is not authority.
+ pub(crate) fn document_security_origin(&self, document: DomHandle) -> url::Origin {
if document == self.document_handle() && self.document_sandbox_policy().forces_opaque_origin
{
return url::Origin::new_opaque();
}
+ if let Some(child) = self.child_browsing_context_host_for_document_handle(document) {
+ return self
+ .child_document_security_origin(child)
+ .unwrap_or_else(url::Origin::new_opaque);
+ }
let serialized = (document == self.document_handle())
.then(|| {
self.frame_owner_store
@@ -186,16 +194,26 @@ impl JsContextHost {
.or_else(|| {
self.lightweight_popup_id_for_document_handle(document)
.and_then(|popup| self.lightweight_popup_origin(popup))
- .or_else(|| {
- self.child_browsing_context_host_for_document_handle(document)
- .and_then(|child| self.child_browsing_context_target_origin(child))
- })
});
serialized.map_or_else(
|| self.document_url_for_handle(document).origin(),
- |origin| {
- url::Url::parse(&origin)
- .map_or_else(|_| url::Origin::new_opaque(), |url| url.origin())
+ |origin| security_origin_from_serialized(&origin),
+ )
+ }
+
+ // Commit observers run before the adapter updates its Document-handle map.
+ pub(crate) fn child_document_security_origin(&self, child: DomHandle) -> Option<url::Origin> {
+ let owner = self.frame_owner_store.current_child_owner_snapshot(child)?;
+ Some(
+ if owner
+ .settings
+ .document_policy_container
+ .sandbox
+ .forces_opaque_origin
+ {
+ url::Origin::new_opaque()
+ } else {
+ security_origin_from_serialized(&owner.settings.origin)
},
)
}