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): reject javascript: location assignments without ghosting href

BibekPathak38a07bad55a9
diff --git a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs
index 2cb92c0e0..376ccbdd5 100644
--- a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs
+++ b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs
@@ -627,7 +627,9 @@ fn navigate_location_object_with_source_element_and_child_navigate_event<'s>(
return;
}
- sync_location_object(scope, location, resolved.as_str());
+ if resolved.scheme() != "javascript" {
+ sync_location_object(scope, location, resolved.as_str());
+ }
let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) else {
return;
diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/lifecycle.rs b/moli-renderer-v8/src/runtime/page_vm/tests/lifecycle.rs
index c212d7b57..0ca4355b7 100644
--- a/moli-renderer-v8/src/runtime/page_vm/tests/lifecycle.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/tests/lifecycle.rs
@@ -928,6 +928,54 @@ globalThis.__events.push('after setter');
.await;
}
+#[tokio::test]
+async fn javascript_location_assignment_keeps_href_and_drops_pending_when_not_followed() {
+ run_page_vm_async_test(async move {
+ let page_vm = test_page_vm_with_document_url(
+ Url::parse("https://javascript-location-ghost.test/start.html").unwrap(),
+ );
+ let local_executor = page_vm.local_executor.clone();
+
+ local_executor
+ .run(async move {
+ let mut page_vm = page_vm;
+ let href = page_vm.vm_mut().eval(
+ r#"
+location.href = "javascript:document.title = 'LOC-RAN'";
+location.href
+"#,
+ )?;
+ assert_eq!(
+ href, "https://javascript-location-ghost.test/start.html",
+ "assigning a javascript: URL must not ghost location.href"
+ );
+ assert!(
+ page_vm.vm().has_pending_location_navigation(),
+ "javascript: location should remain pending until the owner follows it"
+ );
+ assert!(
+ !page_vm
+ .vm_mut()
+ .publish_pending_non_javascript_location_navigation()?,
+ "a javascript: pending navigation must never be published to the browser"
+ );
+ assert!(
+ !page_vm.vm().has_pending_location_navigation(),
+ "dropping a javascript: pending navigation must clear the pending record"
+ );
+ let href = page_vm.vm_mut().eval("location.href")?;
+ assert_eq!(
+ href, "https://javascript-location-ghost.test/start.html",
+ "location.href must remain unchanged after the pending navigation is dropped"
+ );
+ Ok::<_, anyhow::Error>(())
+ })
+ .await
+ .expect("javascript: location ghost-state rejection should complete");
+ })
+ .await;
+}
+
#[tokio::test]
async fn javascript_string_completion_restarts_renderer_lifecycle_on_same_document_token() {
run_page_vm_async_test(async move {
diff --git a/moli-renderer-v8/src/runtime/tests.rs b/moli-renderer-v8/src/runtime/tests.rs
index 5066bda7d..40e462417 100644
--- a/moli-renderer-v8/src/runtime/tests.rs
+++ b/moli-renderer-v8/src/runtime/tests.rs
@@ -1872,6 +1872,67 @@ document.body.appendChild(frame);
.expect("ignored child navigation page should close");
}
+#[tokio::test(flavor = "multi_thread")]
+async fn javascript_location_assignment_does_not_ghost_location_href_on_delegate_pages() {
+ let runtime = JsRuntime::initialize();
+ let loader =
+ ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("default loader");
+ let url = url::Url::parse("https://javascript-location-ghost.test/start.html").unwrap();
+ let mut page = create_test_html_page_with_navigation_dispatch(
+ &runtime,
+ &loader,
+ url,
+ "<!doctype html><title>start</title>",
+ RendererTopLevelNavigationDispatch::DelegateToBrowser,
+ )
+ .await;
+
+ let (set_reply, _) = page
+ .run_async_command(RendererPageCommand::EvaluateExpression {
+ expression: r#"location.href = "javascript:document.title = 'LOC-RAN'"; "set""#
+ .to_owned(),
+ await_promise: false,
+ })
+ .await
+ .expect("javascript: location assignment should complete");
+ assert_eq!(
+ renderer_json_value(set_reply),
+ Some(serde_json::json!("set"))
+ );
+
+ let (href_reply, _) = page
+ .run_async_command(RendererPageCommand::EvaluateExpression {
+ expression: "location.href".to_owned(),
+ await_promise: false,
+ })
+ .await
+ .expect("location.href readback should complete");
+ assert_eq!(
+ renderer_json_value(href_reply),
+ Some(serde_json::json!(
+ "https://javascript-location-ghost.test/start.html"
+ )),
+ "assigning a javascript: URL must not ghost location.href"
+ );
+
+ let (title_reply, _) = page
+ .run_async_command(RendererPageCommand::EvaluateExpression {
+ expression: "document.title".to_owned(),
+ await_promise: false,
+ })
+ .await
+ .expect("document.title readback should complete");
+ assert_eq!(
+ renderer_json_value(title_reply),
+ Some(serde_json::json!("start")),
+ "a javascript: location assignment must not execute its script"
+ );
+
+ page.close_async()
+ .await
+ .expect("javascript location ghost page should close");
+}
+
#[tokio::test(flavor = "multi_thread")]
async fn owner_scheduler_continues_from_stale_to_latest_child_navigation_generation() {
let runtime = JsRuntime::initialize();
diff --git a/moli-renderer-v8/src/script_vm.rs b/moli-renderer-v8/src/script_vm.rs
index 2664f756d..ee5dbc124 100644
--- a/moli-renderer-v8/src/script_vm.rs
+++ b/moli-renderer-v8/src/script_vm.rs
@@ -5550,6 +5550,11 @@ impl ScriptVm {
&mut self,
) -> Option<super::native_bridge::PendingLocationNavigation> {
if self.pending_location_navigation_scheme_is("javascript") {
+ let source_url = self.document_runtime.document_url().clone();
+ self._context_host
+ .borrow_mut()
+ .clear_pending_location_navigation();
+ self.restore_top_level_location_runtime_state(&source_url);
return None;
}
let source_url = self.document_runtime.document_url().clone();