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(dom): require owning hosts for native bridge identity
ldm0e87d0390174f
diff --git a/moli-renderer-v8/src/native_bridge/bridge/wrappers.rs b/moli-renderer-v8/src/native_bridge/bridge/wrappers.rsindex 3500ed64c..669ff4271 100644--- a/moli-renderer-v8/src/native_bridge/bridge/wrappers.rs+++ b/moli-renderer-v8/src/native_bridge/bridge/wrappers.rs@@ -49,9 +49,9 @@ impl NativeDomBridge {host_ptr: *mut JsContextHost,handle: BridgeHandle,) -> Option<v8::Local<'s, v8::Object>> {- if crate::util::context_host_ptr_from_context_slot(scope.get_current_context())- .is_some_and(|current| current != host_ptr)- {+ let current_host =+ crate::util::context_host_ptr_from_context_slot(scope.get_current_context())?;+ if current_host != host_ptr {// Reflector IDs are local to a DOM bridge. Related Pages share// an isolate, but must never consult one another's wrapper cache.let host = unsafe { &*host_ptr };diff --git a/moli-renderer-v8/src/native_bridge/helpers/identity.rs b/moli-renderer-v8/src/native_bridge/helpers/identity.rsindex cdbb01ac5..9290b0ba8 100644--- a/moli-renderer-v8/src/native_bridge/helpers/identity.rs+++ b/moli-renderer-v8/src/native_bridge/helpers/identity.rs@@ -1,5 +1,5 @@use super::super::{BridgeHandle, JsContextHost, ReflectorId};-use crate::util::{context_host_ptr_from_context_slot, context_host_ptr_from_global_bridge};+use crate::util::context_host_ptr_from_context_slot;use anyhow::{Context, bail, ensure};fn object_reflector_id(@@ -34,11 +34,11 @@ pub(in crate::native_bridge) fn bridge_handle_from_object() -> anyhow::Result<(*mut JsContextHost, BridgeHandle)> {// Related Pages share an isolate, but reflector IDs belong to the// wrapper's original DOM bridge rather than the calling Window's bridge.- let runtime_ptr = object+ let context = object.get_creation_context(scope)- .and_then(context_host_ptr_from_context_slot)- .or_else(|| context_host_ptr_from_global_bridge(scope))- .context("current context has no JsContextHost")?;+ .context("native wrapper has no creation context")?;+ let runtime_ptr = context_host_ptr_from_context_slot(context)+ .context("native wrapper's creation context has no JsContextHost")?;let reflector_id = object_reflector_id(scope, object)?;let handle = unsafe { &*runtime_ptr }.native_bridge()diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rsindex 199007bf7..181cc334e 100644--- a/moli-renderer-v8/src/script_vm/tests/mod.rs+++ b/moli-renderer-v8/src/script_vm/tests/mod.rs@@ -2169,6 +2169,7 @@ mod lazy_storage;mod lazy_window_surfaces;mod midi_owner_interfaces;mod mouse_snapshot;+mod native_bridge_identity;mod native_exception_stack_capture;mod no_cors_header_fill;mod observer_callbacks;diff --git a/moli-renderer-v8/src/script_vm/tests/native_bridge_identity.rs b/moli-renderer-v8/src/script_vm/tests/native_bridge_identity.rsnew file mode 100644index 000000000..afb99cf0e--- /dev/null+++ b/moli-renderer-v8/src/script_vm/tests/native_bridge_identity.rs@@ -0,0 +1,75 @@+use super::*;+use crate::native_bridge::node_runtime_and_handle_from_object;++#[test]+fn native_node_identity_does_not_resolve_an_unowned_reflector_in_the_callers_bridge() {+ let mut vm = new_storage_test_vm("https://native-bridge-owner.test/");+ vm.eval("globalThis.nativeNode = document.createElement('div')")+ .unwrap();+ vm.with_default_context_scope_and_checkpoint_for_test(|scope, host_ptr| {+ let global = scope.get_current_context().global(scope);+ let key = v8::String::new(scope, "nativeNode").unwrap();+ let value = global.get(scope, key.into()).unwrap();+ let native = v8::Local::<v8::Object>::try_from(value).unwrap();+ let (owner, handle) = node_runtime_and_handle_from_object(scope, native).unwrap();+ assert_eq!(owner, host_ptr);+ let reflector = native.get_internal_field(scope, 0).unwrap();++ // This V8-only fixture has a real, colliding reflector ID but no host.+ // Looking up that ID in the caller's bridge would return nativeNode.+ let context = v8::Context::new(scope, Default::default());+ let unowned = {+ let scope = &mut v8::ContextScope::new(scope, context);+ let template = v8::ObjectTemplate::new(scope);+ template.set_internal_field_count(1);+ let object = template.new_instance(scope).unwrap();+ assert!(object.set_internal_field(0, reflector));+ v8::Global::new(scope, object)+ };+ let unowned = v8::Local::new(scope, &unowned);+ assert!(+ node_runtime_and_handle_from_object(scope, unowned).is_err(),+ "a reflector without its owning host must not borrow the caller's identity"+ );+ assert_eq!(+ node_runtime_and_handle_from_object(scope, native).unwrap(),+ (owner, handle),+ "the genuine wrapper still resolves to its native identity"+ );+ Ok(())+ })+ .unwrap();+}++#[test]+fn native_node_wrapper_materialization_requires_a_host_for_the_current_context() {+ let mut vm = new_storage_test_vm("https://native-bridge-context.test/");+ vm.eval("globalThis.nativeNode = document.createElement('div')")+ .unwrap();+ vm.with_default_context_scope_and_checkpoint_for_test(|scope, host_ptr| {+ let global = scope.get_current_context().global(scope);+ let key = v8::String::new(scope, "nativeNode").unwrap();+ let value = global.get(scope, key.into()).unwrap();+ let native = v8::Local::<v8::Object>::try_from(value).unwrap();+ let (_, handle) = node_runtime_and_handle_from_object(scope, native).unwrap();++ let context = v8::Context::new(scope, Default::default());+ {+ let scope = &mut v8::ContextScope::new(scope, context);+ assert!(+ unsafe { &mut *host_ptr }+ .native_bridge_mut()+ .wrap_handle(scope, host_ptr, handle)+ .is_none(),+ "an unowned context must not receive a cached or newly created native wrapper"+ );+ }+ let wrapper = unsafe { &mut *host_ptr }+ .native_bridge_mut()+ .wrap_handle(scope, host_ptr, handle)+ .unwrap();+ assert_eq!(wrapper, native, "the owning context keeps wrapper identity");+ Ok(())+ })+ .unwrap();+}