Skip to content

Truly independent web browser

LibWeb: Rate limit history updates and traversals

Andreas Kling57ea51de4670
diff --git a/Libraries/LibWeb/HTML/History.cpp b/Libraries/LibWeb/HTML/History.cpp
index f5d2137c1b8..92268549f77 100644
--- a/Libraries/LibWeb/HTML/History.cpp
+++ b/Libraries/LibWeb/HTML/History.cpp
@@ -86,14 +86,19 @@ WebIDL::ExceptionOr<void> History::delta_traverse(WebIDL::Long delta)
if (!document.is_fully_active())
return WebIDL::SecurityError::create("Cannot perform go on a document that isn't fully active."_utf16);
- // 3. If delta is 0, then reload document's node navigable, and return.
+ // 3. If history's relevant global object's navigable's allowed to perform a navigation or history update returns
+ // blocked, then return.
+ if (auto navigable = document.navigable(); navigable && navigable->allowed_to_perform_a_navigation_or_history_update() == LocalNavigable::NavigationOrHistoryUpdatePermission::Blocked)
+ return {};
+
+ // 4. If delta is 0, then reload document's node navigable, and return.
if (delta == 0) {
VERIFY(document.navigable());
document.navigable()->reload();
return {};
}
- // 4. Traverse the history by a delta given document's node navigable's traversable navigable, delta, and with
+ // 5. Traverse the history by a delta given document's node navigable's traversable navigable, delta, and with
// sourceDocument set to document.
// NB: The UI process queues the operation on the traversable that hosts this page.
document.page().history_executor().traverse_the_history_by_delta(delta, document);
@@ -172,37 +177,33 @@ WebIDL::ExceptionOr<void> History::shared_history_push_replace_state(JS::Realm&
if (!document.is_fully_active())
return WebIDL::SecurityError::create("Cannot perform pushState or replaceState on a document that isn't fully active."_utf16);
- // 3. Optionally, throw a "SecurityError" DOMException. (For example, the user agent might disallow calls to these
- // methods that are invoked on a timer, or from event listeners that are not triggered in response to a clear
- // user action, or that are invoked in rapid succession.)
+ // 3. Let serializedData be StructuredSerializeForStorage(data). Rethrow any exceptions.
+ auto serialized_data = TRY(structured_serialize_for_storage(realm.vm(), data));
- // 4. Let serializedData be StructuredSerializeForStorage(data). Rethrow any exceptions.
- // FIXME: Actually rethrow exceptions here once we start using the serialized data.
- // Throwing here on data types we don't yet serialize will regress sites that use push/replaceState.
- auto serialized_data_or_error = structured_serialize_for_storage(realm.vm(), data);
- auto serialized_data = serialized_data_or_error.is_error() ? MUST(structured_serialize_for_storage(realm.vm(), JS::js_null())) : serialized_data_or_error.release_value();
-
- // 5. Let newURL be document's URL.
+ // 4. Let newURL be document's URL.
auto new_url = document.url();
- // 6. If url is not null or the empty string, then:
+ // 5. If url is not null or the empty string:
if (url.has_value() && !url->is_empty()) {
-
- // 1. Parse url, relative to the relevant settings object of history.
+ // 1. Set newURL to the result of encoding-parsing a URL given url, relative to the relevant settings object of
+ // history.
auto parsed_url = document.relevant_settings_object().encoding_parse_url(url->utf16_view());
- // 2. If that fails, then throw a "SecurityError" DOMException.
+ // 2. If newURL is failure, then throw a "SecurityError" DOMException.
if (!parsed_url.has_value())
return WebIDL::SecurityError::create("Cannot pushState or replaceState to incompatible URL"_utf16);
-
- // 3. Set newURL to the resulting URL record.
new_url = parsed_url.release_value();
- // 4. If document cannot have its URL rewritten to newURL, then throw a "SecurityError" DOMException.
+ // 3. If document cannot have its URL rewritten to newURL, then throw a "SecurityError" DOMException.
if (!can_have_its_url_rewritten(document, new_url))
return WebIDL::SecurityError::create("Cannot pushState or replaceState to incompatible URL"_utf16);
}
+ // 6. If history's relevant global object's navigable's allowed to perform a navigation or history update returns
+ // blocked, then return.
+ if (auto navigable = document.navigable(); navigable && navigable->allowed_to_perform_a_navigation_or_history_update() == LocalNavigable::NavigationOrHistoryUpdatePermission::Blocked)
+ return {};
+
// 7. Let navigation be history's relevant global object's navigation API.
auto navigation = document.window()->navigation();
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index c22228fb2c0..38034ee2be8 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -1590,6 +1590,29 @@ void LocalNavigable::set_active_document(GC::Ptr<DOM::Document> document)
m_active_session_history_entry->document_state()->set_document_id(document_id);
}
+// https://html.spec.whatwg.org/multipage/document-sequences.html#allowed-to-perform-a-navigation-or-history-update
+LocalNavigable::NavigationOrHistoryUpdatePermission LocalNavigable::allowed_to_perform_a_navigation_or_history_update()
+{
+ // An allowed to perform a navigation or history update algorithm, which is implementation-defined and returns
+ // 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) {
+ 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)
+ return NavigationOrHistoryUpdatePermission::Blocked;
+
+ ++m_navigation_or_history_updates_in_rate_limit_window;
+ return NavigationOrHistoryUpdatePermission::Allowed;
+}
+
Optional<u64> LocalNavigable::browsing_context_group_id() const
{
Navigable const* top_level_traversable = this;
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.h
index 8c03990ff1d..528e28727d4 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalNavigable.h
@@ -10,6 +10,7 @@
#include <AK/Assertions.h>
#include <AK/HashTable.h>
#include <AK/OwnPtr.h>
+#include <AK/Time.h>
#include <AK/Tuple.h>
#include <AK/Utf16String.h>
#include <AK/Utf16View.h>
@@ -223,6 +224,13 @@ public:
virtual void set_has_been_destroyed() override;
void remove_from_all_local_navigables();
+ // https://html.spec.whatwg.org/multipage/document-sequences.html#allowed-to-perform-a-navigation-or-history-update
+ enum class NavigationOrHistoryUpdatePermission : u8 {
+ Allowed,
+ Blocked,
+ };
+ NavigationOrHistoryUpdatePermission allowed_to_perform_a_navigation_or_history_update();
+
virtual bool is_local_navigable() const override { return true; }
CSSPixelPoint to_page_position(CSSPixelPoint);
@@ -571,6 +579,9 @@ private:
NavigationObserver::NavigationObserversList m_navigation_observers;
bool m_has_been_destroyed { false };
+
+ MonotonicTime m_navigation_or_history_update_rate_limit_window_start { MonotonicTime::now_coarse() };
+ u32 m_navigation_or_history_updates_in_rate_limit_window { 0 };
GC::Ptr<WindowProxy> m_window_proxy_after_unload;
GC::Ptr<RemoteNavigable> m_provisional_for;
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_go_too_many_calls.optional.txt b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_go_too_many_calls.optional.txt
index a57f47b6a4d..1ed18a14ffe 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_go_too_many_calls.optional.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_go_too_many_calls.optional.txt
@@ -2,7 +2,6 @@ Harness status: OK
Found 2 tests
-1 Pass
-1 Fail
-Fail history go too many calls
+2 Pass
+Pass history go too many calls
Pass fully active check should be before rate limit check
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_pushstate_too_many_calls.optional.txt b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_pushstate_too_many_calls.optional.txt
index fcaa46b3095..80f07693495 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_pushstate_too_many_calls.optional.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_pushstate_too_many_calls.optional.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail history pushState too many calls
\ No newline at end of file
+1 Pass
+Pass history pushState too many calls
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_pushstate_too_many_calls_ordering.optional.txt b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_pushstate_too_many_calls_ordering.optional.txt
index 432c5f21e65..ebc7de952b8 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_pushstate_too_many_calls_ordering.optional.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_pushstate_too_many_calls_ordering.optional.txt
@@ -2,9 +2,8 @@ Harness status: OK
Found 4 tests
-1 Pass
-3 Fail
-Fail pushState serializes state before checking the rate limit
-Fail pushState parses the URL before checking the rate limit
-Fail pushState checks whether the URL can be rewritten before checking the rate limit
+4 Pass
+Pass pushState serializes state before checking the rate limit
+Pass pushState parses the URL before checking the rate limit
+Pass pushState checks whether the URL can be rewritten before checking the rate limit
Pass pushState checks fully active before checking the rate limit
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_replacestate_too_many_calls.optional.txt b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_replacestate_too_many_calls.optional.txt
index aacb77f7387..517292485bd 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_replacestate_too_many_calls.optional.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_replacestate_too_many_calls.optional.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail history replaceState too many calls
\ No newline at end of file
+1 Pass
+Pass history replaceState too many calls
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_replacestate_too_many_calls_ordering.optional.txt b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_replacestate_too_many_calls_ordering.optional.txt
index ea89640ac3d..a12f232cb88 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_replacestate_too_many_calls_ordering.optional.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/history/the-history-interface/history_replacestate_too_many_calls_ordering.optional.txt
@@ -2,9 +2,8 @@ Harness status: OK
Found 4 tests
-1 Pass
-3 Fail
-Fail replaceState serializes state before checking the rate limit
-Fail replaceState parses the URL before checking the rate limit
-Fail replaceState checks whether the URL can be rewritten before checking the rate limit
+4 Pass
+Pass replaceState serializes state before checking the rate limit
+Pass replaceState parses the URL before checking the rate limit
+Pass replaceState checks whether the URL can be rewritten before checking the rate limit
Pass replaceState checks fully active before checking the rate limit
\ No newline at end of file