Skip to content

Truly independent web browser

LibWeb: Stop remote WindowProxies from keeping dead documents alive

Andreas Kling5b3911b89423
diff --git a/Libraries/LibWeb/HTML/RemoteNavigable.cpp b/Libraries/LibWeb/HTML/RemoteNavigable.cpp
index 3f5ca2c7efb..b8ccb40af04 100644
--- a/Libraries/LibWeb/HTML/RemoteNavigable.cpp
+++ b/Libraries/LibWeb/HTML/RemoteNavigable.cpp
@@ -98,21 +98,38 @@ void RemoteNavigable::replace_child(Navigable& child, GC::Ref<Navigable> replace
GC::Ptr<WindowProxy> RemoteNavigable::active_window_proxy()
{
- // The WindowProxy of a navigable hosted by another process answers every access on the cross-origin path. It lives
- // in the realm of the script asking for it.
+ // The WindowProxy of a navigable hosted by another process answers every access on the cross-origin path.
return active_window_proxy_in_realm_of(current_window());
}
-// Outside script, the caller gives the window it hands the WindowProxy to.
+// The WindowProxy lives in the realm of window_for_window_proxy(), or else of the window the caller gives, which is the
+// one it hands the WindowProxy to.
GC::Ref<WindowProxy> RemoteNavigable::active_window_proxy_in_realm_of(Window& window)
{
if (!m_window_proxy) {
- m_window_proxy = WindowProxy::create(relevant_realm(window));
+ auto window_for_proxy = window_for_window_proxy();
+ m_window_proxy = WindowProxy::create(relevant_realm(window_for_proxy ? *window_for_proxy : window));
m_window_proxy->set_window(active_window());
}
return *m_window_proxy;
}
+// The WindowProxy's shape keeps the realm it lives in alive, and with it the document of that realm's window. So it lives
+// in the realm of the window that embeds the navigable here, or its nearest ancestor embedded here, which outlives the
+// navigable. A navigable that nothing here embeds has its opener's window here, if any.
+GC::Ptr<Window> RemoteNavigable::window_for_window_proxy()
+{
+ for (GC::Ptr<Navigable> navigable = this; navigable; navigable = navigable->parent()) {
+ if (auto container = navigable->container())
+ return container->document().window();
+ }
+ if (auto* opener = as_if<LocalNavigable>(active_browsing_context_opener_navigable().ptr())) {
+ if (auto document = opener->active_document())
+ return document->window();
+ }
+ return nullptr;
+}
+
GC::Ptr<WindowProxy> RemoteNavigable::active_browsing_context_opener_window_proxy() const
{
if (auto opener_navigable = active_browsing_context_opener_navigable())
diff --git a/Libraries/LibWeb/HTML/RemoteNavigable.h b/Libraries/LibWeb/HTML/RemoteNavigable.h
index 691dd337ad4..972cff14ec7 100644
--- a/Libraries/LibWeb/HTML/RemoteNavigable.h
+++ b/Libraries/LibWeb/HTML/RemoteNavigable.h
@@ -56,6 +56,7 @@ public:
virtual GC::Ptr<WindowProxy> active_window_proxy() override;
GC::Ref<WindowProxy> active_window_proxy_in_realm_of(Window&);
+ GC::Ptr<Window> window_for_window_proxy();
GC::Ref<RemoteWindow> active_window();
virtual Utf16String const& target_name() const override { return m_replicated_state.target_name; }
virtual Optional<u64> browsing_context_group_id() const override { return m_replicated_state.browsing_context_group_id; }
diff --git a/Libraries/LibWeb/HTML/WindowProxy.cpp b/Libraries/LibWeb/HTML/WindowProxy.cpp
index cadfe8d2a66..635aae733b2 100644
--- a/Libraries/LibWeb/HTML/WindowProxy.cpp
+++ b/Libraries/LibWeb/HTML/WindowProxy.cpp
@@ -398,6 +398,16 @@ void WindowProxy::set_window(GC::Ref<RemoteWindow> window)
{
m_window = nullptr;
set_remote_window_over_provisional_window(window);
+
+ if (!realm().host_defined() || !Bindings::host_defined_wrapper_world(realm()).is_main_world())
+ return;
+
+ // The proxy's shape keeps its realm alive. With no Window of this process to follow, it moves to the realm its
+ // navigable picks, instead of pinning the realm of the Window it followed, whose document is gone.
+ auto navigable = window->navigable();
+ auto window_for_proxy = navigable ? navigable->window_for_window_proxy() : nullptr;
+ if (window_for_proxy && &window_for_proxy->principal_realm() != &realm())
+ m_object->set_realm_and_prototype(window_for_proxy->principal_realm(), nullptr);
}
void WindowProxy::set_remote_window_over_provisional_window(GC::Ref<RemoteWindow> window)
diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/window-proxy-of-iframe-leaving-for-another-process-leaves-its-realm.txt b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/window-proxy-of-iframe-leaving-for-another-process-leaves-its-realm.txt
index a275871cc4d..ac37b811d7f 100644
--- a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/window-proxy-of-iframe-leaving-for-another-process-leaves-its-realm.txt
+++ b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/window-proxy-of-iframe-leaving-for-another-process-leaves-its-realm.txt
@@ -1,3 +1,3 @@
in the realm of a destroyed document here: false
document here destroyed: true
-in the realm of a destroyed document when remote: true
+in the realm of a destroyed document when remote: false
diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/window-proxy-reached-by-same-site-grandchild-leaves-its-realm.txt b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/window-proxy-reached-by-same-site-grandchild-leaves-its-realm.txt
index cae95f427ac..00f8a87ef87 100644
--- a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/window-proxy-reached-by-same-site-grandchild-leaves-its-realm.txt
+++ b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/window-proxy-reached-by-same-site-grandchild-leaves-its-realm.txt
@@ -2,4 +2,4 @@ WebContent#0
iframe#0: remote WebContent#1
iframe#0: remote WebContent#0
first grandchild's document destroyed: true
-in the realm of a destroyed document: true
+in the realm of a destroyed document: false
diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/window-proxy-of-popup-leaving-for-another-process-leaves-its-realm.txt b/Tests/LibWeb/Text/expected/SiteIsolation/window-proxy-of-popup-leaving-for-another-process-leaves-its-realm.txt
index 4b1a9958b30..41449add6d4 100644
--- a/Tests/LibWeb/Text/expected/SiteIsolation/window-proxy-of-popup-leaving-for-another-process-leaves-its-realm.txt
+++ b/Tests/LibWeb/Text/expected/SiteIsolation/window-proxy-of-popup-leaving-for-another-process-leaves-its-realm.txt
@@ -2,4 +2,4 @@ message: same origin
in the realm of a destroyed document here: false
message: cross-site
document here destroyed: true
-in the realm of a destroyed document when remote: true
+in the realm of a destroyed document when remote: false