Skip to content

Truly independent web browser

LibWeb: Move a WindowProxy to the realm of its Window

Jelle Raaijmakers8387afa8e315
diff --git a/Libraries/LibWeb/Bindings/Wrappable.cpp b/Libraries/LibWeb/Bindings/Wrappable.cpp
index e50f5867189..8b8b00f6e9f 100644
--- a/Libraries/LibWeb/Bindings/Wrappable.cpp
+++ b/Libraries/LibWeb/Bindings/Wrappable.cpp
@@ -217,19 +217,6 @@ JS::Realm& this_value_realm(JS::Realm& fallback_realm, JS::Value this_value)
auto& proxy_realm = object.shape().realm();
if (!proxy_realm.host_defined())
return fallback_realm;
-
- // A main-world WindowProxy is created once, in the initial about:blank
- // realm, then reused as globalThis across later navigations. Do not use
- // that stale shape realm for receiver-realm decisions: HTML defines a
- // WindowProxy's relevant realm as the realm of its current [[Window]].
- //
- // Isolated-world WindowProxy objects are created in their own world
- // realm; keep those on the proxy's realm so cross-world wrapping stays
- // isolated instead of leaking through the principal Window realm.
- if (host_defined_wrapper_world(proxy_realm).is_main_world()) {
- if (auto window = window_proxy->window())
- return window->principal_realm();
- }
}
return object.shape().realm();
diff --git a/Libraries/LibWeb/HTML/WindowProxy.cpp b/Libraries/LibWeb/HTML/WindowProxy.cpp
index 52096374d61..20064ec5279 100644
--- a/Libraries/LibWeb/HTML/WindowProxy.cpp
+++ b/Libraries/LibWeb/HTML/WindowProxy.cpp
@@ -386,8 +386,11 @@ void WindowProxy::set_window(GC::Ref<Window> window)
// still exposes Window's public prototype, while ordinary property caches
// can guard and load through this hidden link. Retargeting the proxy after
// navigation changes its shape and invalidates those caches naturally.
- auto& window_wrapper = Bindings::platform_object_for_window(*m_window, realm());
- m_object->set_prototype(&window_wrapper);
+
+ // The proxy's shape keeps its realm alive, so it follows the Window instead of pinning the realm it was created in.
+ auto& window_realm = m_window->principal_realm();
+ auto& window_wrapper = Bindings::platform_object_for_window(*m_window, window_realm);
+ m_object->set_realm_and_prototype(window_realm, &window_wrapper);
}
void WindowProxy::set_window(GC::Ref<RemoteWindow> window)
diff --git a/Tests/LibWeb/TestWindowProxyWorld.cpp b/Tests/LibWeb/TestWindowProxyWorld.cpp
index ce878115734..1313e1aef1e 100644
--- a/Tests/LibWeb/TestWindowProxyWorld.cpp
+++ b/Tests/LibWeb/TestWindowProxyWorld.cpp
@@ -89,6 +89,7 @@ TEST_CASE(per_world_windowproxy_and_window_wrapper)
auto& main_wrapper = Web::Bindings::platform_object_for_window(*window, window->principal_realm());
auto main_proxy = browsing_context->window_proxy();
EXPECT(main_proxy->object().shape().prototype() == &main_wrapper);
+ EXPECT(&main_proxy->object().shape().realm() == &window->principal_realm());
EXPECT(Web::HTML::WindowProxy::from_object(main_proxy->object()) == main_proxy);
EXPECT(!Web::HTML::WindowProxy::from_object(main_wrapper));
@@ -191,10 +192,8 @@ TEST_CASE(per_world_windowproxy_and_window_wrapper)
EXPECT(popped_extension_execution_context == extension_execution_context.ptr());
auto replacement_window = Web::HTML::Window::create();
- browsing_context->set_active_window(replacement_window);
- EXPECT(main_proxy->window().ptr() == replacement_window.ptr());
+ extension_proxy->set_window(replacement_window);
EXPECT(extension_proxy->window().ptr() == replacement_window.ptr());
- EXPECT(main_proxy->object().shape().prototype() == &Web::Bindings::platform_object_for_window(*replacement_window, main_proxy->realm()));
EXPECT(extension_proxy->object().shape().prototype() == nullptr);
vm.pop_execution_context();