Truly independent web browser
RequestServer: Only cache responses for the site it serves
Ali Mohammad Purf6f53e32974e
diff --git a/Services/RequestServer/Request.cpp b/Services/RequestServer/Request.cppindex 35c2167c206..34dc3323e76 100644--- a/Services/RequestServer/Request.cpp+++ b/Services/RequestServer/Request.cpp@@ -541,6 +541,10 @@ Request::Request(, m_response_headers(HTTP::HeaderList::create()), m_transfer_lease(move(transfer_lease)){+ // A response for another site is only passing through this RequestServer on its way to that site's.+ if (auto const& site = process_top_level_site(); site.has_value() && m_disk_cache.has_value() && m_network_isolation_key->top_level_site != *site)+ m_disk_cache.clear();+if constexpr (REQUESTSERVER_WIRE_DEBUG)wire_stats().ensure(this).created_at = MonotonicTime::now();}diff --git a/Services/RequestServer/Resolver.cpp b/Services/RequestServer/Resolver.cppindex 5d78b79000c..1b0a3c00967 100644--- a/Services/RequestServer/Resolver.cpp+++ b/Services/RequestServer/Resolver.cpp@@ -24,6 +24,7 @@ void set_default_certificate_path(ByteString default_certificate_path)}static HTTP::ProxyConfiguration g_proxy_configuration;+static Optional<Utf16String> g_process_top_level_site;HTTP::ProxyConfiguration const& proxy_configuration(){@@ -35,6 +36,16 @@ void set_proxy_configuration(HTTP::ProxyConfiguration proxy_configuration)g_proxy_configuration = move(proxy_configuration);}+Optional<Utf16String> const& process_top_level_site()+{+ return g_process_top_level_site;+}++void set_process_top_level_site(Optional<Utf16String> site)+{+ g_process_top_level_site = move(site);+}+DNSInfo& DNSInfo::the(){static DNSInfo g_dns_info;diff --git a/Services/RequestServer/Resolver.h b/Services/RequestServer/Resolver.hindex 2d4350b088a..52a65b630be 100644--- a/Services/RequestServer/Resolver.h+++ b/Services/RequestServer/Resolver.h@@ -9,6 +9,7 @@#include <AK/Optional.h>#include <AK/RefCounted.h>+#include <AK/Utf16String.h>#include <AK/Weakable.h>#include <LibCore/Forward.h>#include <LibDNS/Resolver.h>@@ -56,4 +57,9 @@ void set_default_certificate_path(ByteString);HTTP::ProxyConfiguration const& proxy_configuration();void set_proxy_configuration(HTTP::ProxyConfiguration);+// The top-level site this RequestServer serves, when there is one per site. Only responses for that site go into its+// disk cache; a response it fetches for another site is on its way to that site's RequestServer.+Optional<Utf16String> const& process_top_level_site();+void set_process_top_level_site(Optional<Utf16String>);+}diff --git a/Services/RequestServer/main.cpp b/Services/RequestServer/main.cppindex 26acfc6b942..f40605023bc 100644--- a/Services/RequestServer/main.cpp+++ b/Services/RequestServer/main.cpp@@ -50,6 +50,7 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)StringView http_disk_cache_mode;StringView resource_map_path;StringView cache_path;+ StringView top_level_site;bool wait_for_debugger = false;bool disable_sandbox = false;@@ -61,6 +62,7 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)args_parser.add_option(http_disk_cache_mode, "HTTP disk cache mode", "http-disk-cache-mode", 0, "mode");args_parser.add_option(resource_map_path, "Path to JSON file mapping URLs to local files", "resource-map", 0, "path");args_parser.add_option(cache_path, "Path to the profile cache", "cache-path", 0, "path");+ args_parser.add_option(top_level_site, "The top-level site this RequestServer serves", "site", 0, "site");args_parser.add_option(wait_for_debugger, "Wait for debugger", "wait-for-debugger");args_parser.add_option(disable_sandbox, "Disable process sandboxing", "disable-sandbox");args_parser.parse(arguments);@@ -122,6 +124,9 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)if (!cache_path.is_empty())(void)FileSystem::remove(LexicalPath::join(cache_path, "alt-svc-cache.txt"sv).string(), FileSystem::RecursionMode::Disallowed);+ if (!top_level_site.is_empty())+ RequestServer::set_process_top_level_site(Utf16String::from_utf8(top_level_site));+TRY(RequestServer::initialize_libcurl());if (!disable_sandbox)diff --git a/Tests/RequestServer/TestExportImport.cpp b/Tests/RequestServer/TestExportImport.cppindex 8c0ea84c733..c127181dcce 100644--- a/Tests/RequestServer/TestExportImport.cpp+++ b/Tests/RequestServer/TestExportImport.cpp@@ -456,3 +456,26 @@ TEST_CASE(only_unrestricted_clients_may_move_responses)importer.import_request(2, exported.release_value(), true);EXPECT(!importer.is_open());}++TEST_CASE(responses_for_other_sites_are_not_cached_by_a_site_request_server)+{+ Core::EventLoop event_loop;+ TestServer server { "own-site"sv };+ DrippingServer http_server { patterned_body(4 * KiB), 4 * KiB, AK::Duration::from_milliseconds(1) };+ auto url = http_server.url();+ auto key = navigation_key_for(url);++ RequestServer::set_process_top_level_site("https://other.example"_utf16);+ ScopeGuard reset_site = [] { RequestServer::set_process_top_level_site({}); };++ TestConnection client { server };+ for (u64 request_id : { 1u, 2u }) {+ client.start_request(request_id, url, key, false);+ auto headers = client.wait_for<Messages::RequestClient::HeadersBecameAvailable>(event_loop, request_id);+ // A request that never touches the disk cache reports no cache status at all.+ auto cache_status = headers->response_headers().first_matching([](auto const& header) { return header.name == HTTP::TEST_CACHE_STATUS_HEADER; });+ EXPECT(!cache_status.has_value());+ (void)client.wait_for<Messages::RequestClient::RequestFinished>(event_loop, request_id);+ }+ EXPECT_EQ(http_server.request_count(), 2u);+}