Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(webidl): validate Document attribute receivers
ldm05727dc7dff61
diff --git a/moli-renderer-v8/src/native_bridge/document.rs b/moli-renderer-v8/src/native_bridge/document.rsindex 42dd23d95..60f36fdfb 100644--- a/moli-renderer-v8/src/native_bridge/document.rs+++ b/moli-renderer-v8/src/native_bridge/document.rs@@ -302,7 +302,7 @@ pub(crate) const XHTML_NS: &str = "http://www.w3.org/1999/xhtml";pub(crate) const SVG_NS: &str = "http://www.w3.org/2000/svg";#[derive(WebApiFunctionTemplate)]-#[webapi(interface = web_api_interfaces::Document, enumerable)]+#[webapi(interface = web_api_interfaces::Document, enumerable, receiver)]struct DocumentMetadataPrototypeDeclaration {#[webapi(accessor_property = "URL", getter = document_url_getter_function)]url: (),@@ -325,7 +325,7 @@ struct DocumentMetadataPrototypeDeclaration {getter = document_last_modified_getter_function)]last_modified: (),- #[webapi(accessor_property, getter = document_referrer_getter_function, receiver = web_api_interfaces::Document::is_instance)]+ #[webapi(accessor_property, getter = document_referrer_getter_function)]referrer: (),}@@ -356,7 +356,7 @@ struct DocumentStructurePrototypeDeclaration {}#[derive(WebApiFunctionTemplate)]-#[webapi(interface = web_api_interfaces::Document, enumerable)]+#[webapi(interface = web_api_interfaces::Document, enumerable, receiver)]struct DocumentViewPrototypeDeclaration {#[webapi(accessor_property = "defaultView",@@ -366,7 +366,7 @@ struct DocumentViewPrototypeDeclaration {}#[derive(WebApiFunctionTemplate)]-#[webapi(interface = web_api_interfaces::Document, enumerable)]+#[webapi(interface = web_api_interfaces::Document, enumerable, receiver)]struct DocumentFocusPrototypeDeclaration {#[webapi(accessor_property = "activeElement",@@ -376,7 +376,7 @@ struct DocumentFocusPrototypeDeclaration {}#[derive(WebApiFunctionTemplate)]-#[webapi(interface = web_api_interfaces::Document, enumerable)]+#[webapi(interface = web_api_interfaces::Document, enumerable, receiver)]struct DocumentStatePrototypeDeclaration {#[webapi(accessor_property,diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rsindex bf3a70d53..ad892814c 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs@@ -83,6 +83,37 @@ fn cross_realm_dom_bindings_reject_incompatible_receivers_in_their_own_realm() {);}+#[test]+fn document_metadata_view_focus_and_state_validate_native_receivers() {+ let mut vm = new_storage_test_vm("https://document-receivers.test/");+ vm.eval(+ r#"+const root = document.documentElement || document.appendChild(document.createElement('html'));+const frame = document.createElement('iframe');+frame.id = 'document-receiver-child';+root.appendChild(frame);+"#,+ )+ .expect("receiver test child should be created");+ materialize_single_child_default_realm_for_test(&mut vm, "Document receiver child realm");++ let fixture = include_str!("../../../../tests/fixtures/document-receivers.js");+ vm.eval(&format!(+ "{fixture}\ndocumentReceiverProbe().then(value => {{ globalThis.documentReceiverResult = value; }}, error => {{ globalThis.documentReceiverResult = {{error: String(error.stack || error)}}; }});"+ )).expect("Document receiver probe should start");+ let result: serde_json::Value =+ serde_json::from_str(&vm.eval("JSON.stringify(documentReceiverResult)").unwrap()).unwrap();+ let checks = result["checks"]+ .as_array()+ .unwrap_or_else(|| panic!("{result}"));+ let failures: Vec<_> = checks+ .iter()+ .filter(|check| check["pass"] != true)+ .collect();+ assert_eq!(result["state"], "pass", "{failures:?}");+ assert_eq!(checks.len(), 111);+}+#[test]fn document_compat_mode_reflects_parser_quirks_mode() {let cases = [diff --git a/moli-renderer-v8/tests/fixtures/document-receivers.js b/moli-renderer-v8/tests/fixtures/document-receivers.jsnew file mode 100644index 000000000..2e82f4c3a--- /dev/null+++ b/moli-renderer-v8/tests/fixtures/document-receivers.js@@ -0,0 +1,97 @@+async function documentReceiverProbe() {+ const checks = [];+ const check = (label, actual, wanted) => checks.push({+ label, actual, wanted, pass: JSON.stringify(actual) === JSON.stringify(wanted)+ });+ let frame = document.getElementById('document-receiver-child');+ if (!frame) {+ frame = document.createElement('iframe');+ frame.id = 'document-receiver-child';+ (document.body || document.documentElement).appendChild(frame);+ }+ try {+ const child = frame.contentWindow;+ const names = [+ 'URL', 'documentURI', 'readyState', 'contentType', 'characterSet', 'charset',+ 'inputEncoding', 'compatMode', 'lastModified', 'referrer', 'defaultView',+ 'activeElement', 'implementation', 'fonts', 'currentScript', 'hidden',+ 'visibilityState', 'prerendering', 'domain', 'scrollingElement'+ ];+ let traps = 0;+ const revoked = Proxy.revocable(document, {});+ revoked.revoke();+ const element = document.createElement('div');+ const invalid = [+ undefined, null, 1, 'document', false, Symbol('document'), 1n, {},+ Document.prototype, HTMLDocument.prototype, Object.create(document),+ Object.create(child.Document.prototype), element,+ document.createTextNode('text'), document.createDocumentFragment(),+ new Proxy(document, {+ get() { traps++; throw new Error('get trap'); },+ getPrototypeOf() { traps++; throw new Error('prototype trap'); }+ }),+ revoked.proxy, new Proxy(child.document, {})+ ];+ const changed = document.implementation.createHTMLDocument('changed prototype');+ const genuine = [+ document, child.document, new Document(), new child.Document(),+ document.implementation.createHTMLDocument('windowless'),+ child.document.implementation.createDocument(null, 'root'),+ new DOMParser().parseFromString('<root/>', 'application/xml'),+ new (class extends Document {})(), changed+ ];+ Object.setPrototypeOf(changed, null);+ for (const [realmName, realm] of [['parent', window], ['child', child]]) {+ const typeError = callback => {+ try { callback(); return 'returned'; }+ catch (error) {+ return Object.getPrototypeOf(error) === realm.TypeError.prototype+ ? 'callee TypeError' : error.name;+ }+ };+ for (const name of names) {+ const descriptor = Object.getOwnPropertyDescriptor(realm.Document.prototype, name);+ check(`${realmName}/${name}/invalid`, invalid.map(value =>+ typeError(() => descriptor.get.call(value))), invalid.map(() => 'callee TypeError'));+ check(`${realmName}/${name}/genuine`, genuine.map(value => {+ try { return descriptor.get.call(value) !== undefined; }+ catch (error) { return error.name; }+ }), genuine.map(() => true));+ }+ const domain = Object.getOwnPropertyDescriptor(realm.Document.prototype, 'domain');+ let conversions = 0;+ const sentinel = {};+ const value = {toString() { conversions++; throw sentinel; }};+ check(`${realmName}/domain/invalid-set`, invalid.map(receiver =>+ typeError(() => domain.set.call(receiver, value))), invalid.map(() => 'callee TypeError'));+ check(`${realmName}/domain/no-conversion`, conversions, 0);+ for (const [index, receiver] of genuine.entries()) {+ let caught;+ try { domain.set.call(receiver, value); } catch (error) { caught = error; }+ check(`${realmName}/domain/genuine-conversion-${index}`, caught === sentinel, true);+ }+ check(`${realmName}/domain/conversion-count`, conversions, genuine.length);+ for (const [name, descriptor] of [+ ['onmouseenter', Object.getOwnPropertyDescriptor(realm.HTMLElement.prototype, 'onmouseenter')],+ ['onmouseleave', Object.getOwnPropertyDescriptor(realm.HTMLElement.prototype, 'onmouseleave')]+ ]) {+ const incompatible = invalid.filter(receiver => receiver !== element);+ check(`${realmName}/${name}/lenient`, incompatible.map(receiver => {+ try {+ return descriptor.get.call(receiver) === undefined &&+ descriptor.set.call(receiver, value) === undefined;+ } catch (error) { return error.name; }+ }), incompatible.map(() => true));+ }+ }+ check('author-proxy-traps', traps, 0);+ const parentView = Object.getOwnPropertyDescriptor(Document.prototype, 'defaultView').get;+ const childView = Object.getOwnPropertyDescriptor(child.Document.prototype, 'defaultView').get;+ check('borrowed-defaultView', [parentView.call(child.document) === child,+ childView.call(document) === window, parentView.call(changed) === null], [true, true, true]);+ const hidden = Object.getOwnPropertyDescriptor(child.Document.prototype, 'hidden').get;+ const visibility = Object.getOwnPropertyDescriptor(Document.prototype, 'visibilityState').get;+ check('windowless-visibility', [hidden.call(changed), visibility.call(changed)], [true, 'hidden']);+ } finally { frame.remove(); }+ return {state: checks.every(check => check.pass) ? 'pass' : 'fail', checks};+}