From 611df91c5db256bbbe849f6a85151c3bc28b3694 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Mon, 31 Aug 2026 08:45:24 +0800 Subject: [PATCH] fix(webidl): honor NewTarget realm prototype fallback --- moli-renderer-v8/src/dom_parser.rs | 5 +- .../script_vm/tests/dom_xhr/dom/dom_parser.rs | 101 ++++++++++++++++++ moli-renderer-v8/src/util.rs | 1 + 3 files changed, 105 insertions(+), 2 deletions(-) diff --git a/moli-renderer-v8/src/dom_parser.rs b/moli-renderer-v8/src/dom_parser.rs index 3f5f43ad281..181aaa47c30 100644 --- a/moli-renderer-v8/src/dom_parser.rs +++ b/moli-renderer-v8/src/dom_parser.rs @@ -19,8 +19,8 @@ use super::{ }, }, util::{ - context_host_ptr_from_global_bridge, get_private_object, get_private_value, - set_private_value, throw_type_error, + apply_webidl_constructor_prototype_fallback, context_host_ptr_from_global_bridge, + get_private_object, get_private_value, set_private_value, throw_type_error, }, }; @@ -102,6 +102,7 @@ pub(super) fn dom_parser_constructor_callback<'s>( } let parser = args.this(); moli_webapi::dom_parser_values::initialize_dom_parser_object(scope, parser); + apply_webidl_constructor_prototype_fallback(scope, parser, args.new_target(), "DOMParser"); let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) else { throw_type_error(scope, "DOMParser constructor has no associated Document"); return; diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/dom_parser.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/dom_parser.rs index 84f27a23520..8cc1d2f5cc0 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/dom_parser.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/dom_parser.rs @@ -161,6 +161,107 @@ fn dom_parser_uses_its_constructor_realm_associated_document() { ); } +#[test] +fn dom_parser_non_object_new_target_prototype_uses_new_target_realm_default() { + let mut vm = new_storage_test_vm("https://dom-parser-new-target.test/top.html"); + + let result = vm + .eval( + r#" +(() => { + const frame = document.createElement('iframe'); + (document.body || document.documentElement || document).appendChild(frame); + const child = frame.contentWindow; + child.history.replaceState(null, '', '/child.html'); + + const TopBad = new Function(); + TopBad.prototype = 7; + const ChildBad = new child.Function(); + ChildBad.prototype = 7; + + const BoundChild = Function.prototype.bind.call(new child.Function()); + BoundChild.prototype = 7; + const BoundTop = child.Function.prototype.bind.call(new Function()); + BoundTop.prototype = 7; + + const ProxyChild = new Proxy(new child.Function(), {}); + ProxyChild.prototype = 7; + const ProxyTop = new child.Proxy(new Function(), {}); + ProxyTop.prototype = 7; + + let getterCount = 0; + const GetterProxyChild = new Proxy(new child.Function(), { + get(target, property, receiver) { + if (property === 'prototype') { + getterCount += 1; + return 7; + } + return Reflect.get(target, property, receiver); + } + }); + + const parseUrl = parser => + DOMParser.prototype.parseFromString.call( + parser, + '', + 'text/html' + ).URL; + const check = (parser, expectedPrototype, expectedUrl) => [ + Object.getPrototypeOf(parser) === expectedPrototype, + parseUrl(parser) === expectedUrl + ]; + const topUrl = location.href; + const childUrl = child.location.href; + + return JSON.stringify({ + directTop: check( + Reflect.construct(child.DOMParser, [], TopBad), + DOMParser.prototype, + childUrl + ), + directChild: check( + Reflect.construct(DOMParser, [], ChildBad), + child.DOMParser.prototype, + topUrl + ), + boundChild: check( + Reflect.construct(DOMParser, [], BoundChild), + child.DOMParser.prototype, + topUrl + ), + boundTop: check( + Reflect.construct(child.DOMParser, [], BoundTop), + DOMParser.prototype, + childUrl + ), + proxyChild: check( + Reflect.construct(DOMParser, [], ProxyChild), + child.DOMParser.prototype, + topUrl + ), + proxyTop: check( + Reflect.construct(child.DOMParser, [], ProxyTop), + DOMParser.prototype, + childUrl + ), + getterProxyChild: check( + Reflect.construct(DOMParser, [], GetterProxyChild), + child.DOMParser.prototype, + topUrl + ), + getterCount + }); +})() +"#, + ) + .expect("DOMParser NewTarget realm prototype fallback probe should evaluate"); + + assert_eq!( + result, + r#"{"directTop":[true,true],"directChild":[true,true],"boundChild":[true,true],"boundTop":[true,true],"proxyChild":[true,true],"proxyTop":[true,true],"getterProxyChild":[true,true],"getterCount":1}"# + ); +} + #[tokio::test(flavor = "current_thread")] async fn dom_parser_retained_across_child_navigation_uses_original_document() { const HOST: &str = "dom-parser-retained.test"; diff --git a/moli-renderer-v8/src/util.rs b/moli-renderer-v8/src/util.rs index 5e1e4cad589..d91ecfc0b11 100644 --- a/moli-renderer-v8/src/util.rs +++ b/moli-renderer-v8/src/util.rs @@ -1,5 +1,6 @@ use super::document_runtime::DomHandle; use super::native_bridge::PageScriptHost; +pub(crate) use moli_v8_util::apply_webidl_constructor_prototype_fallback; pub(crate) use moli_v8_util::v8_string_from_utf16_units; pub use moli_v8_util::{ array_contains_strict, array_push_value, call_object_method, callback_data_index_value,