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): enforce source sandbox for top-level hyperlinks

ldm00d90fb8f3142
diff --git a/moli-renderer-v8/src/context_bootstrap.rs b/moli-renderer-v8/src/context_bootstrap.rs
index bf7ad5a2f..3b69b9675 100644
--- a/moli-renderer-v8/src/context_bootstrap.rs
+++ b/moli-renderer-v8/src/context_bootstrap.rs
@@ -153,6 +153,7 @@ pub(crate) use location_navigation::{
navigate_location_object_with_child_navigate_event,
navigate_location_object_with_source_element, navigate_top_level_meta_refresh,
navigate_top_level_same_document_from_browser, resolve_cross_window_location_target,
+ sandbox_blocks_ancestor_or_top_navigation_from_source,
};
pub(crate) use navigation_cancellation::{
NavigationCancellationReason, inform_about_canceled_navigation_for_window,
diff --git a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs
index 28155e8b0..31958c74f 100644
--- a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs
+++ b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs
@@ -754,7 +754,7 @@ fn sandbox_blocks_ancestor_or_top_location_navigation<'s>(
sandbox_blocks_ancestor_or_top_navigation_from_source(scope, source_handle, owner)
}
-pub(super) fn sandbox_blocks_ancestor_or_top_navigation_from_source<'s>(
+pub(crate) fn sandbox_blocks_ancestor_or_top_navigation_from_source<'s>(
scope: &mut v8::PinScope<'s, '_>,
source_handle: crate::document_runtime::DomHandle,
owner: v8::Local<'s, v8::Object>,
diff --git a/moli-renderer-v8/src/native_bridge/element/activation/targets.rs b/moli-renderer-v8/src/native_bridge/element/activation/targets.rs
index fd068f4e6..da5bb3d3b 100644
--- a/moli-renderer-v8/src/native_bridge/element/activation/targets.rs
+++ b/moli-renderer-v8/src/native_bridge/element/activation/targets.rs
@@ -3,6 +3,7 @@ use crate::{
context_bootstrap::{
dispatch_cross_document_navigation_navigate_event_for_window,
dispatch_top_level_navigation_event_with_source_element, runtime_window_dispatch_scope,
+ sandbox_blocks_ancestor_or_top_navigation_from_source,
},
document_runtime::{DocumentPolicyContainer, DomHandle},
native_bridge::context_host::ChildBrowsingContextNavigationRequest,
@@ -695,6 +696,16 @@ pub(in crate::native_bridge) fn navigate_hyperlink_target_browsing_context<'s>(
runtime_window_dispatch_scope(scope, target_window),
Some(crate::native_bridge::OwnerDispatchScope::Top)
) {
+ // Check the hyperlink's native Document before any event or intercepted commit.
+ if let crate::native_bridge::OwnerDispatchScope::Child(source_frame) = dispatch_scope
+ && sandbox_blocks_ancestor_or_top_navigation_from_source(
+ scope,
+ source_frame,
+ target_window,
+ )
+ {
+ return true;
+ }
let runtime = unsafe { &*runtime_ptr };
let can_intercept = url::Url::parse(resolved_url)
.is_ok_and(|url| moli_url::same_origin(runtime.document_url(), &url));
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/element_navigation.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/element_navigation.rs
index 23f1ff645..83610f3d6 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/element_navigation.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/element_navigation.rs
@@ -226,6 +226,135 @@ fn child_navigation_to_top_fires_once_and_honors_cancel_or_intercept() {
}
}
+#[test]
+fn hyperlink_top_navigation_checks_source_document_sandbox_across_realms() {
+ for (depth, target) in [(1, "_top"), (1, "_PARENT"), (2, "_ToP")] {
+ for source_is_child in [true, false] {
+ for allow_top_navigation in [false, true] {
+ for use_child_realm in [true, false] {
+ for intercept in [false, true] {
+ let mut vm = new_parsed_test_vm(
+ "https://anchor-navigation-sandbox.test/source",
+ "<!doctype html><body></body>",
+ );
+ let description = format!(
+ "depth={depth} target={target} source_is_child={source_is_child} \
+ allow_top_navigation={allow_top_navigation} \
+ child_realm={use_child_realm} intercept={intercept}"
+ );
+ vm.eval(&format!(
+ r#"
+ (() => {{
+ const log = [];
+ let childDocument = document;
+ for (let i = 0; i < {depth}; ++i) {{
+ const frame = childDocument.createElement('iframe');
+ frame.sandbox = 'allow-scripts allow-same-origin' +
+ ({allow_top_navigation} ? ' allow-top-navigation' : '');
+ frame.srcdoc = '<body></body>';
+ childDocument.body.appendChild(frame);
+ childDocument = frame.contentDocument;
+ frame.contentWindow.navigation.onnavigate = () => log.push('child');
+ }}
+ const source = {source_is_child} ? childDocument : document;
+ const anchor = source.createElement('a');
+ anchor.href = 'https://anchor-navigation-sandbox.test/destination';
+ anchor.target = {target:?};
+ source.body.appendChild(anchor);
+ const from = navigation.currentEntry;
+ const initialHistoryLength = history.length;
+ navigation.onnavigate = event => {{
+ log.push('navigate');
+ if ({intercept}) event.intercept({{
+ handler() {{
+ log.push([
+ 'handler',
+ location.href,
+ navigation.currentEntry.url,
+ navigation.transition instanceof NavigationTransition,
+ navigation.transition.from === from,
+ navigation.transition.navigationType
+ ]);
+ return Promise.resolve().then(() => log.push('handler-complete'));
+ }}
+ }});
+ }};
+ navigation.oncurrententrychange = () => log.push('currententrychange');
+ navigation.onnavigatesuccess = () => log.push('success');
+ navigation.onnavigateerror = () => log.push('error');
+ globalThis.__lmSandboxLinkSnapshot = () => ({{
+ log,
+ href: location.href,
+ entryUrl: navigation.currentEntry.url,
+ entryUnchanged: navigation.currentEntry === from,
+ historyDelta: history.length - initialHistoryLength,
+ transition: navigation.transition
+ }});
+ const realm = {use_child_realm} ? childDocument.defaultView : window;
+ realm.HTMLElement.prototype.click.call(anchor);
+ }})()
+ "#,
+ ))
+ .expect(&description);
+ let allowed = !source_is_child || allow_top_navigation;
+ let committed = allowed && intercept;
+ let expected_url = "https://anchor-navigation-sandbox.test/destination";
+ let mut expected_log = Vec::new();
+ if allowed {
+ expected_log.push(serde_json::json!("navigate"));
+ }
+ if committed {
+ expected_log.extend([
+ serde_json::json!("currententrychange"),
+ serde_json::json!([
+ "handler",
+ expected_url,
+ expected_url,
+ true,
+ true,
+ "push"
+ ]),
+ serde_json::json!("handler-complete"),
+ serde_json::json!("success"),
+ ]);
+ }
+ let committed_url = if committed {
+ expected_url
+ } else {
+ "https://anchor-navigation-sandbox.test/source"
+ };
+ let result = vm
+ .eval("JSON.stringify(__lmSandboxLinkSnapshot())")
+ .expect(&description);
+ assert_eq!(
+ serde_json::from_str::<serde_json::Value>(&result).unwrap(),
+ serde_json::json!({
+ "log": expected_log,
+ "href": committed_url,
+ "entryUrl": committed_url,
+ "entryUnchanged": !committed,
+ "historyDelta": if committed { 1 } else { 0 },
+ "transition": null
+ }),
+ "{description}"
+ );
+ let pending = vm.take_pending_location_navigation_with_seed();
+ if allowed && !intercept {
+ assert_eq!(
+ pending.expect(&description).url.as_str(),
+ expected_url,
+ "{description}"
+ );
+ } else {
+ assert!(pending.is_none(), "{description}");
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
#[test]
fn form_navigation_events_use_resolved_top_window_across_realms() {
for (depth, target) in [(1, "_top"), (1, "_PARENT"), (2, "_ToP")] {