Skip to content

Truly independent web browser

LibWeb: Don’t run callbacks from documents that aren’t fully active

sideshowbarker5524f5a6e26f
diff --git a/Libraries/LibWeb/DOM/EventTarget.cpp b/Libraries/LibWeb/DOM/EventTarget.cpp
index 2304e128e2e..feb6f5787cc 100644
--- a/Libraries/LibWeb/DOM/EventTarget.cpp
+++ b/Libraries/LibWeb/DOM/EventTarget.cpp
@@ -180,7 +180,8 @@ JS::Completion invoke_event_listener(WebIDL::CallbackType& callback, DOM::Event&
auto wrapped_event = Bindings::event(event_realm, GC::Ref { event });
static Utf16FlyString const& handle_event = *new Utf16FlyString("handleEvent"_utf16_fly_string);
- return WebIDL::call_user_object_operation(callback, handle_event, this_value.ptr(), { { wrapped_event } });
+ // Inner invoke has just skipped the listener if its callback's Window has a document that isn't fully active.
+ return WebIDL::call_user_object_operation(callback, handle_event, this_value.ptr(), { { wrapped_event } }, WebIDL::RefuseCallbackFromInactiveWindow::No);
}
JS::Completion invoke_event_handler(WebIDL::CallbackType& callback, DOM::Event& event, bool special_error_event_handling)
diff --git a/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cpp b/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cpp
index 391fd098154..1fe15b99931 100644
--- a/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cpp
@@ -93,7 +93,7 @@ void WindowEnvironmentSettingsObject::setup(Page& page, URL::URL const& creation
GC::Ptr<DOM::Document> WindowEnvironmentSettingsObject::responsible_document()
{
// Return window's associated Document.
- return m_window->associated_document();
+ return m_window->associated_document_if_any();
}
// https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:api-base-url
diff --git a/Libraries/LibWeb/WebIDL/AbstractOperations.cpp b/Libraries/LibWeb/WebIDL/AbstractOperations.cpp
index 6d0967711a0..657f5d6a962 100644
--- a/Libraries/LibWeb/WebIDL/AbstractOperations.cpp
+++ b/Libraries/LibWeb/WebIDL/AbstractOperations.cpp
@@ -17,6 +17,7 @@
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibJS/Runtime/ValueInlines.h>
+#include <LibWeb/DOM/Document.h>
#include <LibWeb/Export.h>
#include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
#include <LibWeb/HTML/Scripting/Environments.h>
@@ -163,8 +164,26 @@ inline JS::Completion clean_up_on_return(HTML::EnvironmentSettingsObject& stored
return JS::Value { rejected_promise->promise() };
}
+// AD-HOC: None of invoking a callback function, constructing one, or calling a user object's operation checks that the
+// callback's realm can still run script, so a callback from a page that's been navigated away from (or an
+// iframe that's been removed) would keep running wherever a live object holds it — a timer on the new page's
+// Window, e.g., which lets the old page's code re-arm itself there and keep the whole old page alive. So refuse
+// to call into a realm whose global object is a Window whose associated Document isn't fully active.
+// See https://github.com/whatwg/webidl/issues/993.
+static bool is_settings_object_of_inactive_window(HTML::EnvironmentSettingsObject& relevant_settings)
+{
+ // Only a Window's settings object has a responsible document: the Window's associated Document.
+ auto document = relevant_settings.responsible_document();
+ return document && !document->is_fully_active();
+}
+
+static JS::Completion refuse_callback_from_inactive_window(JS::Realm& realm)
+{
+ return throw_completion(realm, NotSupportedError::create(realm, "Cannot run a callback from a window whose document is not fully active"_utf16));
+}
+
// https://webidl.spec.whatwg.org/#call-a-user-objects-operation
-JS::Completion call_user_object_operation(CallbackType& callback, Utf16FlyString const& operation_name, Optional<JS::Value> this_argument, ReadonlySpan<JS::Value> args)
+JS::Completion call_user_object_operation(CallbackType& callback, Utf16FlyString const& operation_name, Optional<JS::Value> this_argument, ReadonlySpan<JS::Value> args, RefuseCallbackFromInactiveWindow refuse_from_inactive_window)
{
// 1. Let completion be an uninitialized variable.
JS::Completion completion;
@@ -192,6 +211,10 @@ JS::Completion call_user_object_operation(CallbackType& callback, Utf16FlyString
return throw_completion(caller_realm, WebIDL::SecurityError::create(caller_realm, "Cannot invoke a callback on a cross-origin object"_utf16));
}
+ // AD-HOC: See is_settings_object_of_inactive_window().
+ if (refuse_from_inactive_window == RefuseCallbackFromInactiveWindow::Yes && is_settings_object_of_inactive_window(relevant_settings))
+ return refuse_callback_from_inactive_window(realm);
+
// 7. Prepare to run script with relevant settings.
HTML::prepare_to_run_script(relevant_settings);
@@ -304,6 +327,10 @@ static auto invoke_callback_impl(CallbackType& callback, Optional<JS::Value> thi
// 7. Let stored settings be callable’s callback context.
auto& stored_settings = callback.callback_context;
+ // AD-HOC: See is_settings_object_of_inactive_window().
+ if (is_settings_object_of_inactive_window(relevant_settings))
+ return return_steps(realm, refuse_callback_from_inactive_window(realm));
+
// 8. Prepare to run script with relevant realm.
HTML::prepare_to_run_script(relevant_settings);
@@ -428,6 +455,10 @@ JS::Completion construct(CallbackType& callable, ReadonlySpan<JS::Value> args)
// 6. Let stored settings be callable’s callback context.
auto& stored_settings = callable.callback_context;
+ // AD-HOC: See is_settings_object_of_inactive_window().
+ if (is_settings_object_of_inactive_window(relevant_settings))
+ return refuse_callback_from_inactive_window(realm);
+
// 7. Prepare to run script with relevant settings.
HTML::prepare_to_run_script(relevant_settings);
diff --git a/Libraries/LibWeb/WebIDL/AbstractOperations.h b/Libraries/LibWeb/WebIDL/AbstractOperations.h
index b5bedadfddc..c0d26c3e011 100644
--- a/Libraries/LibWeb/WebIDL/AbstractOperations.h
+++ b/Libraries/LibWeb/WebIDL/AbstractOperations.h
@@ -28,7 +28,13 @@ WEB_API ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_sour
WEB_API ErrorOr<ByteBuffer> get_buffer_source_copy(BufferSource const&);
WEB_API ErrorOr<ByteBuffer> get_buffer_source_copy(ArrayBufferView const&);
-JS::Completion call_user_object_operation(CallbackType& callback, Utf16FlyString const& operation_name, Optional<JS::Value> this_argument, ReadonlySpan<JS::Value> args);
+// AD-HOC: Whether to refuse a callback whose realm's global object is a Window whose associated Document isn't fully
+// active. A caller that has just skipped such a callback itself passes No.
+enum class RefuseCallbackFromInactiveWindow {
+ Yes,
+ No,
+};
+JS::Completion call_user_object_operation(CallbackType& callback, Utf16FlyString const& operation_name, Optional<JS::Value> this_argument, ReadonlySpan<JS::Value> args, RefuseCallbackFromInactiveWindow = RefuseCallbackFromInactiveWindow::Yes);
WEB_API JS::ThrowCompletionOr<Utf16String> to_utf16_string(JS::VM&, JS::Value);
JS::ThrowCompletionOr<Utf16String> to_utf16_usv_string(JS::VM&, JS::Value);
diff --git a/Tests/LibWeb/Text/expected/HTML/callbacks-from-inactive-documents-do-not-run.txt b/Tests/LibWeb/Text/expected/HTML/callbacks-from-inactive-documents-do-not-run.txt
new file mode 100644
index 00000000000..ecc2eb7b78f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/callbacks-from-inactive-documents-do-not-run.txt
@@ -0,0 +1,3 @@
+Callback from a removed iframe: did not run
+Callback from a navigated-away document: did not run
+Constructor from a removed iframe: did not run
diff --git a/Tests/LibWeb/Text/input/HTML/callbacks-from-inactive-documents-do-not-run.html b/Tests/LibWeb/Text/input/HTML/callbacks-from-inactive-documents-do-not-run.html
new file mode 100644
index 00000000000..c5282faff6f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/callbacks-from-inactive-documents-do-not-run.html
@@ -0,0 +1,56 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ function addFrame(srcdoc) {
+ const frame = document.createElement("iframe");
+ frame.srcdoc = srcdoc;
+ return new Promise(resolve => {
+ frame.addEventListener("load", () => resolve(frame), { once: true });
+ document.body.append(frame);
+ });
+ }
+
+ function navigateFrame(frame, srcdoc) {
+ return new Promise(resolve => {
+ frame.addEventListener("load", resolve, { once: true });
+ frame.srcdoc = srcdoc;
+ });
+ }
+
+ // Resolves once a timer set on the given window after this call has run, so every timer set on it earlier, with
+ // the same timeout, has had its turn.
+ function afterPendingTimers(win) {
+ return new Promise(resolve => win.setTimeout(resolve, 0));
+ }
+
+ asyncTest(async done => {
+ window.ran = [];
+
+ // A live window's timer, holding a callback from an iframe that's since been removed.
+ const removed = await addFrame("<script>const ran = parent.ran; parent.removedCallback = () => ran.push('removed');<\/script>");
+ const { removedCallback } = window;
+ removed.remove();
+ setTimeout(removedCallback, 0);
+ await afterPendingTimers(window);
+ println(`Callback from a removed iframe: ${ran.includes("removed") ? "ran" : "did not run"}`);
+
+ // A navigated-away document's code, scheduling through its WindowProxy: The timer lands on the new document's
+ // Window, which is fully active, but the callback is still the old document's.
+ const navigated = await addFrame("<script>const ran = parent.ran; parent.rearm = () => window.setTimeout(() => ran.push('navigated'), 0);<\/script>");
+ const { rearm } = window;
+ await navigateFrame(navigated, "<p>New document</p>");
+ rearm();
+ await afterPendingTimers(navigated.contentWindow);
+ println(`Callback from a navigated-away document: ${ran.includes("navigated") ? "ran" : "did not run"}`);
+
+ // A custom element constructor from an iframe that's since been removed, constructing an element here.
+ const constructorFrame = await addFrame("<script>const ran = parent.ran; parent.RemovedElement = class extends HTMLElement { constructor() { ran.push('constructed'); super(); } };<\/script>");
+ const { RemovedElement } = window;
+ constructorFrame.remove();
+ customElements.define("removed-iframe-element", RemovedElement);
+ document.createElement("removed-iframe-element");
+ println(`Constructor from a removed iframe: ${ran.includes("constructed") ? "ran" : "did not run"}`);
+
+ done();
+ });
+</script>