Skip to content

Truly independent web browser

LibWeb: Wrap a frame’s Navigation and NavigateEvents in its own realm

sideshowbarker710dcfbd6c29
diff --git a/Libraries/LibWeb/HTML/NavigateEvent.cpp b/Libraries/LibWeb/HTML/NavigateEvent.cpp
index d00190e18f8..db476dd0dbe 100644
--- a/Libraries/LibWeb/HTML/NavigateEvent.cpp
+++ b/Libraries/LibWeb/HTML/NavigateEvent.cpp
@@ -137,6 +137,11 @@ void NavigateEvent::visit_edges(GC::Cell::Visitor& visitor)
visitor.visit(m_relevant_window);
}
+GC::Ptr<Bindings::Wrappable> NavigateEvent::relevant_global_impl() const
+{
+ return m_relevant_window;
+}
+
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigateevent-intercept
WebIDL::ExceptionOr<void> NavigateEvent::intercept(JS::Realm& realm, NavigationInterceptOptions const& options)
{
diff --git a/Libraries/LibWeb/HTML/NavigateEvent.h b/Libraries/LibWeb/HTML/NavigateEvent.h
index 3b2e389cd0b..0d4fdc57fed 100644
--- a/Libraries/LibWeb/HTML/NavigateEvent.h
+++ b/Libraries/LibWeb/HTML/NavigateEvent.h
@@ -112,6 +112,7 @@ public:
virtual ~NavigateEvent() override;
Window& relevant_window() const { return *m_relevant_window; }
+ virtual GC::Ptr<Bindings::Wrappable> relevant_global_impl() const override;
GC::Ref<DOM::AbortController> abort_controller() const { return *m_abort_controller; }
InterceptionState interception_state() const { return m_interception_state; }
diff --git a/Libraries/LibWeb/HTML/Navigation.cpp b/Libraries/LibWeb/HTML/Navigation.cpp
index d201a275795..f59b68e4e64 100644
--- a/Libraries/LibWeb/HTML/Navigation.cpp
+++ b/Libraries/LibWeb/HTML/Navigation.cpp
@@ -108,6 +108,11 @@ Window& Navigation::window() const
return m_window;
}
+GC::Ptr<Bindings::Wrappable> Navigation::relevant_global_impl() const
+{
+ return m_window;
+}
+
void Navigation::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
diff --git a/Libraries/LibWeb/HTML/Navigation.h b/Libraries/LibWeb/HTML/Navigation.h
index 673e2189224..62f5d17b6b0 100644
--- a/Libraries/LibWeb/HTML/Navigation.h
+++ b/Libraries/LibWeb/HTML/Navigation.h
@@ -167,6 +167,7 @@ private:
explicit Navigation(Window&);
Window& window() const;
+ virtual GC::Ptr<Bindings::Wrappable> relevant_global_impl() const override;
virtual void visit_edges(Visitor&) override;
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index c2b1dead378..c456c0dd4cc 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -303,6 +303,7 @@ Text/input/navigation/reload-aborts-nested-navigate-events.html
Text/input/navigation/fragment-navigation-event-order.html
Text/input/navigation/intercept-handler-rejection-aborts-navigate-event.html
Text/input/navigation/pushstate-in-abort-handler-of-superseded-navigate-event.html
+Text/input/navigation/navigate-event-keeps-its-realm-after-navigation-wrapper-is-collected.html
Text/input/navigation/navigation-activation-push-replace-traverse.html
Text/input/navigation/navigation-activation-in-sandboxed-frame.html
diff --git a/Tests/LibWeb/Text/expected/navigation/navigate-event-keeps-its-realm-after-navigation-wrapper-is-collected.txt b/Tests/LibWeb/Text/expected/navigation/navigate-event-keeps-its-realm-after-navigation-wrapper-is-collected.txt
new file mode 100644
index 00000000000..2aa35fa0da0
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/navigation/navigate-event-keeps-its-realm-after-navigation-wrapper-is-collected.txt
@@ -0,0 +1,2 @@
+navigation object is the frame's: true
+navigate event is the frame's: true
diff --git a/Tests/LibWeb/Text/input/navigation/navigate-event-keeps-its-realm-after-navigation-wrapper-is-collected.html b/Tests/LibWeb/Text/input/navigation/navigate-event-keeps-its-realm-after-navigation-wrapper-is-collected.html
new file mode 100644
index 00000000000..a1a8be8f045
--- /dev/null
+++ b/Tests/LibWeb/Text/input/navigation/navigate-event-keeps-its-realm-after-navigation-wrapper-is-collected.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<iframe id="frame" src="../../data/iframe-test-content-1.html"></iframe>
+<script>
+ // A frame's navigation object and its navigate events belong to the frame's realm, even when a handler from this
+ // window gets them after the navigation object's wrapper has been collected.
+ asyncTest(async done => {
+ const frame = document.getElementById("frame");
+ await new Promise(resolve => window.addEventListener("load", () => setTimeout(resolve, 0)));
+
+ await (() => frame.contentWindow.navigation.navigate("#1").finished)();
+
+ let resolveNavigate;
+ const navigated = new Promise(resolve => (resolveNavigate = resolve));
+ (() => {
+ frame.contentWindow.navigation.onnavigate = function (event) {
+ resolveNavigate({
+ navigationInFrameRealm: this instanceof frame.contentWindow.Navigation,
+ eventInFrameRealm: event instanceof frame.contentWindow.NavigateEvent,
+ });
+ };
+ })();
+
+ // Nothing references the navigation object's wrapper any more, so this collects it.
+ await internals.gcAsync();
+
+ frame.contentWindow.history.back();
+ const result = await navigated;
+ println(`navigation object is the frame's: ${result.navigationInFrameRealm}`);
+ println(`navigate event is the frame's: ${result.eventInFrameRealm}`);
+ done();
+ });
+</script>