Skip to content

Truly independent web browser

LibWebView: Refuse HTTP(S) navigation responses a renderer made up

Andreas Klingd18406a985eb
diff --git a/Libraries/LibWebView/NavigationLoader.cpp b/Libraries/LibWebView/NavigationLoader.cpp
index 7f881283e7a..bcee1928be9 100644
--- a/Libraries/LibWebView/NavigationLoader.cpp
+++ b/Libraries/LibWebView/NavigationLoader.cpp
@@ -252,8 +252,8 @@ void NavigationLoader::obtain_the_response_policies(CanonicalNavigable const& na
auto const& response_url = navigation_params->response.url_list.is_empty() ? m_request.history_entry.url : navigation_params->response.url_list.last();
- // NB: A response that the fetching process read from its own HTTP memory cache has no RequestServer request, so
- // its headers are the ones that process reported.
+ // NB: A response without a RequestServer request, such as one for a non-HTTP(S) URL or one that WebDriver BiDi
+ // provided, has the headers that the fetching process reported.
auto header_list = m_response_header_list ? NonnullRefPtr { *m_response_header_list } : HTTP::HeaderList::create(navigation_params->response.headers);
// 17. Let responseCOOP be a new opener policy.
@@ -391,7 +391,7 @@ void NavigationLoader::acquire_response_body(WebContentClient const& population_
auto const* body_handle = response_body_handle(*m_result);
if (!body_handle) {
- did_acquire(true);
+ did_acquire(!reports_an_http_response_of_its_own());
return;
}
if (!response_body_was_fetched_by(*body_handle, population_worker)) {
@@ -447,6 +447,38 @@ bool NavigationLoader::reported_urls_match_the_response_url(URL::URL const& resp
});
}
+// NB: A navigation's response to a request with an HTTP(S) URL comes from RequestServer, from which the UI process
+// takes it. One the process that fetched it reports with a response of its own is made up: the document it would
+// create could run what that process chose with the origin of another site, in a process of that site. Such a
+// navigation is canceled.
+bool NavigationLoader::reports_an_http_response_of_its_own() const
+{
+ auto const* navigation_params = m_result->navigation_params.get_pointer<Web::HTML::NavigationParamsDescriptor>();
+ if (!navigation_params)
+ return false;
+
+ auto reports_an_http_url = any_reported_url(m_request.history_entry.url, navigation_params, [](URL::URL const& url) {
+ return Web::Fetch::Infrastructure::is_http_or_https_scheme(url.scheme());
+ });
+ if (!reports_an_http_url)
+ return false;
+
+ // AD-HOC: WebDriver BiDi provides the responses of the requests it intercepts, which the process running a request
+ // reports with the body that WebDriver gave it. The Hub saw that body when it routed the command, so the
+ // process must report exactly those bytes.
+ if (!m_response_provided_by_webdriver.has_value())
+ return true;
+ auto const& provided = *m_response_provided_by_webdriver;
+ auto const& response_url = navigation_params->response.url_list.is_empty() ? m_request.history_entry.url : navigation_params->response.url_list.last();
+ if (!provided.url.equals(response_url, URL::ExcludeFragment::Yes))
+ return true;
+ auto const* reported_body = navigation_params->response.body.get_pointer<ByteBuffer>();
+ auto reported_bytes = reported_body ? reported_body->bytes() : ReadonlyBytes {};
+ if (reported_bytes != provided.body.bytes())
+ return true;
+ return !reported_urls_match_the_response_url(response_url);
+}
+
void NavigationLoader::set_response_body_callbacks(Requests::Request& request)
{
auto weak_this = make_weak_ptr();
diff --git a/Libraries/LibWebView/NavigationLoader.h b/Libraries/LibWebView/NavigationLoader.h
index 2777b084f93..cd07d913788 100644
--- a/Libraries/LibWebView/NavigationLoader.h
+++ b/Libraries/LibWebView/NavigationLoader.h
@@ -82,6 +82,27 @@ public:
Web::HTML::NavigationPopulationResult take_result();
void reclaim_response_body_after_failed_handoff();
+ // The response that WebDriver BiDi provides for this navigation's intercepted request, which the process running the
+ // request reports with the body that WebDriver gave it. The first command to provide one keeps it, until that
+ // command fails.
+ struct ResponseProvidedByWebDriver {
+ URL::URL url;
+ ByteBuffer body;
+ u64 command_id { 0 };
+ };
+ bool set_response_provided_by_webdriver(ResponseProvidedByWebDriver response)
+ {
+ if (m_response_provided_by_webdriver.has_value())
+ return false;
+ m_response_provided_by_webdriver = move(response);
+ return true;
+ }
+ void withdraw_response_provided_by_webdriver(u64 command_id)
+ {
+ if (m_response_provided_by_webdriver.has_value() && m_response_provided_by_webdriver->command_id == command_id)
+ m_response_provided_by_webdriver.clear();
+ }
+
static bool response_body_belongs_to_another_process(Web::HTML::NavigationPopulationResult const&, WebContentClient const&);
static void discard(WebContentClient const& population_worker, Web::HTML::NavigationPopulationResult&);
@@ -93,6 +114,7 @@ private:
void determine_the_origin_of_the_response();
bool reported_urls_match_the_response_url(URL::URL const&) const;
+ bool reports_an_http_response_of_its_own() const;
void did_acquire(bool succeeded);
void set_response_body_callbacks(Requests::Request&);
void did_designate_response_body_adopter();
@@ -109,6 +131,7 @@ private:
bool m_response_body_was_handed_off { false };
WeakPtr<WebContentClient> m_response_body_host;
Optional<int> m_response_body_adopter_client_id;
+ Optional<ResponseProvidedByWebDriver> m_response_provided_by_webdriver;
Function<void(bool)> m_completion_steps;
Function<void()> m_steps_after_adopter_designation;
};
diff --git a/Libraries/LibWebView/WebDriverBiDi.cpp b/Libraries/LibWebView/WebDriverBiDi.cpp
index c3068dd03e5..06049e30ed4 100644
--- a/Libraries/LibWebView/WebDriverBiDi.cpp
+++ b/Libraries/LibWebView/WebDriverBiDi.cpp
@@ -16,7 +16,9 @@
#include <LibRequests/RequestTimingInfo.h>
#include <LibURL/Parser.h>
#include <LibWebView/CanonicalNavigable.h>
+#include <LibWebView/CanonicalNavigation.h>
#include <LibWebView/CanonicalTraversable.h>
+#include <LibWebView/NavigationLoader.h>
#include <LibWebView/ViewImplementation.h>
#include <LibWebView/WebContentClient.h>
#include <LibWebView/WebContentPage.h>
@@ -454,6 +456,21 @@ void WebDriverBiDi::fail_navigations_of(Function<bool(Web::HTML::CrossProcessId)
}
}
+static RefPtr<CanonicalNavigation> navigation_with_id(Web::HTML::CrossProcessId navigable_id, Utf16String const& navigation_id)
+{
+ auto* traversable = CanonicalTraversable::traversable_containing(navigable_id);
+ if (!traversable)
+ return nullptr;
+ RefPtr<CanonicalNavigation> navigation;
+ traversable->for_each_in_inclusive_subtree([&](CanonicalNavigable& navigable) {
+ if (navigable.id() != navigable_id)
+ return IterationDecision::Continue;
+ navigation = navigable.navigation_with_id(navigation_id);
+ return IterationDecision::Break;
+ });
+ return navigation;
+}
+
static bool navigable_exists(Web::HTML::CrossProcessId navigable_id)
{
return CanonicalTraversable::traversable_containing(navigable_id) != nullptr;
@@ -904,9 +921,11 @@ void WebDriverBiDi::did_receive_event(WebContentPage& page, Web::HTML::CrossProc
auto existing = m_blocked_network_requests.find(*id);
if (existing != m_blocked_network_requests.end() && existing->value.page != &page)
return;
- if (params.as_object().get_bool("isBlocked"sv).value_or(false))
- m_blocked_network_requests.set(*id, { page, navigable_id, m_next_network_interception_generation++ });
- else if (name.is_one_of("network.responseCompleted"sv, "network.fetchError"sv))
+ if (params.as_object().get_bool("isBlocked"sv).value_or(false)) {
+ auto url = request->get_string("url"sv).map([](auto const& url) { return URL::Parser::basic_parse(url); }).value_or({});
+ auto navigation_id = params.as_object().get_string("navigation"sv).map([](auto const& id) { return Utf16String::from_utf8(id); });
+ m_blocked_network_requests.set(*id, { page, navigable_id, m_next_network_interception_generation++, move(url), move(navigation_id) });
+ } else if (name.is_one_of("network.responseCompleted"sv, "network.fetchError"sv))
m_blocked_network_requests.remove(*id);
}
}
@@ -1080,12 +1099,32 @@ void WebDriverBiDi::run_command(u64 command_id, String const& method, JsonValue
auto id = params.get_string("request"sv).release_value();
if (auto blocked = m_blocked_network_requests.get(id); blocked.has_value() && blocked->page->routed_connection()) {
auto content_command_id = m_next_content_command_id++;
- m_pending_content_commands.set(content_command_id, [id, generation = blocked->generation, on_complete = move(on_complete)](Web::WebDriver::Response response) mutable {
+ // NB: The process running a navigation's request reports the response that WebDriver provides for it with
+ // a body of its own. The process also reports which navigation the request belongs to, so the
+ // navigation's loader takes such a body only for the request's URL and with the bytes WebDriver gave.
+ WeakPtr<NavigationLoader> provided_response_loader;
+ if (method == "network.provideResponse"sv && blocked->url.has_value() && blocked->navigation_id.has_value()) {
+ if (auto navigation = navigation_with_id(blocked->navigable_id, *blocked->navigation_id); navigation) {
+ auto overrides = Web::WebDriver::Network::deserialize_interception_overrides(method, params);
+ if (auto loader = navigation->loader(); loader && !overrides.is_error()) {
+ auto provided = loader->set_response_provided_by_webdriver(NavigationLoader::ResponseProvidedByWebDriver {
+ .url = *blocked->url,
+ .body = overrides.release_value().body.value_or({}),
+ .command_id = content_command_id,
+ });
+ if (provided)
+ provided_response_loader = loader->make_weak_ptr();
+ }
+ }
+ }
+ m_pending_content_commands.set(content_command_id, [id, content_command_id, generation = blocked->generation, provided_response_loader, on_complete = move(on_complete)](Web::WebDriver::Response response) mutable {
if (!response.is_error()) {
if (auto* bidi = WebDriverBiDi::the()) {
if (auto current = bidi->m_blocked_network_requests.get(id); current.has_value() && current->generation == generation)
bidi->m_blocked_network_requests.remove(id);
}
+ } else if (auto loader = provided_response_loader.strong_ref()) {
+ loader->withdraw_response_provided_by_webdriver(content_command_id);
}
on_complete(move(response));
});
diff --git a/Libraries/LibWebView/WebDriverBiDi.h b/Libraries/LibWebView/WebDriverBiDi.h
index 9e0123c1f03..da1dbc39275 100644
--- a/Libraries/LibWebView/WebDriverBiDi.h
+++ b/Libraries/LibWebView/WebDriverBiDi.h
@@ -169,6 +169,9 @@ private:
RefPtr<WebContentPage> page;
Web::HTML::CrossProcessId navigable_id;
u64 generation;
+ Optional<URL::URL> url;
+ // The id of the navigation the request fetches the response of, if it does.
+ Optional<Utf16String> navigation_id;
};
u64 m_next_network_interception_generation { 0 };
HashMap<String, BlockedNetworkRequest> m_blocked_network_requests;
diff --git a/Tests/LibWebView/TestRendererResponseBodyClaims.cpp b/Tests/LibWebView/TestRendererResponseBodyClaims.cpp
index dac07a8f9f1..068001229ad 100644
--- a/Tests/LibWebView/TestRendererResponseBodyClaims.cpp
+++ b/Tests/LibWebView/TestRendererResponseBodyClaims.cpp
@@ -306,6 +306,21 @@ public:
return true;
}
+ // Has the UI process acquire the response that the view's process reports with a body of its own. Returns whether
+ // the UI process acquired it.
+ bool try_acquire_response_with_a_body_of_its_own(Function<void(Web::HTML::NavigationPopulationResult&)> const& report)
+ {
+ auto result = result_with_response_body_of(m_view.page().client().request_server_site_bindings().client_id().value(), 0);
+ result.navigation_params.get<Web::HTML::NavigationParamsDescriptor>().response.body = MUST(ByteBuffer::copy("<!DOCTYPE html><script>document.title = location.origin</script>"sv.bytes()));
+ 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(); });
+ return *acquired;
+ }
+
void move_response_body_to(WebView::HeadlessWebView const& host_view)
{
Optional<bool> moved;
@@ -649,6 +664,32 @@ TEST_CASE(reported_response_url_is_the_url_requestserver_fetched)
EXPECT(view->page().client().is_open());
}
+// A response to a request with an HTTP(S) URL comes from RequestServer. The UI process acquires a response that a
+// process reports with a body of its own only if none of the navigation's URLs is an HTTP(S) URL.
+TEST_CASE(reported_http_response_with_a_body_of_its_own_is_refused)
+{
+ TestFixture fixture;
+ auto view = fixture.create_view();
+ auto victim_url = URL::Parser::basic_parse("https://victim.example/account"sv).release_value();
+ auto data_url = URL::Parser::basic_parse("data:text/html,<title>data</title>"sv).release_value();
+
+ auto acquires = [&](URL::URL const& url, Function<void(Web::HTML::NavigationPopulationResult&)> const& report) {
+ TestPopulation population { *view, url };
+ return population.try_acquire_response_with_a_body_of_its_own(report);
+ };
+ auto response_url_list = [](Web::HTML::NavigationPopulationResult& result) -> Vector<URL::URL>& {
+ return result.navigation_params.get<Web::HTML::NavigationParamsDescriptor>().response.url_list;
+ };
+
+ EXPECT(acquires(data_url, [&](auto& result) { response_url_list(result) = { data_url }; }));
+
+ EXPECT(!acquires(victim_url, [&](auto& result) { response_url_list(result) = { victim_url }; }));
+ EXPECT(!acquires(victim_url, [](auto&) { }));
+ EXPECT(!acquires(data_url, [&](auto& result) { response_url_list(result) = { victim_url }; }));
+ EXPECT(!acquires(data_url, [&](auto& result) { result.redirected_url = victim_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)