Skip to content

Truly independent web browser

LibHTTP+LibWebView+RequestServer: Open no cache without a site

Andreas Klingfb776f70144b
diff --git a/Libraries/LibHTTP/Cache/DiskCache.cpp b/Libraries/LibHTTP/Cache/DiskCache.cpp
index 6eca9f1289d..27bd1a8f587 100644
--- a/Libraries/LibHTTP/Cache/DiskCache.cpp
+++ b/Libraries/LibHTTP/Cache/DiskCache.cpp
@@ -61,7 +61,7 @@ ErrorOr<Optional<DiskCache>> DiskCache::create(Mode mode, LexicalPath const& cac
// Start with a clean slate in test mode. The index is in-memory, but the cache files themselves are leftover.
if (mode == Mode::Testing)
- (void)FileSystem::remove(cache_directory.string(), FileSystem::RecursionMode::Allowed);
+ remove(mode, cache_root);
TRY(Core::Directory::create(cache_directory, Core::Directory::CreateDirectories::Yes));
@@ -81,6 +81,11 @@ ErrorOr<Optional<DiskCache>> DiskCache::create(Mode mode, LexicalPath const& cac
return DiskCache { mode, move(database), move(cache_directory), move(index) };
}
+void DiskCache::remove(Mode mode, LexicalPath const& cache_root)
+{
+ (void)FileSystem::remove(cache_root.append(cache_directory_for_mode(mode)).string(), FileSystem::RecursionMode::Allowed);
+}
+
DiskCache::DiskCache(Mode mode, NonnullRefPtr<Database::Database> database, LexicalPath cache_directory, CacheIndex index)
: m_mode(mode)
, m_database(move(database))
diff --git a/Libraries/LibHTTP/Cache/DiskCache.h b/Libraries/LibHTTP/Cache/DiskCache.h
index be64e7ad6cc..be2f360ef4f 100644
--- a/Libraries/LibHTTP/Cache/DiskCache.h
+++ b/Libraries/LibHTTP/Cache/DiskCache.h
@@ -33,6 +33,7 @@ public:
Testing,
};
static ErrorOr<Optional<DiskCache>> create(Mode, LexicalPath const& cache_root);
+ static void remove(Mode, LexicalPath const& cache_root);
DiskCache(DiskCache&&);
DiskCache& operator=(DiskCache&&);
diff --git a/Libraries/LibWebView/HelperProcess.cpp b/Libraries/LibWebView/HelperProcess.cpp
index 383c01dc5d0..513a81ae7c7 100644
--- a/Libraries/LibWebView/HelperProcess.cpp
+++ b/Libraries/LibWebView/HelperProcess.cpp
@@ -665,7 +665,7 @@ void apply_dns_settings(Requests::RequestControlClient& client)
});
}
-ErrorOr<NonnullRefPtr<Requests::RequestControlClient>> launch_request_server_process(ByteString const& cache_path, Optional<Utf16String> const& site, bool sites_have_their_own, HTTPDiskCacheMode http_disk_cache_mode)
+ErrorOr<NonnullRefPtr<Requests::RequestControlClient>> launch_request_server_process(ByteString const& cache_path, Optional<Utf16String> const& site, HTTPDiskCacheMode http_disk_cache_mode)
{
auto const& browser_options = Application::browser_options();
auto const& request_server_options = Application::request_server_options();
@@ -678,8 +678,6 @@ ErrorOr<NonnullRefPtr<Requests::RequestControlClient>> launch_request_server_pro
if (site.has_value()) {
arguments.append("--site"sv);
arguments.append(site->to_byte_string());
- } else if (sites_have_their_own) {
- arguments.append("--sites-have-their-own"sv);
}
if (browser_options.disable_sandbox == DisableSandbox::Yes)
diff --git a/Libraries/LibWebView/HelperProcess.h b/Libraries/LibWebView/HelperProcess.h
index 73dab12abd5..21aaa3d2362 100644
--- a/Libraries/LibWebView/HelperProcess.h
+++ b/Libraries/LibWebView/HelperProcess.h
@@ -31,9 +31,8 @@ WEBVIEW_API ErrorOr<NonnullRefPtr<VideoDecoderServerControlClient>> launch_video
WEBVIEW_API void shut_down_video_decoder_servers();
WEBVIEW_API ErrorOr<NonnullRefPtr<WebView::CompositorClient>> launch_compositor_process();
WEBVIEW_API ErrorOr<NonnullRefPtr<WebView::WebWorkerClient>> launch_web_worker_process(Web::HTML::AgentType, IsPrivate, Web::HTML::WorkerAgentId);
-// Serves the given site's partitions, or all partitions if site is empty. With sites_have_their_own,
-// the server with no site only handles site requests in transit to their site's RequestServer.
-WEBVIEW_API ErrorOr<NonnullRefPtr<Requests::RequestControlClient>> launch_request_server_process(ByteString const& cache_path, Optional<Utf16String> const& site, bool sites_have_their_own, HTTPDiskCacheMode);
+// Serves the given site's partitions, or all partitions if site is empty.
+WEBVIEW_API ErrorOr<NonnullRefPtr<Requests::RequestControlClient>> launch_request_server_process(ByteString const& cache_path, Optional<Utf16String> const& site, HTTPDiskCacheMode);
WEBVIEW_API void apply_dns_settings(Requests::RequestControlClient&);
struct ImageDecoderConnection {
diff --git a/Libraries/LibWebView/RequestServerManager.cpp b/Libraries/LibWebView/RequestServerManager.cpp
index 18dcda0309d..530da6c2354 100644
--- a/Libraries/LibWebView/RequestServerManager.cpp
+++ b/Libraries/LibWebView/RequestServerManager.cpp
@@ -11,6 +11,7 @@
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibFileSystem/FileSystem.h>
+#include <LibHTTP/Cache/DiskCache.h>
#include <LibHTTP/Cache/Utilities.h>
#include <LibWebView/Application.h>
#include <LibWebView/HelperProcess.h>
@@ -46,6 +47,9 @@ RequestServerManager& RequestServerManager::the()
RequestServerManager::RequestServerManager()
: m_maximum_site_instances(Application::request_server_options().maximum_site_request_servers)
{
+ // Older versions kept the responses of every site in the cache of the RequestServer with no site.
+ if (uses_site_request_servers())
+ HTTP::DiskCache::remove(HTTP::DiskCache::Mode::Normal, LexicalPath { Application::request_server_options().cache_path });
}
RequestServerManager::~RequestServerManager() = default;
@@ -119,8 +123,10 @@ ErrorOr<NonnullRefPtr<RequestServerInstance>> RequestServerManager::launch(Nonnu
ErrorOr<void> RequestServerManager::launch_process(RequestServerInstance& instance)
{
// Private sessions keep nothing on disk. Every RequestServer of a session would open the same cache otherwise.
- auto disk_cache_mode = instance.session().is_private() == IsPrivate::Yes ? HTTPDiskCacheMode::Disabled : Application::request_server_options().http_disk_cache_mode;
- auto control_client = TRY(launch_request_server_process(instance.cache_path(), instance.site(), uses_site_request_servers(), disk_cache_mode));
+ // A site's responses only pass through the RequestServer with no site on their way to the site's own.
+ auto keeps_disk_cache = instance.session().is_private() == IsPrivate::No && (instance.site().has_value() || !uses_site_request_servers());
+ auto disk_cache_mode = keeps_disk_cache ? Application::request_server_options().http_disk_cache_mode : HTTPDiskCacheMode::Disabled;
+ auto control_client = TRY(launch_request_server_process(instance.cache_path(), instance.site(), disk_cache_mode));
auto weak_instance = instance.make_weak_ptr();
control_client->on_client_disconnected = [weak_instance](int client_id) {
diff --git a/Services/RequestServer/Resolver.cpp b/Services/RequestServer/Resolver.cpp
index 08d2f6c4d94..791d422d40a 100644
--- a/Services/RequestServer/Resolver.cpp
+++ b/Services/RequestServer/Resolver.cpp
@@ -25,7 +25,6 @@ void set_default_certificate_path(ByteString default_certificate_path)
static HTTP::ProxyConfiguration g_proxy_configuration;
static Optional<Utf16String> g_process_top_level_site;
-static bool g_process_sites_have_their_own { false };
HTTP::ProxyConfiguration const& proxy_configuration()
{
@@ -47,16 +46,9 @@ void set_process_top_level_site(Optional<Utf16String> site)
g_process_top_level_site = move(site);
}
-void set_process_sites_have_their_own()
-{
- g_process_sites_have_their_own = true;
-}
-
bool process_keeps_responses_under(Utf16String const& top_level_site)
{
- if (g_process_top_level_site.has_value())
- return top_level_site == *g_process_top_level_site;
- return !g_process_sites_have_their_own;
+ return !g_process_top_level_site.has_value() || top_level_site == *g_process_top_level_site;
}
DNSInfo& DNSInfo::the()
diff --git a/Services/RequestServer/Resolver.h b/Services/RequestServer/Resolver.h
index e02fcb73d1a..5524e03994f 100644
--- a/Services/RequestServer/Resolver.h
+++ b/Services/RequestServer/Resolver.h
@@ -62,11 +62,8 @@ void set_proxy_configuration(HTTP::ProxyConfiguration);
Optional<Utf16String> const& process_top_level_site();
void set_process_top_level_site(Optional<Utf16String>);
-// Sites have RequestServers of their own, and this one has no site.
-void set_process_sites_have_their_own();
-
-// Cache only this server's site's responses. A server with no site caches all sites unless they have
-// their own RequestServers; other sites' responses are only in transit to their site's server.
+// Cache only this server's site's responses. Other sites' responses are only in transit to their site's server. A
+// server with no site has a disk cache only if sites have no servers of their own, so it caches all sites.
bool process_keeps_responses_under(Utf16String const& top_level_site);
}
diff --git a/Services/RequestServer/main.cpp b/Services/RequestServer/main.cpp
index 8126c3ab147..5402ba094c9 100644
--- a/Services/RequestServer/main.cpp
+++ b/Services/RequestServer/main.cpp
@@ -52,7 +52,6 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
StringView resource_map_path;
StringView cache_path;
StringView top_level_site;
- bool sites_have_their_own = false;
bool wait_for_debugger = false;
bool disable_sandbox = false;
bool enable_test_mode = false;
@@ -68,7 +67,6 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
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(sites_have_their_own, "Sites have RequestServers of their own; keep none of their responses", "sites-have-their-own");
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.add_option(enable_test_mode, "Enable test mode", "test-mode");
@@ -138,8 +136,6 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
if (!top_level_site.is_empty())
RequestServer::set_process_top_level_site(Utf16String::from_utf8(top_level_site));
- else if (sites_have_their_own)
- RequestServer::set_process_sites_have_their_own();
TRY(RequestServer::initialize_libcurl());
diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txt
index 1c86cf1b1e1..5e3c1efd47f 100644
--- a/Tests/LibWebView/CMakeLists.txt
+++ b/Tests/LibWebView/CMakeLists.txt
@@ -264,6 +264,7 @@ if (NOT WIN32)
TestRendererOriginClaims.cpp
TestRendererViolationReports.cpp
TestRequestServerClientSessions.cpp
+ TestRequestServerDiskCaches.cpp
TestShadowTreeTooltipsAndLinks.cpp
TestTabPerformanceMonitor.cpp
TestUserActivation.cpp
diff --git a/Tests/LibWebView/TestRequestServerDiskCaches.cpp b/Tests/LibWebView/TestRequestServerDiskCaches.cpp
new file mode 100644
index 00000000000..786e88079e5
--- /dev/null
+++ b/Tests/LibWebView/TestRequestServerDiskCaches.cpp
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "HeadlessTest.h"
+#include <AK/LexicalPath.h>
+#include <LibCore/Directory.h>
+#include <LibFileSystem/FileSystem.h>
+#include <LibTest/TestCase.h>
+#include <LibWebView/Application.h>
+#include <LibWebView/BrowsingSession.h>
+#include <LibWebView/RequestServerManager.h>
+
+// A RequestServer is sandboxed to the disk cache it opens, so only the one of a site in a normal session may have one.
+TEST_CASE(only_sites_of_normal_sessions_have_disk_caches)
+{
+ Test::HeadlessTestEnvironment environment { "TestRequestServerDiskCaches"sv };
+ auto app = TRY_OR_FAIL(environment.create_application());
+
+ auto& manager = WebView::Application::request_server_manager();
+ auto& normal_session = WebView::Application::default_session();
+ auto private_session = WebView::Application::session_for_new_view(WebView::IsPrivate::Yes);
+
+ auto site_server = TRY_OR_FAIL(manager.instance_for_site(normal_session, "https://normal.example"_utf16));
+ EXPECT(FileSystem::is_directory(LexicalPath::join(site_server->cache_path(), "Cache"sv).string()));
+
+ // A directory named after the site would reveal that the site was visited in a private session.
+ auto private_site_server = TRY_OR_FAIL(manager.instance_for_site(*private_session, "https://private.example"_utf16));
+ EXPECT(!FileSystem::exists(private_site_server->cache_path()));
+
+ auto browser_server = TRY_OR_FAIL(manager.browser_instance(normal_session));
+ auto shared_cache = LexicalPath::join(browser_server->cache_path(), "Cache"sv).string();
+ EXPECT(!FileSystem::exists(shared_cache));
+
+ // A profile from before sites had RequestServers of their own has a shared cache that nothing uses any more.
+ TRY_OR_FAIL(Core::Directory::create(shared_cache, Core::Directory::CreateDirectories::Yes));
+ WebView::RequestServerManager new_manager;
+ EXPECT(!FileSystem::exists(shared_cache));
+}