Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(custom-elements): guard reentrant definitions

ldm062b16009672b
diff --git a/moli-benchmark/wpt-cross-current/failed-cases.txt b/moli-benchmark/wpt-cross-current/failed-cases.txt
index 4e2a02b22..7d9f7ff62 100644
--- a/moli-benchmark/wpt-cross-current/failed-cases.txt
+++ b/moli-benchmark/wpt-cross-current/failed-cases.txt
@@ -2556,7 +2556,6 @@ css/selectors/parsing/parse-part.html
css/selectors/parsing/parse-slotted.html
css/selectors/selectors-4/lang-singleton-subtag-matching.html
css/selectors/webkit-pseudo-element.html
-custom-elements/CustomElementRegistry.html
custom-elements/parser/parser-uses-registry-of-owner-document.html
custom-elements/perform-microtask-checkpoint-before-construction.html
custom-elements/reactions/Document.html
diff --git a/moli-benchmark/wpt-cross-current/passed-cases.txt b/moli-benchmark/wpt-cross-current/passed-cases.txt
index 449b3b377..b1616ed53 100644
--- a/moli-benchmark/wpt-cross-current/passed-cases.txt
+++ b/moli-benchmark/wpt-cross-current/passed-cases.txt
@@ -3262,6 +3262,7 @@ css/selectors/selectors-case-sensitive-001.html
css/selectors/x-pseudo-element.html
custom-elements/CustomElementRegistry-constructor-and-callbacks-are-held-strongly.html
custom-elements/CustomElementRegistry-getName.html
+custom-elements/CustomElementRegistry.html
custom-elements/Document-createElement-customized-builtins.html
custom-elements/Document-createElement.html
custom-elements/Document-createElementNS-customized-builtins.html
diff --git a/moli-renderer-v8/src/context_bootstrap/constructors/custom_elements_registry.rs b/moli-renderer-v8/src/context_bootstrap/constructors/custom_elements_registry.rs
index c7ed85379..8c6f3c36a 100644
--- a/moli-renderer-v8/src/context_bootstrap/constructors/custom_elements_registry.rs
+++ b/moli-renderer-v8/src/context_bootstrap/constructors/custom_elements_registry.rs
@@ -156,10 +156,14 @@ pub(in crate::context_bootstrap) fn custom_elements_define_callback<'s>(
let extends_local_name = parsed.options.extends;
let is_autonomous_definition = extends_local_name.is_none();
let registry_key = custom_elements::registry_store_key(scope, args.this());
- let disables_shadow = match unsafe { &mut *host_ptr }
- .custom_elements_mut_for_registry_key(registry_key)
- .define(scope, &parsed.name, parsed.constructor, extends_local_name)
- {
+ let disables_shadow = match custom_elements::define_custom_element_for_registry(
+ scope,
+ host_ptr,
+ registry_key,
+ &parsed.name,
+ parsed.constructor,
+ extends_local_name,
+ ) {
Ok(disables_shadow) => disables_shadow,
Err(err) => {
if err.is_pending_exception() {
diff --git a/moli-renderer-v8/src/custom_elements.rs b/moli-renderer-v8/src/custom_elements.rs
index be09afd4f..8dc9627dd 100644
--- a/moli-renderer-v8/src/custom_elements.rs
+++ b/moli-renderer-v8/src/custom_elements.rs
@@ -19,6 +19,7 @@ mod definition_error;
mod definition_extends;
mod definition_lookup;
mod definition_registry;
+pub(crate) use definition_registry::define_custom_element_for_registry;
mod definition_sequence;
mod definition_state;
mod definition_upgrade;
diff --git a/moli-renderer-v8/src/custom_elements/definition_registry.rs b/moli-renderer-v8/src/custom_elements/definition_registry.rs
index 29d95a630..b273d10ff 100644
--- a/moli-renderer-v8/src/custom_elements/definition_registry.rs
+++ b/moli-renderer-v8/src/custom_elements/definition_registry.rs
@@ -1,4 +1,5 @@
use super::super::util::v8_string;
+use super::CustomElementRegistryKey;
use super::definition::{CustomElementStore, PendingWhenDefined};
use super::definition_builder::{
build_custom_element_definition, constructor_source_is_non_constructable,
@@ -6,31 +7,35 @@ use super::definition_builder::{
};
use super::definition_error::CustomElementDefineError;
use crate::dom::custom_elements::is_valid_custom_element_name;
+use crate::native_bridge::JsContextHost;
-impl CustomElementStore {
- pub(crate) fn define<'s>(
- &mut self,
- scope: &mut v8::PinScope<'s, '_>,
- name: &str,
- constructor: v8::Local<'s, v8::Function>,
- extends_local_name: Option<String>,
- ) -> Result<bool, CustomElementDefineError> {
- let constructor_value: v8::Local<'_, v8::Value> = constructor.into();
- if constructor_value.is_async_function() || constructor_value.is_generator_function() {
- return Err(CustomElementDefineError::ConstructorNotConstructable);
- }
- if constructor_source_is_non_constructable(scope, constructor_value)? {
- return Err(CustomElementDefineError::ConstructorNotConstructable);
- }
- if !is_valid_custom_element_name(name) {
- return Err(CustomElementDefineError::InvalidName(name.to_owned()));
- }
- if self.definitions.contains_key(name) {
+pub(crate) fn define_custom_element_for_registry<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ host_ptr: *mut JsContextHost,
+ registry_key: CustomElementRegistryKey,
+ name: &str,
+ constructor: v8::Local<'s, v8::Function>,
+ extends_local_name: Option<String>,
+) -> Result<bool, CustomElementDefineError> {
+ let constructor_value: v8::Local<'_, v8::Value> = constructor.into();
+ if constructor_value.is_async_function() || constructor_value.is_generator_function() {
+ return Err(CustomElementDefineError::ConstructorNotConstructable);
+ }
+ if constructor_source_is_non_constructable(scope, constructor_value)? {
+ return Err(CustomElementDefineError::ConstructorNotConstructable);
+ }
+ if !is_valid_custom_element_name(name) {
+ return Err(CustomElementDefineError::InvalidName(name.to_owned()));
+ }
+
+ {
+ let store = unsafe { &mut *host_ptr }.custom_elements_mut_for_registry_key(registry_key);
+ if store.definitions.contains_key(name) {
return Err(CustomElementDefineError::NameAlreadyDefined(
name.to_owned(),
));
}
- if self.definitions.values().any(|definition| {
+ if store.definitions.values().any(|definition| {
let registered_constructor = v8::Local::new(scope, &definition.constructor);
registered_constructor.strict_equals(constructor.into())
}) {
@@ -43,21 +48,34 @@ impl CustomElementStore {
extends_local_name.to_owned(),
));
}
- if self.definition_is_running {
+ if store.definition_is_running {
return Err(CustomElementDefineError::DefinitionAlreadyRunning);
}
+ store.definition_is_running = true;
+ }
+
+ // Constructor property access can synchronously call define() again. Do
+ // not retain a Rust reference to the registry store across that re-entry.
+ let definition_result = build_custom_element_definition(scope, constructor, extends_local_name);
+ unsafe { &mut *host_ptr }
+ .custom_elements_mut_for_registry_key(registry_key)
+ .definition_is_running = false;
- self.definition_is_running = true;
- let definition_result =
- build_custom_element_definition(scope, constructor, extends_local_name);
- self.definition_is_running = false;
- let definition = definition_result?;
- let disables_shadow = definition.disables_shadow;
- self.definitions.insert(name.to_owned(), definition);
- self.resolve_when_defined(scope, name);
- Ok(disables_shadow)
+ let definition = definition_result?;
+ let disables_shadow = definition.disables_shadow;
+ let pending = {
+ let store = unsafe { &mut *host_ptr }.custom_elements_mut_for_registry_key(registry_key);
+ store.definitions.insert(name.to_owned(), definition);
+ store.pending_when_defined.remove(name)
+ };
+ if let Some(pending) = pending {
+ let resolver = v8::Local::new(scope, &pending.resolver);
+ let _ = resolver.resolve(scope, constructor.into());
}
+ Ok(disables_shadow)
+}
+impl CustomElementStore {
pub(crate) fn definition_constructor<'s>(
&self,
scope: &mut v8::PinScope<'s, '_>,
@@ -134,15 +152,4 @@ impl CustomElementStore {
);
Some(promise)
}
-
- fn resolve_when_defined(&mut self, scope: &mut v8::PinScope<'_, '_>, name: &str) {
- let Some(pending) = self.pending_when_defined.remove(name) else {
- return;
- };
- let Some(constructor) = self.definition_constructor(scope, name) else {
- return;
- };
- let resolver = v8::Local::new(scope, &pending.resolver);
- let _ = resolver.resolve(scope, constructor.into());
- }
}
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements.rs
index 7ca09cc33..3b38303c2 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/custom_elements.rs
@@ -267,6 +267,60 @@ fn custom_elements_define_validates_definition_inputs_in_spec_order() {
);
}
+#[test]
+fn custom_elements_define_reentrant_proxy_stops_before_constructor_properties() {
+ let mut vm = new_storage_test_vm("https://example.com/");
+
+ let result = vm
+ .eval(
+ r#"
+ (() => {
+ const calls = [];
+ const InnerElement = new Proxy(class extends HTMLElement {}, {
+ get(target, property, receiver) {
+ calls.push(`inner:${String(property)}`);
+ return Reflect.get(target, property, receiver);
+ }
+ });
+ const OuterElement = new Proxy(class extends HTMLElement {}, {
+ get(target, property, receiver) {
+ calls.push(`outer:${String(property)}`);
+ if (property === "prototype") {
+ customElements.define("wpt-reentrant-inner", InnerElement);
+ }
+ return Reflect.get(target, property, receiver);
+ }
+ });
+
+ let errorName = "no-throw";
+ try {
+ customElements.define("wpt-reentrant-outer", OuterElement);
+ } catch (error) {
+ errorName = error.name;
+ }
+
+ class DefinitionAfterFailure extends HTMLElement {}
+ customElements.define("wpt-after-reentrant-failure", DefinitionAfterFailure);
+
+ return JSON.stringify({
+ errorName,
+ calls,
+ innerRegistered: customElements.get("wpt-reentrant-inner") !== undefined,
+ outerRegistered: customElements.get("wpt-reentrant-outer") !== undefined,
+ flagCleared:
+ customElements.get("wpt-after-reentrant-failure") === DefinitionAfterFailure
+ });
+ })()
+ "#,
+ )
+ .expect("reentrant customElements.define proxy probe should evaluate");
+
+ assert_eq!(
+ result,
+ r#"{"errorName":"NotSupportedError","calls":["outer:prototype"],"innerRegistered":false,"outerRegistered":false,"flagCleared":true}"#
+ );
+}
+
#[test]
fn parent_node_replace_children_flushes_custom_element_reactions_after_operation() {
let mut vm = new_storage_test_vm("https://example.com/");