Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(cssom-view): zero detached window screen geometry

ldm03e56061ed658
diff --git a/moli-renderer-v8/src/context_bootstrap/navigator_runtime/screen.rs b/moli-renderer-v8/src/context_bootstrap/navigator_runtime/screen.rs
index 173b74cdc..819c6921a 100644
--- a/moli-renderer-v8/src/context_bootstrap/navigator_runtime/screen.rs
+++ b/moli-renderer-v8/src/context_bootstrap/navigator_runtime/screen.rs
@@ -4,8 +4,10 @@ use super::super::media_queries::{
simple_event_target_remove_event_listener_callback,
};
use super::super::*;
-use crate::util::{callback_data_index_value, callback_data_item};
-use crate::util::{get_private_value, set_private_value, throw_type_error};
+use crate::util::{
+ callback_data_index_value, callback_data_item, context_host_ptr_from_global_bridge,
+ get_private_value, set_private_value, throw_type_error,
+};
use crate::webidl;
use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};
@@ -351,6 +353,21 @@ pub(in crate::context_bootstrap) fn build_window_screen<'s>(
Ok(screen)
}
+fn screen_owner_realm_is_current(
+ scope: &mut v8::PinScope<'_, '_>,
+ screen: v8::Local<'_, v8::Object>,
+) -> bool {
+ let Some(context) = screen.get_creation_context(scope) else {
+ return false;
+ };
+ let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) else {
+ return false;
+ };
+ let host = unsafe { &*host_ptr };
+ host.window_execution_context_identity_for_access_check(context)
+ .is_some_and(|identity| host.window_execution_context_identity_is_current(identity))
+}
+
fn screen_attribute_getter_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
@@ -375,6 +392,19 @@ fn screen_attribute_getter_callback<'s>(
}
return;
}
+ if matches!(
+ slot,
+ SCREEN_WIDTH_SLOT
+ | SCREEN_HEIGHT_SLOT
+ | SCREEN_AVAIL_WIDTH_SLOT
+ | SCREEN_AVAIL_HEIGHT_SLOT
+ | SCREEN_AVAIL_LEFT_SLOT
+ | SCREEN_AVAIL_TOP_SLOT
+ ) && !screen_owner_realm_is_current(scope, args.this())
+ {
+ rv.set(v8::Number::new(scope, 0.0).into());
+ return;
+ }
rv.set(
get_private_value(scope, args.this(), slot).unwrap_or_else(|| v8::undefined(scope).into()),
);
diff --git a/moli-renderer-v8/src/script_vm/tests/lazy_window_surfaces.rs b/moli-renderer-v8/src/script_vm/tests/lazy_window_surfaces.rs
index f6249d8e5..f410aba83 100644
--- a/moli-renderer-v8/src/script_vm/tests/lazy_window_surfaces.rs
+++ b/moli-renderer-v8/src/script_vm/tests/lazy_window_surfaces.rs
@@ -719,6 +719,51 @@ fn navigator_scalar_access_does_not_materialize_unused_same_object_children() {
);
}
+#[test]
+fn detached_child_screen_keeps_identity_and_zeros_geometry() {
+ let mut vm = new_storage_test_vm("https://detached-child-screen.test/");
+ vm.eval(
+ r#"
+ (() => {
+ const root = document.documentElement ||
+ document.appendChild(document.createElement("html"));
+ const body = document.body || root.appendChild(document.createElement("body"));
+ const frame = document.createElement("iframe");
+ frame.id = "detached-screen-frame";
+ body.appendChild(frame);
+ globalThis.__heldChildWindow = frame.contentWindow;
+ globalThis.__heldChildScreen = __heldChildWindow.screen;
+ })()
+ "#,
+ )
+ .expect("child Screen should materialize before frame removal");
+ materialize_single_child_default_realm_for_test(&mut vm, "detached child Screen");
+
+ assert_eq!(
+ vm.eval(
+ r#"
+ (() => {
+ document.getElementById("detached-screen-frame").remove();
+ const value = __heldChildWindow.screen;
+ return JSON.stringify({
+ same: value === __heldChildScreen,
+ width: value.width,
+ height: value.height,
+ availWidth: value.availWidth,
+ availHeight: value.availHeight,
+ availLeft: value.availLeft,
+ availTop: value.availTop,
+ colorDepth: value.colorDepth,
+ pixelDepth: value.pixelDepth
+ });
+ })()
+ "#,
+ )
+ .expect("retained child Screen should remain readable after frame removal"),
+ r#"{"same":true,"width":0,"height":0,"availWidth":0,"availHeight":0,"availLeft":0,"availTop":0,"colorDepth":24,"pixelDepth":24}"#
+ );
+}
+
#[test]
fn borrowed_navigator_getters_materialize_subobjects_in_the_receiver_realm() {
let mut vm = new_storage_test_vm("https://lazy-navigator-borrowed-getter.test/");