Skip to content

Truly independent web browser

LibWebView: Name site cache directories with a per-profile key

Andreas Kling6c7c028970d8
diff --git a/Libraries/LibWebView/RequestServerManager.cpp b/Libraries/LibWebView/RequestServerManager.cpp
index 530da6c2354..bbca43b39c4 100644
--- a/Libraries/LibWebView/RequestServerManager.cpp
+++ b/Libraries/LibWebView/RequestServerManager.cpp
@@ -4,12 +4,15 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Hex.h>
#include <AK/LexicalPath.h>
#include <AK/QuickSort.h>
-#include <AK/StringBuilder.h>
+#include <AK/Random.h>
#include <LibCore/DirIterator.h>
+#include <LibCore/Directory.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
+#include <LibCrypto/Authentication/HMAC.h>
#include <LibFileSystem/FileSystem.h>
#include <LibHTTP/Cache/DiskCache.h>
#include <LibHTTP/Cache/Utilities.h>
@@ -30,6 +33,8 @@ static constexpr u64 cache_eviction_target_percent = 90;
static constexpr auto cache_eviction_timeout = AK::Duration::from_seconds(10);
+static constexpr size_t site_cache_key_size = 32;
+
RequestServerInstance::RequestServerInstance(NonnullRefPtr<BrowsingSession> session, Optional<Utf16String> site, ByteString cache_path)
: m_session(move(session))
, m_site(move(site))
@@ -47,9 +52,13 @@ RequestServerManager& RequestServerManager::the()
RequestServerManager::RequestServerManager()
: m_maximum_site_instances(Application::request_server_options().maximum_site_request_servers)
{
+ if (!uses_site_request_servers())
+ return;
+
// 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 });
+ HTTP::DiskCache::remove(HTTP::DiskCache::Mode::Normal, LexicalPath { Application::request_server_options().cache_path });
+
+ m_site_cache_key = load_or_create_site_cache_key();
}
RequestServerManager::~RequestServerManager() = default;
@@ -111,7 +120,7 @@ void RequestServerManager::for_each_instance(Function<IterationDecision(RequestS
ErrorOr<NonnullRefPtr<RequestServerInstance>> RequestServerManager::launch(NonnullRefPtr<BrowsingSession> session, Optional<Utf16String> site)
{
- auto cache_path = site.has_value() ? cache_path_for_site(sites_directory(), *site) : Application::request_server_options().cache_path;
+ auto cache_path = site.has_value() ? cache_path_for_site(*site) : Application::request_server_options().cache_path;
auto instance = adopt_ref(*new RequestServerInstance(session, move(site), move(cache_path)));
TRY(launch_process(*instance));
m_instances.append(instance);
@@ -266,20 +275,40 @@ ByteString RequestServerManager::sites_directory() const
return LexicalPath::join(Application::request_server_options().cache_path, "RequestServer"sv, "Sites"sv).string();
}
-ByteString RequestServerManager::cache_path_for_site(ByteString const& sites_directory, Utf16String const& site)
+// The sandbox of a RequestServer does not stop it from asking whether a path exists, so a name that it could work out
+// itself would tell it which other sites the user has visited.
+ByteString RequestServerManager::cache_path_for_site(Utf16String const& site) const
{
- // Keep the site name readable; append a hash to distinguish sites after sanitizing and truncating it.
- StringBuilder name;
- for (auto character : site.to_byte_string()) {
- if (name.length() >= 48)
- break;
- if (is_ascii_alphanumeric(character) || character == '.' || character == '-')
- name.append(character);
- else
- name.append('_');
+ Crypto::Authentication::HMAC hmac(Crypto::Hash::HashKind::SHA256, m_site_cache_key);
+ auto site_bytes = site.to_byte_string();
+ auto digest = hmac.process(site_bytes.view());
+ return LexicalPath::join(sites_directory(), encode_hex(digest.bytes())).string();
+}
+
+ByteBuffer RequestServerManager::load_or_create_site_cache_key() const
+{
+ auto path = LexicalPath::join(Application::request_server_options().cache_path, "RequestServer"sv, "SiteCacheKey"sv);
+
+ if (auto file = Core::File::open(path.string(), Core::File::OpenMode::Read); !file.is_error()) {
+ if (auto key = file.value()->read_until_eof(); !key.is_error() && key.value().size() == site_cache_key_size)
+ return key.release_value();
}
- name.appendff("-{:08x}", site.hash());
- return LexicalPath::join(sites_directory, name.string_view()).string();
+
+ // Nothing can find the caches of sites that were named with another key, or by older versions.
+ (void)FileSystem::remove(sites_directory(), FileSystem::RecursionMode::Allowed);
+
+ auto key = MUST(ByteBuffer::create_uninitialized(site_cache_key_size));
+ fill_with_random(key.bytes());
+
+ auto store_key = [&]() -> ErrorOr<void> {
+ TRY(Core::Directory::create(path.parent(), Core::Directory::CreateDirectories::Yes));
+ auto file = TRY(Core::File::open(path.string(), Core::File::OpenMode::Write, 0600));
+ return file->write_until_depleted(key.bytes());
+ };
+ if (auto result = store_key(); result.is_error())
+ warnln("Unable to store the site cache key: {}", result.error());
+
+ return key;
}
// Private sessions keep nothing in the cache directories of their RequestServers.
diff --git a/Libraries/LibWebView/RequestServerManager.h b/Libraries/LibWebView/RequestServerManager.h
index b7134c33cf8..b5a075d24a6 100644
--- a/Libraries/LibWebView/RequestServerManager.h
+++ b/Libraries/LibWebView/RequestServerManager.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/ByteBuffer.h>
#include <AK/HashMap.h>
#include <AK/HashTable.h>
#include <AK/Optional.h>
@@ -110,7 +111,8 @@ private:
void retire_idle_instances();
ByteString sites_directory() const;
- static ByteString cache_path_for_site(ByteString const& sites_directory, Utf16String const& site);
+ ByteString cache_path_for_site(Utf16String const& site) const;
+ ByteBuffer load_or_create_site_cache_key() const;
bool is_live_cache_path(ByteString const&) const;
Vector<ByteString> dormant_site_cache_paths();
@@ -129,6 +131,9 @@ private:
Vector<NonnullRefPtr<RequestServerInstance>> m_instances;
size_t m_maximum_site_instances { 0 };
+ // Only the UI process has this. It names the cache directories of sites, see cache_path_for_site().
+ ByteBuffer m_site_cache_key;
+
Optional<HashMap<ByteString, DormantSiteCache>> m_dormant_site_caches;
// Time out evictions if a RequestServer disappears before answering.
Optional<MonotonicTime> m_cache_eviction_start_time;
diff --git a/Tests/LibWebView/TestRequestServerDiskCaches.cpp b/Tests/LibWebView/TestRequestServerDiskCaches.cpp
index 786e88079e5..dc7f992f5e8 100644
--- a/Tests/LibWebView/TestRequestServerDiskCaches.cpp
+++ b/Tests/LibWebView/TestRequestServerDiskCaches.cpp
@@ -39,3 +39,39 @@ TEST_CASE(only_sites_of_normal_sessions_have_disk_caches)
WebView::RequestServerManager new_manager;
EXPECT(!FileSystem::exists(shared_cache));
}
+
+// A RequestServer can ask whether any path exists, so it must not be able to work out where other sites' caches are.
+TEST_CASE(site_cache_directories_cannot_be_named_without_the_key)
+{
+ 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 = "https://normal.example"_utf16;
+
+ auto site_server = TRY_OR_FAIL(manager.instance_for_site(normal_session, site));
+ auto cache_path = LexicalPath { site_server->cache_path() };
+ EXPECT(!cache_path.basename().contains("normal"sv));
+
+ auto other_site_server = TRY_OR_FAIL(manager.instance_for_site(normal_session, "https://other.example"_utf16));
+ EXPECT_NE(other_site_server->cache_path(), cache_path.string());
+
+ // A site finds its cache again after a restart. A private session's RequestServer keeps this one from opening it.
+ {
+ WebView::RequestServerManager restarted_manager;
+ auto restarted_server = TRY_OR_FAIL(restarted_manager.instance_for_site(*private_session, site));
+ EXPECT_EQ(restarted_server->cache_path(), cache_path.string());
+ }
+
+ // Directories named by older versions leak the site, and nothing can find them again once the key is gone.
+ auto sites_directory = cache_path.parent();
+ auto readable_cache = sites_directory.append("https___normal.example-12345678"sv).string();
+ TRY_OR_FAIL(Core::Directory::create(readable_cache, Core::Directory::CreateDirectories::Yes));
+ TRY_OR_FAIL(FileSystem::remove(sites_directory.parent().append("SiteCacheKey"sv).string(), FileSystem::RecursionMode::Disallowed));
+
+ WebView::RequestServerManager new_manager;
+ EXPECT(!FileSystem::exists(readable_cache));
+ EXPECT(FileSystem::exists(sites_directory.parent().append("SiteCacheKey"sv).string()));
+}