Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(window): resolve API URLs against entry realms

ldm0da38689cc53d
diff --git a/moli-renderer-v8/src/context_bootstrap/window_runtime/dialogs.rs b/moli-renderer-v8/src/context_bootstrap/window_runtime/dialogs.rs
index 1c7c2716f..1796538c1 100644
--- a/moli-renderer-v8/src/context_bootstrap/window_runtime/dialogs.rs
+++ b/moli-renderer-v8/src/context_bootstrap/window_runtime/dialogs.rs
@@ -305,13 +305,31 @@ pub(in crate::context_bootstrap) fn entered_window_api_base_url(
scope: &mut v8::PinScope<'_, '_>,
host: &crate::native_bridge::JsContextHost,
) -> Url {
- if let Some(handle) = entered_child_window_handle(scope)
- && let Some(url) = host.child_browsing_context_base_url(handle)
- {
- return url;
- }
- if let Some(url) = host.active_lightweight_popup_base_url(scope) {
- return url;
+ // The HTML entry settings object follows the context that entered the
+ // script or microtask, not the borrowed Window method's native realm.
+ let ambient_scope = host.entered_owner_dispatch_scope(scope);
+ let entry_scope = match ambient_scope {
+ crate::native_bridge::OwnerDispatchScope::LightweightPopup(_) => ambient_scope,
+ crate::native_bridge::OwnerDispatchScope::Top
+ | crate::native_bridge::OwnerDispatchScope::Child(_) => {
+ let entry_context = scope.get_entered_or_microtask_context();
+ host.window_execution_context_identity_for_access_check(entry_context)
+ .map(|identity| identity.dispatch_scope())
+ .unwrap_or(ambient_scope)
+ }
+ };
+ match entry_scope {
+ crate::native_bridge::OwnerDispatchScope::Child(handle) => {
+ if let Some(url) = host.child_browsing_context_base_url(handle) {
+ return url;
+ }
+ }
+ crate::native_bridge::OwnerDispatchScope::LightweightPopup(popup_id) => {
+ if let Some(url) = host.lightweight_popup_request_base_url(scope, popup_id) {
+ return url;
+ }
+ }
+ crate::native_bridge::OwnerDispatchScope::Top => {}
}
host.dom_host()
.document_base_url()
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
index 87878b4f2..5d80213a4 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
@@ -23822,6 +23822,67 @@ fn window_open_rejects_invalid_urls_before_selecting_a_target() {
assert_eq!(result, "SyntaxError:true:12|SyntaxError:true:12");
}
+#[test]
+fn window_open_resolves_relative_urls_against_the_entry_function_realm() {
+ let mut vm = new_storage_test_vm("https://entry-realm.test/top/page.html");
+
+ let result = vm
+ .eval(
+ r#"
+ (() => {
+ const parent = document.body || document.documentElement || document;
+ const makeFrame = base => {
+ const frame = document.createElement("iframe");
+ parent.appendChild(frame);
+ frame.contentDocument.open();
+ frame.contentDocument.write(
+ `<!doctype html><base href="https://entry-realm.test/${base}/">`
+ );
+ frame.contentDocument.close();
+ return frame.contentWindow;
+ };
+ const current = makeFrame("current");
+ const relevant = makeFrame("relevant");
+ const entry = makeFrame("function");
+ entry.openArgs = { open: current.open, relevant };
+
+ const makeEntryFunction = target => entry.Function(
+ `window.openArgs.open.call(
+ window.openArgs.relevant,
+ "resources/window-to-open.html",
+ "${target}"
+ );`
+ );
+ Promise.resolve().then(makeEntryFunction("promise-entry"));
+
+ const startFunction = makeEntryFunction("wasm-entry");
+ const startModule = new WebAssembly.Module(new Uint8Array([
+ 0, 97, 115, 109, 1, 0, 0, 0,
+ 1, 4, 1, 96, 0, 0,
+ 2, 19, 1, 6, 109, 111, 100, 117, 108, 101,
+ 8, 105, 109, 112, 111, 114, 116, 101, 100, 0, 0,
+ 8, 1, 0
+ ]));
+ new WebAssembly.Instance(startModule, {
+ module: { imported: startFunction }
+ });
+ return "queued";
+ })()
+ "#,
+ )
+ .expect("cross-realm entry window.open calls should evaluate");
+ assert_eq!(result, "queued");
+
+ let mut activations = vm.take_pending_popup_activations();
+ activations.sort_by(|left, right| left.target_name().cmp(right.target_name()));
+ assert_eq!(activations.len(), 2);
+ assert_eq!(activations[0].target_name(), "promise-entry");
+ assert_eq!(activations[1].target_name(), "wasm-entry");
+ assert!(activations.iter().all(|activation| {
+ activation.url() == "https://entry-realm.test/function/resources/window-to-open.html"
+ }));
+}
+
#[test]
fn window_open_about_blank_returns_lightweight_popup_window() {
let mut vm = new_storage_test_vm("https://example.com/");