Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(document): reject XML child document stream operations

ldm007231c6baa8c
diff --git a/moli-core/tests/scripts.rs b/moli-core/tests/scripts.rs
index ae0f63834..73032057c 100644
--- a/moli-core/tests/scripts.rs
+++ b/moli-core/tests/scripts.rs
@@ -6711,3 +6711,6 @@ async fn parse_time_lifecycle_queue_can_stop_cleanly_at_load_stage_after_load_mi
#[path = "scripts/document_write_errors.rs"]
mod document_write_errors;
+
+#[path = "scripts/child_markup_insertion.rs"]
+mod child_markup_insertion;
diff --git a/moli-core/tests/scripts/child_markup_insertion.rs b/moli-core/tests/scripts/child_markup_insertion.rs
new file mode 100644
index 000000000..75511afe7
--- /dev/null
+++ b/moli-core/tests/scripts/child_markup_insertion.rs
@@ -0,0 +1,247 @@
+use super::*;
+
+async fn evaluate_child_xml_probe(body: &str) -> Result<serde_json::Value> {
+ let server = FixtureServer::spawn().await?;
+ let browser = Browser::new(AppConfig::default())?;
+ let mut page = browser.fetch(&server.url("/static")).await?;
+ let expression = r#"(async () => {
+ const frames = [];
+ async function loadFrame(type, markup) {
+ markup ??= type === 'image/svg+xml'
+ ? '<svg xmlns="http://www.w3.org/2000/svg"><g id="kept"/></svg>'
+ : '<html xmlns="http://www.w3.org/1999/xhtml"><head/><body><p id="kept">original</p></body></html>';
+ const url = '/compat/child-dynamic-markup-document?type=' + encodeURIComponent(type) +
+ '&markup=' + encodeURIComponent(markup);
+ return new Promise(resolve => {
+ const frame = document.createElement('iframe');
+ frames.push(frame);
+ frame.onload = () => resolve(frame);
+ frame.src = url;
+ document.body.append(frame);
+ });
+ }
+ try {
+ return JSON.stringify(await (async () => { __PROBE_BODY__ })());
+ } finally {
+ for (const frame of frames) frame.remove();
+ }
+ })()"#
+ .replace("__PROBE_BODY__", body);
+ let result = page
+ .evaluate_runtime_expression_with_await_async(&expression, true)
+ .await?;
+ server.shutdown().await;
+ serde_json::from_str(result["value"].as_str().expect("child XML markup probe"))
+ .map_err(Into::into)
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn child_xml_document_stream_methods_throw_without_side_effects() -> Result<()> {
+ let result = evaluate_child_xml_probe(
+ r#"const results = [];
+ const operations = [['open'], ['open', 'text/html'], ['open', 'text/html', 'replace'],
+ ['write'], ['write', '<p>replacement</p>'], ['writeln'],
+ ['writeln', '<p>replacement</p>'], ['close']];
+ for (const type of ['text/xml', 'application/xml', 'application/xhtml+xml', 'image/svg+xml']) {
+ for (const [method, ...args] of operations) {
+ const frame = await loadFrame(type);
+ const doc = frame.contentDocument, win = frame.contentWindow;
+ const root = doc.documentElement, html = root.outerHTML, url = doc.URL;
+ const contentType = doc.contentType, readyState = doc.readyState;
+ const events = [];
+ doc.addEventListener('probe', () => events.push('document'));
+ win.addEventListener('probe', () => events.push('window'));
+ root.addEventListener('probe', () => events.push('root'));
+ const observer = new MutationObserver(() => {});
+ observer.observe(doc, {childList: true, subtree: true});
+ Object.defineProperty(doc, 'contentType', {get() { throw new Error('must use native document type'); }});
+ let error = null;
+ try { doc[method](...args); }
+ catch (e) { error = {name: e.name, code: e.code, relevantRealm: e instanceof win.DOMException}; }
+ doc.dispatchEvent(new Event('probe'));
+ win.dispatchEvent(new Event('probe'));
+ root.dispatchEvent(new Event('probe'));
+ results.push({type, method, argc: args.length, contentType, error, events,
+ sameDocument: frame.contentDocument === doc,
+ sameRoot: doc.documentElement === root, sameMarkup: doc.documentElement?.outerHTML === html,
+ sameURL: doc.URL === url, sameReadiness: doc.readyState === readyState,
+ mutations: observer.takeRecords().length});
+ observer.disconnect();
+ }
+ }
+ return results;"#,
+ )
+ .await?;
+ let rows = result.as_array().expect("XML stream method matrix");
+ assert_eq!(rows.len(), 32);
+ for row in rows {
+ assert_eq!(row["contentType"], row["type"], "{row}");
+ assert_eq!(
+ row["error"],
+ serde_json::json!({"name": "InvalidStateError", "code": 11, "relevantRealm": true}),
+ "{row}"
+ );
+ assert_eq!(
+ row["events"],
+ serde_json::json!(["document", "window", "root"]),
+ "{row}"
+ );
+ for field in [
+ "sameDocument",
+ "sameRoot",
+ "sameMarkup",
+ "sameURL",
+ "sameReadiness",
+ ] {
+ assert_eq!(row[field], true, "{field}: {row}");
+ }
+ assert_eq!(row["mutations"], 0, "{row}");
+ }
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn child_xml_document_write_converts_arguments_before_rejecting_xml() -> Result<()> {
+ let result = evaluate_child_xml_probe(
+ r#"const results = [];
+ for (const method of ['write', 'writeln']) {
+ const frame = await loadFrame('application/xhtml+xml');
+ const doc = frame.contentDocument, root = doc.documentElement;
+ const conversions = [];
+ const sentinel = {};
+ let conversionError = false;
+ try {
+ doc[method]({toString() { conversions.push('throw'); throw sentinel; }});
+ } catch (e) { conversionError = e === sentinel; }
+ let error = null;
+ try {
+ doc[method]({toString() { conversions.push('first'); return '<p>'; }},
+ {toString() { conversions.push('second'); return '</p>'; }});
+ } catch (e) { error = {name: e.name, relevantRealm: e instanceof frame.contentWindow.DOMException}; }
+ results.push({method, conversionError, conversions, error,
+ sameRoot: doc.documentElement === root});
+ }
+ return results;"#,
+ )
+ .await?;
+ assert_eq!(
+ result,
+ serde_json::json!(["write", "writeln"].map(|method| serde_json::json!({
+ "method": method, "conversionError": true,
+ "conversions": ["throw", "first", "second"], "sameRoot": true,
+ "error": {"name": "InvalidStateError", "relevantRealm": true}
+ })))
+ );
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn child_xml_document_stream_methods_reject_during_parser_script() -> Result<()> {
+ let result = evaluate_child_xml_probe(
+ r#"const frame = await loadFrame('application/xhtml+xml',
+ '<html xmlns="http://www.w3.org/1999/xhtml"><head/><body><p id="before">before</p>' +
+ '<script>' +
+ 'window.streamErrors = []; window.readyStates = [];' +
+ 'document.addEventListener("readystatechange", () => readyStates.push(document.readyState));' +
+ 'for (const method of ["open", "write", "writeln", "close"]) {' +
+ ' const before = document.readyState;' +
+ ' let error = null; try { document[method](); }' +
+ ' catch (e) { error = {name: e.name, relevantRealm: e instanceof DOMException}; }' +
+ ' streamErrors.push({method, error, sameReadiness: document.readyState === before});' +
+ '}' +
+ '<' + '/script><p id="tail">tail</p></body></html>');
+ return {errors: frame.contentWindow.streamErrors, states: frame.contentWindow.readyStates,
+ before: !!frame.contentDocument.getElementById('before'),
+ tail: !!frame.contentDocument.getElementById('tail')};"#,
+ )
+ .await?;
+ assert_eq!(
+ result["errors"],
+ serde_json::json!(
+ ["open", "write", "writeln", "close"].map(|method| serde_json::json!({
+ "method": method, "error": {"name": "InvalidStateError", "relevantRealm": true},
+ "sameReadiness": true
+ }))
+ ),
+ "{result}"
+ );
+ assert_eq!(
+ result["states"],
+ serde_json::json!(["interactive", "complete"])
+ );
+ assert_eq!(result["before"], true);
+ assert_eq!(result["tail"], true);
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn child_xml_document_stream_methods_reject_during_unload() -> Result<()> {
+ let result = evaluate_child_xml_probe(
+ r#"const frame = await loadFrame('application/xhtml+xml');
+ const doc = frame.contentDocument, win = frame.contentWindow;
+ const root = doc.documentElement, errors = [];
+ for (const event of ['beforeunload', 'pagehide', 'unload']) {
+ win.addEventListener(event, () => {
+ for (const method of ['open', 'write', 'writeln', 'close']) {
+ let error = null;
+ try { doc[method](); }
+ catch (e) { error = {name: e.name, relevantRealm: e instanceof win.DOMException}; }
+ errors.push({event, method, error, sameRoot: doc.documentElement === root});
+ }
+ });
+ }
+ await new Promise(resolve => { frame.onload = resolve; frame.src = '/static'; });
+ return errors;"#,
+ )
+ .await?;
+ let rows = result.as_array().expect("XML unload markup calls");
+ let expected = ["beforeunload", "pagehide", "unload"]
+ .into_iter()
+ .flat_map(|event| {
+ ["open", "write", "writeln", "close"].map(|method| {
+ serde_json::json!({
+ "event": event, "method": method, "sameRoot": true,
+ "error": {"name": "InvalidStateError", "relevantRealm": true}
+ })
+ })
+ })
+ .collect::<Vec<_>>();
+ assert_eq!(rows, &expected);
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn child_document_write_converts_arguments_before_parser_constructor_guard() -> Result<()> {
+ let result = evaluate_child_xml_probe(
+ r#"const frame = await loadFrame('text/html');
+ const doc = frame.contentDocument, win = frame.contentWindow, results = [];
+ class MarkupProbe extends win.HTMLElement {
+ constructor() {
+ super();
+ for (const method of ['write', 'writeln']) {
+ const conversions = [], sentinel = {};
+ let conversionError = false, error = null;
+ try { doc[method]({toString() { conversions.push('throw'); throw sentinel; }}); }
+ catch (e) { conversionError = e === sentinel; }
+ try { doc[method]({toString() { conversions.push('string'); return '<b>blocked</b>'; }}); }
+ catch (e) { error = {name: e.name, relevantRealm: e instanceof win.DOMException}; }
+ results.push({method, conversions, conversionError, error});
+ }
+ }
+ }
+ win.customElements.define('markup-probe', MarkupProbe);
+ doc.open();
+ doc.write('<!doctype html><body><markup-probe></markup-probe>');
+ doc.close();
+ return results;"#,
+ )
+ .await?;
+ assert_eq!(
+ result,
+ serde_json::json!(["write", "writeln"].map(|method| serde_json::json!({
+ "method": method, "conversions": ["throw", "string"], "conversionError": true,
+ "error": {"name": "InvalidStateError", "relevantRealm": true}
+ })))
+ );
+ Ok(())
+}
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs
index 044fe9c24..33b1f1e88 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs
@@ -5,8 +5,10 @@ use crate::document_script_scheduler::FrameDocumentClassicScriptSchedulerWork;
use crate::dom::native::Node;
use crate::dom_parser::DOM_PARSER_FOREIGN_NODE_SLOT;
use crate::native_bridge::{
- OwnerDispatchScope, document::detached_native_handle_for_runtime,
- node::remove_child_to_current_reaction_queue, throw_dom_exception,
+ OwnerDispatchScope,
+ document::{detached_native_handle_for_runtime, is_html_document},
+ node::remove_child_to_current_reaction_queue,
+ throw_dom_exception,
};
use crate::util::{context_host_ptr_from_global_bridge, set_private_value, v8str};
use moli_webapi_declare::WebApiObject;
@@ -302,7 +304,7 @@ fn child_document_open_callback<'s>(
redirect_child_document_open_to_window_open(scope, handle, document, &args, &mut rv);
return;
}
- if child_document_has_throw_on_dynamic_markup_insertion_counter(scope, document) {
+ if child_document_has_invalid_dynamic_markup_state(scope, document) {
throw_dynamic_markup_invalid_state(scope);
return;
}
@@ -342,10 +344,6 @@ fn child_document_write_or_writeln_callback<'s>(
return;
};
let document = args.this();
- if child_document_has_throw_on_dynamic_markup_insertion_counter(scope, document) {
- throw_dynamic_markup_invalid_state(scope);
- return;
- }
let mut chunk = String::new();
for index in 0..args.length() {
let Some(value) = args.get(index).to_string(scope) else {
@@ -356,6 +354,11 @@ fn child_document_write_or_writeln_callback<'s>(
if append_newline {
chunk.push('\n');
}
+ // Argument conversion precedes both the XML and parser-constructor guards.
+ if child_document_has_invalid_dynamic_markup_state(scope, document) {
+ throw_dynamic_markup_invalid_state(scope);
+ return;
+ }
let host = unsafe { &mut *host_ptr };
if host.child_document_stream_is_blocked_by_navigation(handle) {
rv.set_undefined();
@@ -417,7 +420,7 @@ fn child_document_close_callback<'s>(
return;
};
let document = args.this();
- if child_document_has_throw_on_dynamic_markup_insertion_counter(scope, document) {
+ if child_document_has_invalid_dynamic_markup_state(scope, document) {
throw_dynamic_markup_invalid_state(scope);
return;
}
@@ -510,15 +513,17 @@ fn child_document_default_view<'s>(
.and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
}
-fn child_document_has_throw_on_dynamic_markup_insertion_counter<'s>(
+fn child_document_has_invalid_dynamic_markup_state<'s>(
scope: &mut v8::PinScope<'s, '_>,
document: v8::Local<'s, v8::Object>,
) -> bool {
context_host_ptr_from_global_bridge(scope)
.and_then(|host_ptr| {
let host = unsafe { &*host_ptr };
- child_document_native_handle_for_runtime(scope, host_ptr, document)
- .map(|document| host.has_throw_on_dynamic_markup_insertion_counter(document))
+ child_document_native_handle_for_runtime(scope, host_ptr, document).map(|document| {
+ !is_html_document(host, document)
+ || host.has_throw_on_dynamic_markup_insertion_counter(document)
+ })
})
.unwrap_or(false)
}