Truly independent web browser
LibWeb: Don't show simple dialogs when they are not allowed
Shannon Booth8bc21a602eeb
diff --git a/Libraries/LibWeb/HTML/Window.cpp b/Libraries/LibWeb/HTML/Window.cppindex fc4524ec8b5..67b2940e85f 100644--- a/Libraries/LibWeb/HTML/Window.cpp+++ b/Libraries/LibWeb/HTML/Window.cpp@@ -1530,6 +1530,23 @@ GC::Ref<Speech::SpeechSynthesis> Window::speech_synthesis()return *m_speech_synthesis;}+// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#cannot-show-simple-dialogs+bool Window::cannot_show_simple_dialogs() const+{+ // 1. If the active sandboxing flag set of window's associated Document has the sandboxed modals flag set, then+ // return true.+ if (has_flag(associated_document().active_sandboxing_flag_set(), SandboxingFlagSet::SandboxedModals))+ return true;++ // 2. If window's relevant agent's event loop's termination nesting level is nonzero, then optionally return true.+ if (main_thread_event_loop().termination_nesting_level() != 0)+ return true;++ // 3. Optionally, return true.+ // 4. Return false.+ return false;+}+// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-alertvoid Window::alert(Utf16String const& message){@@ -1537,14 +1554,23 @@ void Window::alert(Utf16String const& message)// Note: This method is defined using two overloads, instead of using an optional argument,// for historical reasons. The practical impact of this is that alert(undefined) is// treated as alert("undefined"), but alert() is treated as alert("").- // FIXME: Make this fully spec compliant.++ // 1. If we cannot show simple dialogs for this, then return.+ if (cannot_show_simple_dialogs())+ return;++ // FIXME: Make the remaining steps fully spec compliant.page().did_request_alert(message);}// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-confirmbool Window::confirm(Optional<Utf16String> const& message){- // FIXME: Make this fully spec compliant.+ // 1. If we cannot show simple dialogs for this, then return false.+ if (cannot_show_simple_dialogs())+ return false;++ // FIXME: Make the remaining steps fully spec compliant.// NOTE: `message` has an IDL-provided default value and is never empty.return page().did_request_confirm(*message);}@@ -1552,7 +1578,11 @@ bool Window::confirm(Optional<Utf16String> const& message)// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-promptOptional<Utf16String> Window::prompt(Optional<Utf16String> const& message, Optional<Utf16String> const& default_){- // FIXME: Make this fully spec compliant.+ // 1. If we cannot show simple dialogs for this, then return null.+ if (cannot_show_simple_dialogs())+ return {};++ // FIXME: Make the remaining steps fully spec compliant.auto result = page().did_request_prompt(*message, *default_);if (!result.has_value())return {};diff --git a/Libraries/LibWeb/HTML/Window.h b/Libraries/LibWeb/HTML/Window.hindex 4b57be86a6b..349e369116e 100644--- a/Libraries/LibWeb/HTML/Window.h+++ b/Libraries/LibWeb/HTML/Window.h@@ -330,6 +330,8 @@ private:void invoke_idle_callbacks();void invoke_idle_callback_timeout(u32 handle);+ bool cannot_show_simple_dialogs() const;+struct [[nodiscard]] NamedObjects {Vector<GC::Ref<Navigable>> navigables;Vector<GC::Ref<DOM::Element>> elements;diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-1.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-1.txtnew file mode 100644index 00000000000..69e54965d72--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-1.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass Frames without `allow-modals` should not be able to open modal dialogs\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-2.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-2.txtnew file mode 100644index 00000000000..69e54965d72--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-2.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass Frames without `allow-modals` should not be able to open modal dialogs\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-3.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-3.txtnew file mode 100644index 00000000000..69e54965d72--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-3.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass Frames without `allow-modals` should not be able to open modal dialogs\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-1.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-1.htmlnew file mode 100644index 00000000000..e8d06a147ea--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-1.html@@ -0,0 +1,13 @@+<!DOCTYPE html>+<meta charset="utf-8">+<title>iframe sandbox without allow_modals (alert)</title>+<link rel="author" title="Igalia" href="https://www.igalia.com/">+<link rel="help" href="https://html.spec.whatwg.org/multipage/#attr-iframe-sandbox">+<link rel="help" href="https://html.spec.whatwg.org/multipage/#the-iframe-element">+<script src="../../../../resources/testharness.js"></script>+<script src="../../../../resources/testharnessreport.js"></script>+<iframe sandbox="allow-scripts"></iframe>+<script src="support/iframe_sandbox_block_modals.js"></script>+<script>+ runTest("alert", undefined);+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-2.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-2.htmlnew file mode 100644index 00000000000..18a78137dc1--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-2.html@@ -0,0 +1,13 @@+<!DOCTYPE html>+<meta charset="utf-8">+<title>iframe sandbox without allow_modals (confirm)</title>+<link rel="author" title="Igalia" href="https://www.igalia.com/">+<link rel="help" href="https://html.spec.whatwg.org/multipage/#attr-iframe-sandbox">+<link rel="help" href="https://html.spec.whatwg.org/multipage/#the-iframe-element">+<script src="../../../../resources/testharness.js"></script>+<script src="../../../../resources/testharnessreport.js"></script>+<iframe sandbox="allow-scripts"></iframe>+<script src="support/iframe_sandbox_block_modals.js"></script>+<script>+ runTest("confirm", false);+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-3.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-3.htmlnew file mode 100644index 00000000000..ba1b306d093--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_block_modals-3.html@@ -0,0 +1,13 @@+<!DOCTYPE html>+<meta charset="utf-8">+<title>iframe sandbox without allow_modals (prompt)</title>+<link rel="author" title="Igalia" href="https://www.igalia.com/">+<link rel="help" href="https://html.spec.whatwg.org/multipage/#attr-iframe-sandbox">+<link rel="help" href="https://html.spec.whatwg.org/multipage/#the-iframe-element">+<script src="../../../../resources/testharness.js"></script>+<script src="../../../../resources/testharnessreport.js"></script>+<iframe sandbox="allow-scripts"></iframe>+<script src="support/iframe_sandbox_block_modals.js"></script>+<script>+ runTest("prompt", null);+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/support/iframe-that-opens-modals.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/support/iframe-that-opens-modals.htmlnew file mode 100644index 00000000000..50f56c6278a--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/support/iframe-that-opens-modals.html@@ -0,0 +1,23 @@+<!DOCTYPE html>+<script>+function openModal(name) {+ switch (name) {+ case "alert":+ return alert("MESSAGE");+ break;+ case "confirm":+ return confirm("MESSAGE?");+ break;+ case "prompt":+ return prompt("MESSAGE:", "DEFAULT VALUE");+ break;+ case "print":+ return print();+ break;+ }+}++onmessage = function(e) {+ parent.postMessage(openModal(e.data), "*");+}+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/support/iframe_sandbox_block_modals.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/support/iframe_sandbox_block_modals.jsnew file mode 100644index 00000000000..67733d8101b--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/support/iframe_sandbox_block_modals.js@@ -0,0 +1,18 @@+function runTest(modalName, expectedValue) {+ let timeOutForFailingToOpenModal = 500;+ let startTime;+ async_test(t => {+ let iframe = document.querySelector("iframe");+ iframe.onload = t.step_func(() => {+ window.addEventListener("message", t.step_func_done(e => {+ // This tests work by checking the call to open the modal diaglog will return immediately (or at least within timeOutForFailingToOpenModal).+ // If the modal dialog is not blocked, then it will wait for user input and the test will time out.+ assert_less_than(new Date().getTime() - startTime, timeOutForFailingToOpenModal, "Call to open modal dialog did not return immediately");+ assert_equals(e.data, expectedValue, "Call to open modal dialog did not return expected value");+ }));+ startTime = new Date().getTime();+ iframe.contentWindow.postMessage(modalName, "*");+ });+ iframe.src = "support/iframe-that-opens-modals.html";+ }, "Frames without `allow-modals` should not be able to open modal dialogs");+}