Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(xhr): keep open exceptions in the invoked method realm

ldm0d31b51af4c75
diff --git a/moli-renderer-v8/src/network_host/xhr/bindings/open.rs b/moli-renderer-v8/src/network_host/xhr/bindings/open.rs
index 102ecf081..08d6bf322 100644
--- a/moli-renderer-v8/src/network_host/xhr/bindings/open.rs
+++ b/moli-renderer-v8/src/network_host/xhr/bindings/open.rs
@@ -139,7 +139,7 @@ fn xhr_open_url_context(
})
} else {
let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) else {
- xhr_open_throw_invalid_state(
+ xhr_throw_invalid_state(
scope,
"Failed to execute 'open' on 'XMLHttpRequest': The object's document is not fully active.",
);
@@ -147,7 +147,7 @@ fn xhr_open_url_context(
};
let host = unsafe { &*host_ptr };
let Some(execution_context) = xhr_execution_context_binding(scope, host, xhr) else {
- xhr_open_throw_invalid_state(
+ xhr_throw_invalid_state(
scope,
"Failed to execute 'open' on 'XMLHttpRequest': The object's document is not fully active.",
);
@@ -176,7 +176,7 @@ fn xhr_open_url_context(
})
};
if context.is_none() {
- xhr_open_throw_invalid_state(
+ xhr_throw_invalid_state(
scope,
"Failed to execute 'open' on 'XMLHttpRequest': The object's execution context is unavailable.",
);
@@ -214,13 +214,6 @@ fn xhr_open_request_url(
Some(request_url)
}
-fn xhr_open_throw_invalid_state(scope: &mut v8::PinScope<'_, '_>, message: &'static str) {
- let current_context = scope.get_current_context();
- let incumbent_context = scope.get_incumbent_context().unwrap_or(current_context);
- let incumbent_scope = &mut v8::ContextScope::new(scope, incumbent_context);
- xhr_throw_invalid_state(incumbent_scope, message);
-}
-
#[cfg(test)]
mod tests {
use super::XhrOpenUrlContext;
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs
index 1141185ab..a30baa16e 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs
@@ -4071,3 +4071,78 @@ fn xml_http_request_queued_document_response_uses_parsed_overrides_and_xml_error
}
assert_eq!(result, serde_json::to_string(&expected).unwrap());
}
+
+#[test]
+fn xhr_open_inactive_document_errors_use_the_invoked_method_realm() {
+ for callee_is_child in [false, true] {
+ for retire_during_conversion in [false, true] {
+ let mut vm = new_parsed_test_vm(
+ "https://xhr-open-exception-realm.test/",
+ "<!doctype html><body><iframe id='xhr-owner'></iframe></body>",
+ );
+ materialize_single_child_default_realm_for_test(&mut vm, "XHR exception realm");
+ let result = vm
+ .eval(&format!(
+ r#"
+(() => {{
+ const frame = document.getElementById('xhr-owner');
+ const owner = frame.contentWindow;
+ const xhr = new owner.XMLHttpRequest();
+ const callee = {callee_is_child} ? owner : window;
+ const open = callee.XMLHttpRequest.prototype.open;
+ const expectedPrototype = callee.DOMException.prototype;
+ let conversions = 0;
+ const url = {{toString() {{
+ conversions++;
+ if ({retire_during_conversion}) frame.remove();
+ return '/resource';
+ }}}};
+ if (!{retire_during_conversion}) frame.remove();
+ let caught;
+ try {{ open.call(xhr, 'GET', url); }} catch (error) {{ caught = error; }}
+ return JSON.stringify([
+ caught && caught.name, caught && caught.code,
+ !!caught && Object.getPrototypeOf(caught) === expectedPrototype,
+ conversions, xhr.readyState
+ ]);
+}})()
+"#
+ ))
+ .expect("retired XHR owner probe should run");
+ assert_eq!(
+ result, r#"["InvalidStateError",11,true,1,0]"#,
+ "callee_is_child={callee_is_child}, retire_during_conversion={retire_during_conversion}",
+ );
+ }
+ }
+}
+
+#[test]
+fn xhr_open_live_document_errors_use_the_invoked_method_realm() {
+ let mut vm = new_parsed_test_vm(
+ "https://xhr-open-live-exception-realm.test/",
+ "<!doctype html><body><iframe id='xhr-owner'></iframe></body>",
+ );
+ materialize_single_child_default_realm_for_test(&mut vm, "live XHR exception realm");
+ let result = vm.eval(r#"
+(() => {
+ const child = document.getElementById('xhr-owner').contentWindow;
+ for (const callee of [window, child]) {
+ for (const owner of [window, child]) {
+ const xhr = new owner.XMLHttpRequest();
+ xhr.timeout = 1;
+ let caught;
+ try {
+ callee.XMLHttpRequest.prototype.open.call(xhr, 'GET', 'https://example.test/resource', false);
+ } catch (error) { caught = error; }
+ if (!caught || caught.name !== 'InvalidAccessError' || caught.code !== 15 ||
+ Object.getPrototypeOf(caught) !== callee.DOMException.prototype || xhr.readyState !== 0) {
+ throw new Error('synchronous open errors must retain the method realm and unopened state');
+ }
+ }
+ }
+ return 'ok';
+})()
+"#).expect("live XHR exception realm probe should run");
+ assert_eq!(result, "ok");
+}
diff --git a/moli-renderer-v8/src/script_vm/tests/extracted/child_documents.rs b/moli-renderer-v8/src/script_vm/tests/extracted/child_documents.rs
index 9a62fb919..2f82de481 100644
--- a/moli-renderer-v8/src/script_vm/tests/extracted/child_documents.rs
+++ b/moli-renderer-v8/src/script_vm/tests/extracted/child_documents.rs
@@ -209,7 +209,7 @@ async fn child_navigation_retires_local_window_owned_xhr() {
.expect("child XHR realm should exist");
vm.eval_in_child_default_context(
child_context_id,
- "parent.__retiredChildXhrWrapper = new XMLHttpRequest(); 'captured'",
+ "parent.__retiredChildXhrWrapper = new XMLHttpRequest(); parent.__retiredChildXhrExceptionPrototype = DOMException.prototype; 'captured'",
)
.expect("parent should retain the child-created XHR wrapper for stale-owner proof");
let child_context_ptr = {
@@ -292,13 +292,15 @@ async fn child_navigation_retires_local_window_owned_xhr() {
);
return "no-error";
} catch (error) {
- return [error.name, error.code, error instanceof DOMException].join("|");
+ return [error.name, error.code,
+ Object.getPrototypeOf(error) === __retiredChildXhrExceptionPrototype,
+ !(error instanceof DOMException)].join("|");
}
})()
"#,
)
.expect("calling open on a retained old-child XHR wrapper should fail closed");
- assert_eq!(stale_open, "InvalidStateError|11|true");
+ assert_eq!(stale_open, "InvalidStateError|11|true|true");
assert!(
vm._context_host
.borrow()