Skip to content

Truly independent web browser

LibWebView: Accept downloads from a navigation’s destination

sideshowbarker91cf51ccd580
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index de7d82b6360..7c16888720c 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -2773,7 +2773,15 @@ Messages::WebContentClient::DidStartDownloadResponse WebContentPage::did_start_d
// received that response may claim it, and only while its population is in flight.
FileDownloader::ResponseFinished on_response_finished;
bool matches_in_flight_navigation = false;
- if (auto navigable = population_worker_navigable(navigable_id); navigable.has_value()) {
+ if (auto navigable = traversable().top_level_traversable().find(navigable_id); navigable.has_value()) {
+ auto ongoing_navigation = navigation_id.has_value() ? navigable->navigation_with_id(*navigation_id) : nullptr;
+ bool owns_navigation = ongoing_navigation
+ && ((ongoing_navigation->state.has<CanonicalNavigation::CreatingNavigationParams>() && ongoing_navigation->worker() == this)
+ || (ongoing_navigation->state.has<CanonicalNavigation::Populating>() && ongoing_navigation->host() == this));
+ // The active host can report a response after navigation is canceled, so BiDi still needs its terminal error.
+ if (!owns_navigation && !traversable().hosts(*navigable, *this))
+ return { Optional<u64> {} };
+
if (auto* bidi = WebDriverBiDi::the();
bidi && network_response.is_object()) {
on_response_finished = [weak_bidi = bidi->make_weak_ptr(),
@@ -2787,10 +2795,7 @@ Messages::WebContentClient::DidStartDownloadResponse WebContentPage::did_start_d
timing, error);
};
}
- auto ongoing_navigation = navigation_id.has_value() ? navigable->navigation_with_id(*navigation_id) : nullptr;
- if (ongoing_navigation
- && (ongoing_navigation->state.has<CanonicalNavigation::CreatingNavigationParams>() || ongoing_navigation->state.has<CanonicalNavigation::Populating>())
- && ongoing_navigation->loader()->response_body_matches(request_server_client_id, request_server_request_id)) {
+ if (owns_navigation && ongoing_navigation->loader()->response_body_matches(request_server_client_id, request_server_request_id)) {
matches_in_flight_navigation = true;
navigable->did_cancel_navigation(*navigation_id);
}
diff --git a/Tests/LibWebView/TestNavigationDownloads.cpp b/Tests/LibWebView/TestNavigationDownloads.cpp
index 890518f7c64..c915091b9a3 100644
--- a/Tests/LibWebView/TestNavigationDownloads.cpp
+++ b/Tests/LibWebView/TestNavigationDownloads.cpp
@@ -72,9 +72,14 @@ public:
return URL::Parser::basic_parse(ByteString::formatted("http://127.0.0.1:{}/page", m_port)).release_value();
}
- URL::URL url() const
+ URL::URL url(bool cross_site = false) const
{
- return URL::Parser::basic_parse(ByteString::formatted("http://127.0.0.1:{}/file", m_port)).release_value();
+ return URL::Parser::basic_parse(ByteString::formatted("http://{}:{}/file", cross_site ? "localhost" : "127.0.0.1", m_port)).release_value();
+ }
+
+ URL::URL redirect_url() const
+ {
+ return URL::Parser::basic_parse(ByteString::formatted("http://127.0.0.1:{}/redirect", m_port)).release_value();
}
// The number of requests for url(), not counting the page.
@@ -122,6 +127,11 @@ private:
(void)write_all(client, response.bytes());
return;
}
+ if (request.string_view().starts_with("GET /redirect "sv)) {
+ auto response = ByteString::formatted("HTTP/1.1 302 Found\r\nLocation: {}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n", url(true));
+ (void)write_all(client, response.bytes());
+ return;
+ }
m_request_count.fetch_add(1);
auto head = ByteString::formatted("HTTP/1.1 200 OK\r\n{}Content-Length: {}\r\nConnection: close\r\n\r\n", m_headers, m_body.size());
@@ -254,6 +264,16 @@ void expect_navigation_downloads_cached_body(Core::AnonymousBuffer const& theme,
}
}
+void expect_cross_site_navigation_downloads_body(Core::AnonymousBuffer const& theme, StringView what, ByteString headers, ByteBuffer const& body, bool redirect, Optional<size_t> pause_after = {})
+{
+ TestHttpServer server { move(headers), body, pause_after };
+ auto view = WebView::HeadlessWebView::create(theme, { 800, 600 });
+ load_page(*view, server.page_url());
+ expect_download_holds_body(what, download_navigation_response(*view, redirect ? server.redirect_url() : server.url(true)), body);
+ EXPECT(!view->is_loading());
+ EXPECT_EQ(view->url(), server.page_url());
+}
+
}
TEST_CASE(navigation_downloads)
@@ -277,5 +297,11 @@ TEST_CASE(navigation_downloads)
expect_navigation_downloads_cached_body(theme, "a response sniffed from the disk cache"sv, "Cache-Control: max-age=3600\r\nContent-Type: application/octet-stream\r\n"sv, body);
expect_navigation_downloads_cached_body(theme, "an attachment from the disk cache"sv, "Cache-Control: max-age=3600\r\nContent-Disposition: attachment; filename=\"attachment.bin\"\r\n"sv, body);
+ for (bool redirect : { false, true }) {
+ expect_cross_site_navigation_downloads_body(theme, "a cross-site attachment"sv, "Content-Disposition: attachment; filename=\"cross-site.bin\"\r\n"sv, body, redirect);
+ expect_cross_site_navigation_downloads_body(theme, "a cross-site response sniffed as unrenderable"sv, "Content-Type: application/octet-stream\r\n"sv, body, redirect);
+ }
+ expect_cross_site_navigation_downloads_body(theme, "a cross-site response whose bytes arrive in parts"sv, "Content-Type: application/octet-stream\r\n"sv, body, false, 500);
+
outln("PASS: navigation responses download byte for byte");
}