Truly independent web browser
LibWeb: Let the history rate-limit test use up the limit directly
sideshowbarker9d52836aa545
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex 46527611d8c..2d9afb1038f 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -1673,6 +1673,9 @@ void LocalNavigable::set_active_document(GC::Ptr<DOM::Document> document)m_active_session_history_entry->document_state()->set_document_id(document_id);}+static constexpr u32 max_navigation_or_history_updates_in_rate_limit_window = 200;+static constexpr auto navigation_or_history_update_rate_limit_window_duration = AK::Duration::from_seconds(10);+// https://html.spec.whatwg.org/multipage/document-sequences.html#allowed-to-perform-a-navigation-or-history-updateLocalNavigable::NavigationOrHistoryUpdatePermission LocalNavigable::allowed_to_perform_a_navigation_or_history_update(){@@ -1680,24 +1683,30 @@ LocalNavigable::NavigationOrHistoryUpdatePermission LocalNavigable::allowed_to_p// either allowed or blocked.// NB: We block a navigable's navigations and history updates after it has done too many of them within a short// timespan, so a page cannot flood the session history or keep the event loop busy navigating.- static constexpr u32 max_updates_in_rate_limit_window = 200;- static constexpr auto rate_limit_window_duration = AK::Duration::from_seconds(10);-auto now = MonotonicTime::now_coarse();- if (now - m_navigation_or_history_update_rate_limit_window_start > rate_limit_window_duration) {+ if (!m_navigation_or_history_update_rate_limit_held_for_testing+ && now - m_navigation_or_history_update_rate_limit_window_start > navigation_or_history_update_rate_limit_window_duration) {m_navigation_or_history_update_rate_limit_window_start = now;m_navigation_or_history_updates_in_rate_limit_window = 0;}- if (m_navigation_or_history_updates_in_rate_limit_window >= max_updates_in_rate_limit_window)+ if (m_navigation_or_history_updates_in_rate_limit_window >= max_navigation_or_history_updates_in_rate_limit_window)return NavigationOrHistoryUpdatePermission::Blocked;++m_navigation_or_history_updates_in_rate_limit_window;return NavigationOrHistoryUpdatePermission::Allowed;}+void LocalNavigable::exhaust_navigation_or_history_update_rate_limit_for_testing()+{+ // The hold keeps the window from expiring with time, so only the reset between tests lifts the limit again.+ m_navigation_or_history_update_rate_limit_held_for_testing = true;+ m_navigation_or_history_updates_in_rate_limit_window = max_navigation_or_history_updates_in_rate_limit_window;+}+void LocalNavigable::reset_navigation_or_history_update_rate_limit_for_testing(){+ m_navigation_or_history_update_rate_limit_held_for_testing = false;m_navigation_or_history_update_rate_limit_window_start = MonotonicTime::now_coarse();m_navigation_or_history_updates_in_rate_limit_window = 0;}diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.hindex 0470a2d247a..8675c5cda49 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.h+++ b/Libraries/LibWeb/HTML/LocalNavigable.h@@ -235,6 +235,7 @@ public:Blocked,};NavigationOrHistoryUpdatePermission allowed_to_perform_a_navigation_or_history_update();+ void exhaust_navigation_or_history_update_rate_limit_for_testing();void reset_navigation_or_history_update_rate_limit_for_testing();virtual bool is_local_navigable() const override { return true; }@@ -635,6 +636,7 @@ private:MonotonicTime m_navigation_or_history_update_rate_limit_window_start { MonotonicTime::now_coarse() };u32 m_navigation_or_history_updates_in_rate_limit_window { 0 };+ bool m_navigation_or_history_update_rate_limit_held_for_testing { false };GC::Ptr<WindowProxy> m_window_proxy_after_unload;GC::Ptr<RemoteNavigable> m_provisional_for;diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cppindex 984dd3771e4..e1250d411ed 100644--- a/Libraries/LibWeb/Internals/Internals.cpp+++ b/Libraries/LibWeb/Internals/Internals.cpp@@ -2336,6 +2336,12 @@ GC::Ref<WebIDL::Promise> Internals::flush_session_history_traversal_queue()return promise;}+void Internals::exhaust_navigation_rate_limit()+{+ if (auto navigable = window().associated_document().navigable())+ navigable->exhaust_navigation_or_history_update_rate_limit_for_testing();+}+bool Internals::has_shadow_root(GC::Ref<DOM::Element> element){return element->shadow_root() != nullptr;diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.hindex e99a68f0f57..3d646f12c2b 100644--- a/Libraries/LibWeb/Internals/Internals.h+++ b/Libraries/LibWeb/Internals/Internals.h@@ -251,6 +251,7 @@ public:void reload_bypassing_cache_through_ui_process();void traverse_history_through_ui_process(i32 delta);GC::Ref<WebIDL::Promise> flush_session_history_traversal_queue();+ void exhaust_navigation_rate_limit();bool html_parser_body_is_exhausted(DOM::Document&);bool has_html_parser_end_state(DOM::Document& document) { return document.has_html_parser_end_state(); }diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idlindex 976c778628e..8eec2bf1238 100644--- a/Libraries/LibWeb/Internals/Internals.idl+++ b/Libraries/LibWeb/Internals/Internals.idl@@ -263,6 +263,9 @@ interface Internals {// Traverses the history by a delta as the browser's Back and Forward buttons do, with no source document.undefined traverseHistoryThroughUIProcess(long delta);Promise<undefined> flushSessionHistoryTraversalQueue();+ // Uses up this navigable's navigations and history updates until the state reset between tests, as if the page had+ // just made as many as the rate limit allows.+ undefined exhaustNavigationRateLimit();boolean hasHTMLParserEndState(Document document);boolean htmlParserBodyIsExhausted(Document document);diff --git a/Tests/LibWeb/test-web/test-history-rate-limit-reset.py b/Tests/LibWeb/test-web/test-history-rate-limit-reset.pyindex f707cb62374..e91fa4d4d63 100644--- a/Tests/LibWeb/test-web/test-history-rate-limit-reset.py+++ b/Tests/LibWeb/test-web/test-history-rate-limit-reset.py@@ -33,19 +33,20 @@ def main():shutil.copyfile(source / "Tests/LibWeb" / file, root / file)(root / "Text/input/wpt-import").mkdir()shutil.copyfile(source / "Tests/LibWeb/Text/input/include.js", root / "Text/input/include.js")- # More history updates than a navigable may make in one rate-limit window. The UI process applies the accepted- # ones one at a time, after the page's script has finished, so wait for that before ending the test: Otherwise- # the next test's load waits behind them, and on a slow runner can time out before they're done.- (root / "Text/input/flood.html").write_text(- '<!doctype html><script src="include.js"></script><script>asyncTest(async done => {'- 'console.log("HISTORY_RESET_FLOOD");'- 'for (let i = 0; i < 250; ++i) history.pushState(null, "", "#" + i);'- "await internals.flushSessionHistoryTraversalQueue();"- 'println("flooded"); done();'+ # Leaves the view's navigable at its rate limit, held there until the reset between tests. Using the limit up+ # directly, rather than with as many real history updates, leaves no backlog of them for the next test's load+ # to wait behind.+ (root / "Text/input/exhaust.html").write_text(+ '<!doctype html><script src="include.js"></script><script>test(() => {'+ 'console.log("HISTORY_RESET_EXHAUST");'+ "internals.exhaustNavigationRateLimit();"+ "const length = history.length;"+ 'history.pushState(null, "", "#blocked");'+ "println(`history update blocked: ${history.length === length}`);""});</script>")- with (root / "Text/expected/flood.txt").open("w", newline="\n") as file:- file.write("flooded\n")+ with (root / "Text/expected/exhaust.txt").open("w", newline="\n") as file:+ file.write("history update blocked: true\n")# Text tests precede reference tests. At -j1 these two files use the same view, without relying on directory# order. The page turns green only if its own history update goes through.(root / "Ref/input/consumer.html").write_text(@@ -96,8 +97,8 @@ def main():line.split(": ", 1) for line in (results / "harness-status.txt").read_text().splitlines() if ": " in line)assert status["completed"] == "2/2" and status["pass"] == "2", status- processes = re.findall(r"WebContent\((\d+)\).*HISTORY_RESET_(FLOOD|CONSUMER)", process.stdout)- assert len(processes) == 2 and processes[0][1] == "FLOOD" and processes[1][1] == "CONSUMER", processes+ processes = re.findall(r"WebContent\((\d+)\).*HISTORY_RESET_(EXHAUST|CONSUMER)", process.stdout)+ assert len(processes) == 2 and processes[0][1] == "EXHAUST" and processes[1][1] == "CONSUMER", processesassert processes[0][0] == processes[1][0], f"WebContent was not reused: {processes}"