Truly independent web browser
LibWebView: Check a navigation's reported URLs against RequestServer
Andreas Klingf0949200dda3
diff --git a/Libraries/LibWebView/NavigationLoader.cpp b/Libraries/LibWebView/NavigationLoader.cppindex 9396ce006b9..7f881283e7a 100644--- a/Libraries/LibWebView/NavigationLoader.cpp+++ b/Libraries/LibWebView/NavigationLoader.cpp@@ -9,6 +9,7 @@#include <LibRequests/Request.h>#include <LibRequests/RequestClient.h>#include <LibRequests/RequestControlClient.h>+#include <LibURL/Parser.h>#include <LibURL/Site.h>#include <LibWebCommon/Fetch/Infrastructure/URL.h>#include <LibWebCommon/HTML/BrowsingContext.h>@@ -418,15 +419,49 @@ void NavigationLoader::acquire_response_body(WebContentClient const& population_m_response_body_request = request;}+// NB: The process that fetched a navigation's response reports the URLs it reached: the history entry's URL, and the+// last URLs of the response's and the request's URL lists.+template<typename Callback>+static bool any_reported_url(URL::URL const& history_entry_url, Web::HTML::NavigationParamsDescriptor const* navigation_params, Callback const& callback)+{+ if (callback(history_entry_url))+ return true;+ if (!navigation_params)+ return false;+ if (!navigation_params->response.url_list.is_empty() && callback(navigation_params->response.url_list.last()))+ return true;+ return navigation_params->request.has_value() && !navigation_params->request->url_list.is_empty() && callback(navigation_params->request->url_list.last());+}++// NB: The process that fetched the response reports the URLs it reached, and they name the document, its origin, and+// the process that hosts it. RequestServer follows no redirects, so the URL of the request it made for the response+// is the response's URL. A navigation that reports another URL is made up: the document it would create could+// place another site's response in a process of the fetching process's choosing. Such a navigation is canceled.+bool NavigationLoader::reported_urls_match_the_response_url(URL::URL const& response_url) const+{+ auto const* navigation_params = m_result->navigation_params.get_pointer<Web::HTML::NavigationParamsDescriptor>();+ // NB: RequestServer's URL is parsed from its serialization, and a URL the UI process gave a navigation need not be.+ return !any_reported_url(m_request.history_entry.url, navigation_params, [&](URL::URL const& url) {+ auto parsed_url = URL::Parser::basic_parse(url.serialize());+ return !parsed_url.has_value() || !parsed_url->equals(response_url, URL::ExcludeFragment::Yes);+ });+}+void NavigationLoader::set_response_body_callbacks(Requests::Request& request){auto weak_this = make_weak_ptr();request.set_unbuffered_request_callbacks(- [weak_this](NonnullRefPtr<HTTP::HeaderList> response_header_list, Optional<u32>, Optional<String> const&, Requests::CacheState) {- if (auto* loader = weak_this.ptr()) {- loader->m_response_header_list = move(response_header_list);- loader->did_acquire(true);+ // NB: The request owns its callbacks, so it is alive while they run.+ [weak_this, &request](NonnullRefPtr<HTTP::HeaderList> response_header_list, Optional<u32>, Optional<String> const&, Requests::CacheState) {+ auto* loader = weak_this.ptr();+ if (!loader)+ return;+ if (!request.response_url().has_value() || !loader->reported_urls_match_the_response_url(*request.response_url())) {+ loader->did_acquire(false);+ return;}+ loader->m_response_header_list = move(response_header_list);+ loader->did_acquire(true);},[](Requests::ResponseData) { },[](Core::ImmutableBytes) { },diff --git a/Libraries/LibWebView/NavigationLoader.h b/Libraries/LibWebView/NavigationLoader.hindex 8da1b74e2d9..2777b084f93 100644--- a/Libraries/LibWebView/NavigationLoader.h+++ b/Libraries/LibWebView/NavigationLoader.h@@ -92,6 +92,7 @@ private:}void determine_the_origin_of_the_response();+ bool reported_urls_match_the_response_url(URL::URL const&) const;void did_acquire(bool succeeded);void set_response_body_callbacks(Requests::Request&);void did_designate_response_body_adopter();diff --git a/Tests/LibWebView/TestRendererResponseBodyClaims.cpp b/Tests/LibWebView/TestRendererResponseBodyClaims.cppindex 94c758c0412..dac07a8f9f1 100644--- a/Tests/LibWebView/TestRendererResponseBodyClaims.cpp+++ b/Tests/LibWebView/TestRendererResponseBodyClaims.cpp@@ -276,6 +276,13 @@ public:// Fetches the response through the client, and has the UI process acquire its body.void fetch_response_through(Requests::RequestClient& client, Optional<Utf16String> top_level_site = {})+ {+ VERIFY(try_fetch_response_through(client, move(top_level_site), [](auto&) { }));+ }++ // Fetches the response through the client, and has the UI process acquire its body with the result as the client's+ // process reports it. Returns whether the UI process acquired the body.+ bool try_fetch_response_through(Requests::RequestClient& client, Optional<Utf16String> top_level_site, Function<void(Web::HTML::NavigationPopulationResult&)> const& report){m_fetch = start_leased_request(client, m_url);m_fetch->set_unbuffered_request_callbacks(@@ -285,14 +292,18 @@ public:[](auto, auto const&, auto) { });Core::EventLoop::current().spin_until([&] { return m_received_response_headers; });- m_loader->did_finish_navigation_params_creation(result_with_response_body_of(client.request_server_client_id(), m_fetch->id(), move(top_level_site)));+ auto result = result_with_response_body_of(client.request_server_client_id(), m_fetch->id(), move(top_level_site));+ report(result);+ m_loader->did_finish_navigation_params_creation(move(result));Optional<bool> acquired;m_loader->acquire_response_body(m_view.page().client(), [&](bool succeeded) { acquired = succeeded; });Core::EventLoop::current().spin_until([&] { return acquired.has_value(); });- VERIFY(*acquired);+ if (!*acquired)+ return false;m_view.traversable().navigation_with_id(m_navigation_id)->state = WebView::CanonicalNavigation::Populating { m_loader };+ return true;}void move_response_body_to(WebView::HeadlessWebView const& host_view)@@ -585,6 +596,59 @@ TEST_CASE(made_up_file_url_does_not_reach_a_navigation)EXPECT_NE(navigation->url.scheme(), "file"sv);}+// The URLs a process reports for a navigation's response name its document and the process that hosts it. The UI+// process acquires the response body only if they are the URL that RequestServer fetched, apart from the fragment.+TEST_CASE(reported_response_url_is_the_url_requestserver_fetched)+{+ TestFixture fixture;+ TestResponseServer server;+ auto view = fixture.create_view();+ auto fetching_client = connect_client(*view);+ auto made_up_url = URL::Parser::basic_parse("https://victim.example/"sv).release_value();++ auto acquires = [&](Function<void(URL::URL const& fetched_url, Web::HTML::NavigationPopulationResult&)> report) {+ auto url = server.unique_url();+ TestPopulation population { *view, url };+ return population.try_fetch_response_through(*fetching_client, {}, [&](auto& result) { report(url, result); });+ };+ auto navigation_params = [](Web::HTML::NavigationPopulationResult& result) -> Web::HTML::NavigationParamsDescriptor& {+ return result.navigation_params.get<Web::HTML::NavigationParamsDescriptor>();+ };+ auto request_with_url_list = [](Vector<URL::URL> url_list) {+ return Web::HTML::NavigationRequestDescriptor {+ .url_list = move(url_list),+ .method = "GET"sv,+ .client_is_null = false,+ .referrer = Web::Fetch::Infrastructure::RequestReferrer::NoReferrer,+ .referrer_policy = Web::ReferrerPolicy::ReferrerPolicy::EmptyString,+ .policy_container = {},+ .redirect_count = 0,+ .text_directive_user_activation = false,+ .browsing_context_group_has_multiple_contexts = false,+ .cache_mode = HTTP::CacheMode::Default,+ };+ };++ EXPECT(acquires([&](auto const& fetched_url, auto& result) {+ auto url_with_fragment = fetched_url;+ url_with_fragment.set_fragment("fragment"sv);+ navigation_params(result).response.url_list = { url_with_fragment };+ navigation_params(result).request = request_with_url_list({ fetched_url });+ result.redirected_url = url_with_fragment;+ }));++ EXPECT(!acquires([&](auto const&, auto& result) {+ navigation_params(result).response.url_list = { made_up_url };+ }));+ EXPECT(!acquires([&](auto const& fetched_url, auto& result) {+ navigation_params(result).request = request_with_url_list({ fetched_url, made_up_url });+ }));+ EXPECT(!acquires([&](auto const&, auto& result) {+ result.redirected_url = made_up_url;+ }));+ EXPECT(view->page().client().is_open());+}+// A renderer whose connection to RequestServer was replaced keeps the earlier connection until it closes it. The UI// process takes a download from that connection, whether or not it has adopted the response body.TEST_CASE(download_from_a_replaced_connection_that_is_still_open)