Skip to content

Truly independent web browser

LibWebView: Don’t persist spoofed UA and navigator mode across restarts

sideshowbarkerab73ded1f8c1
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 4ecf3d70f97..d04d0862c63 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -2485,10 +2485,9 @@ void Application::initialize_actions()
};
};
- auto add_spoofed_value = [](auto& menu, auto name, auto value, auto& cached_value, auto request, auto persist, auto notify_workers) {
- auto action = Action::create_checkable(name, ActionID::SpoofUserAgent, [value, &cached_value, request, persist, notify_workers]() {
+ auto add_spoofed_value = [](auto& menu, auto name, auto value, auto& cached_value, auto request, auto notify_workers) {
+ auto action = Action::create_checkable(name, ActionID::SpoofUserAgent, [value, &cached_value, request, notify_workers]() {
cached_value = value;
- persist();
// Workers take no debug requests; the worker process manager hands the new value to every live worker.
notify_workers();
@@ -2768,32 +2767,23 @@ void Application::initialize_actions()
m_debug_menu->add_action(Action::create("Crash Compositor Process"sv, ActionID::CrashCompositorProcess, [this]() { crash_compositor_process(); }));
m_debug_menu->add_separator();
- // Both spoofing choices persist in the profile's settings. The --user-agent-preset flag overrides the saved preset for
- // this session only, and test mode ignores the saved choices so test results can't depend on the developer's profile.
- auto use_saved_spoofing_choices = m_web_content_options.is_test_mode == IsTestMode::No;
-
auto spoof_user_agent_menu = Menu::create_group("Spoof User Agent"sv);
- auto user_agent_preset = m_web_content_options.user_agent_preset;
- if (!user_agent_preset.has_value() && use_saved_spoofing_choices)
- user_agent_preset = m_settings->user_agent_preset();
- m_user_agent_string = user_agent_preset.has_value()
- ? *WebView::user_agents.get(*user_agent_preset)
+ m_user_agent_string = m_web_content_options.user_agent_preset.has_value()
+ ? *WebView::user_agents.get(*m_web_content_options.user_agent_preset)
: Web::default_user_agent;
auto notify_workers_of_user_agent = [this] { WorkerProcessManager::the().update_user_agent(user_agent_string()); };
- add_spoofed_value(spoof_user_agent_menu, "Disabled"sv, Web::default_user_agent, m_user_agent_string, "spoof-user-agent"sv, [this] { m_settings->set_user_agent_preset({}); }, notify_workers_of_user_agent);
+ add_spoofed_value(spoof_user_agent_menu, "Disabled"sv, Web::default_user_agent, m_user_agent_string, "spoof-user-agent"sv, notify_workers_of_user_agent);
for (auto const& user_agent : WebView::user_agents) {
- add_spoofed_value(spoof_user_agent_menu, user_agent.key, user_agent.value, m_user_agent_string, "spoof-user-agent"sv, [this, name = user_agent.key] { m_settings->set_user_agent_preset(name); }, notify_workers_of_user_agent);
+ add_spoofed_value(spoof_user_agent_menu, user_agent.key, user_agent.value, m_user_agent_string, "spoof-user-agent"sv, notify_workers_of_user_agent);
}
auto navigator_compatibility_mode_menu = Menu::create_group("Navigator Compatibility Mode"sv);
- m_navigator_compatibility_mode = navigator_compatibility_mode_to_string(use_saved_spoofing_choices
- ? m_settings->navigator_compatibility_mode()
- : Web::default_navigator_compatibility_mode);
+ m_navigator_compatibility_mode = navigator_compatibility_mode_to_string(Web::default_navigator_compatibility_mode);
auto notify_workers_of_navigator_compatibility_mode = [this] { WorkerProcessManager::the().update_navigator_compatibility_mode(navigator_compatibility_mode()); };
auto add_navigator_compatibility_mode = [&](StringView name, Web::NavigatorCompatibilityMode mode) {
- add_spoofed_value(navigator_compatibility_mode_menu, name, navigator_compatibility_mode_to_string(mode), m_navigator_compatibility_mode, "navigator-compatibility-mode"sv, [this, mode] { m_settings->set_navigator_compatibility_mode(mode); }, notify_workers_of_navigator_compatibility_mode);
+ add_spoofed_value(navigator_compatibility_mode_menu, name, navigator_compatibility_mode_to_string(mode), m_navigator_compatibility_mode, "navigator-compatibility-mode"sv, notify_workers_of_navigator_compatibility_mode);
};
add_navigator_compatibility_mode("Chrome"sv, Web::NavigatorCompatibilityMode::Chrome);
add_navigator_compatibility_mode("Gecko"sv, Web::NavigatorCompatibilityMode::Gecko);
diff --git a/Libraries/LibWebView/Settings.cpp b/Libraries/LibWebView/Settings.cpp
index ccce1fb3cf6..f3464676d57 100644
--- a/Libraries/LibWebView/Settings.cpp
+++ b/Libraries/LibWebView/Settings.cpp
@@ -20,7 +20,6 @@
#include <LibWebCommon/Loader/UserAgent.h>
#include <LibWebView/Application.h>
#include <LibWebView/Settings.h>
-#include <LibWebView/UserAgent.h>
#include <LibWebView/Utilities.h>
namespace WebView {
@@ -80,9 +79,6 @@ static constexpr auto DISK_CACHE_MAXIMUM_SIZE_KEY = "maxSize"sv;
static constexpr auto GLOBAL_PRIVACY_CONTROL_KEY = "globalPrivacyControl"sv;
-static constexpr auto USER_AGENT_PRESET_KEY = "userAgentPreset"sv;
-static constexpr auto NAVIGATOR_COMPATIBILITY_MODE_KEY = "navigatorCompatibilityMode"sv;
-
static constexpr auto BACKGROUND_NETWORKING_KEY = "backgroundNetworking"sv;
static constexpr auto BACKGROUND_NETWORKING_ENABLED_KEY = "enabled"sv;
static constexpr auto BACKGROUND_NETWORKING_FEATURES_KEY = "features"sv;
@@ -408,15 +404,6 @@ Settings Settings::create(ByteString settings_path, Vector<String> platform_lang
if (auto global_privacy_control = settings_json.value().get_bool(GLOBAL_PRIVACY_CONTROL_KEY); global_privacy_control.has_value())
settings.m_global_privacy_control = *global_privacy_control ? GlobalPrivacyControl::Yes : GlobalPrivacyControl::No;
- // A preset that's since been dropped or renamed falls back to the default User-Agent.
- if (auto user_agent_preset = settings_json.value().get_string(USER_AGENT_PRESET_KEY); user_agent_preset.has_value())
- settings.m_user_agent_preset = normalize_user_agent_name(*user_agent_preset);
-
- if (auto mode = settings_json.value().get_string(NAVIGATOR_COMPATIBILITY_MODE_KEY); mode.has_value()) {
- if (auto parsed_mode = navigator_compatibility_mode_from_string(*mode); parsed_mode.has_value())
- settings.m_navigator_compatibility_mode = *parsed_mode;
- }
-
if (auto background_networking = settings_json.value().get_object(BACKGROUND_NETWORKING_KEY); background_networking.has_value()) {
if (auto enabled = background_networking->get_bool(BACKGROUND_NETWORKING_ENABLED_KEY); enabled.has_value())
settings.m_background_networking_enabled = *enabled;
@@ -638,10 +625,6 @@ JsonValue Settings::serialize_json() const
settings.set(GLOBAL_PRIVACY_CONTROL_KEY, m_global_privacy_control == GlobalPrivacyControl::Yes);
- if (m_user_agent_preset.has_value())
- settings.set(USER_AGENT_PRESET_KEY, *m_user_agent_preset);
- settings.set(NAVIGATOR_COMPATIBILITY_MODE_KEY, navigator_compatibility_mode_to_string(m_navigator_compatibility_mode));
-
JsonObject background_network_features;
background_network_features.set(CONTENT_BLOCKER_SUBSCRIPTION_UPDATES_KEY, m_filter_list_updates_enabled);
@@ -1125,18 +1108,6 @@ void Settings::set_global_privacy_control(GlobalPrivacyControl global_privacy_co
observer.global_privacy_control_changed();
}
-void Settings::set_user_agent_preset(Optional<StringView> user_agent_preset_name)
-{
- m_user_agent_preset = user_agent_preset_name.has_value() ? normalize_user_agent_name(*user_agent_preset_name) : OptionalNone {};
- persist_settings();
-}
-
-void Settings::set_navigator_compatibility_mode(Web::NavigatorCompatibilityMode navigator_compatibility_mode)
-{
- m_navigator_compatibility_mode = navigator_compatibility_mode;
- persist_settings();
-}
-
DNSSettings Settings::parse_dns_settings(JsonValue const& dns_settings)
{
if (!dns_settings.is_object())
diff --git a/Libraries/LibWebView/Settings.h b/Libraries/LibWebView/Settings.h
index 927bdd6b323..398dd85e457 100644
--- a/Libraries/LibWebView/Settings.h
+++ b/Libraries/LibWebView/Settings.h
@@ -18,7 +18,6 @@
#include <LibHTTP/Cache/DiskCacheSettings.h>
#include <LibURL/URL.h>
#include <LibWebCommon/HTML/AutoplayPolicy.h>
-#include <LibWebCommon/Loader/UserAgent.h>
#include <LibWebCommon/WebView/BrowsingBehavior.h>
#include <LibWebView/Forward.h>
#include <LibWebView/Options.h>
@@ -217,12 +216,6 @@ public:
GlobalPrivacyControl global_privacy_control() const { return m_global_privacy_control; }
void set_global_privacy_control(GlobalPrivacyControl);
- Optional<StringView> user_agent_preset() const { return m_user_agent_preset; }
- void set_user_agent_preset(Optional<StringView> user_agent_preset_name);
-
- Web::NavigatorCompatibilityMode navigator_compatibility_mode() const { return m_navigator_compatibility_mode; }
- void set_navigator_compatibility_mode(Web::NavigatorCompatibilityMode);
-
bool background_networking_enabled() const { return m_background_networking_enabled; }
void set_background_networking_enabled(bool);
bool filter_list_updates_enabled() const { return m_filter_list_updates_enabled; }
@@ -296,9 +289,6 @@ private:
BrowsingDataSettings m_browsing_data_settings;
GlobalPrivacyControl m_global_privacy_control { GlobalPrivacyControl::No };
- Optional<StringView> m_user_agent_preset;
- Web::NavigatorCompatibilityMode m_navigator_compatibility_mode { Web::default_navigator_compatibility_mode };
-
bool m_background_networking_enabled { true };
bool m_filter_list_updates_enabled { false };
bool m_content_blocker_enabled { false };
diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txt
index 6c655e1e899..1ba641dacb6 100644
--- a/Tests/LibWebView/CMakeLists.txt
+++ b/Tests/LibWebView/CMakeLists.txt
@@ -22,7 +22,6 @@ set(TEST_SOURCES
TestSessionHistoryTraversalQueue.cpp
TestSessionStore.cpp
TestSiteCompatibilityDataLoading.cpp
- TestSpoofingSettings.cpp
TestStorageJar.cpp
TestTabPerformanceStats.cpp
TestWebViewURL.cpp
diff --git a/Tests/LibWebView/TestSpoofingSettings.cpp b/Tests/LibWebView/TestSpoofingSettings.cpp
deleted file mode 100644
index 28531e65167..00000000000
--- a/Tests/LibWebView/TestSpoofingSettings.cpp
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Copyright (c) 2026-present, the Ladybird developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <AK/LexicalPath.h>
-#include <LibCore/File.h>
-#include <LibCore/StandardPaths.h>
-#include <LibCore/System.h>
-#include <LibFileSystem/FileSystem.h>
-#include <LibTest/TestCase.h>
-#include <LibWebView/Settings.h>
-
-static ByteString settings_path()
-{
- return LexicalPath::join(Core::StandardPaths::tempfile_directory(), ByteString::formatted("test-spoofing-settings-{}.json", Core::System::getpid())).string();
-}
-
-static void remove_settings_file()
-{
- if (FileSystem::exists(settings_path()))
- MUST(FileSystem::remove(settings_path(), FileSystem::RecursionMode::Disallowed));
-}
-
-static void write_settings_file(StringView contents)
-{
- auto file = MUST(Core::File::open(settings_path(), Core::File::OpenMode::Write));
- MUST(file->write_until_depleted(contents.bytes()));
-}
-
-TEST_CASE(spoofing_settings_default_to_no_spoofing)
-{
- remove_settings_file();
-
- auto settings = WebView::Settings::create(settings_path());
- EXPECT(!settings.user_agent_preset().has_value());
- EXPECT_EQ(settings.navigator_compatibility_mode(), Web::NavigatorCompatibilityMode::Chrome);
-}
-
-TEST_CASE(spoofing_settings_are_persistent)
-{
- remove_settings_file();
-
- {
- auto settings = WebView::Settings::create(settings_path());
- settings.set_user_agent_preset("Firefox macOS Desktop"sv);
- settings.set_navigator_compatibility_mode(Web::NavigatorCompatibilityMode::Gecko);
- }
-
- {
- auto settings = WebView::Settings::create(settings_path());
- EXPECT_EQ(settings.user_agent_preset(), "Firefox macOS Desktop"sv);
- EXPECT_EQ(settings.navigator_compatibility_mode(), Web::NavigatorCompatibilityMode::Gecko);
-
- settings.set_user_agent_preset({});
- settings.set_navigator_compatibility_mode(Web::NavigatorCompatibilityMode::WebKit);
- }
-
- auto settings = WebView::Settings::create(settings_path());
- EXPECT(!settings.user_agent_preset().has_value());
- EXPECT_EQ(settings.navigator_compatibility_mode(), Web::NavigatorCompatibilityMode::WebKit);
- remove_settings_file();
-}
-
-TEST_CASE(saved_user_agent_preset_names_are_normalized)
-{
- write_settings_file(R"({ "userAgentPreset": "safari ios mobile" })"sv);
-
- auto settings = WebView::Settings::create(settings_path());
- EXPECT_EQ(settings.user_agent_preset(), "Safari iOS Mobile"sv);
- remove_settings_file();
-}
-
-TEST_CASE(unknown_saved_spoofing_values_fall_back_to_defaults)
-{
- write_settings_file(R"({ "userAgentPreset": "Netscape Navigator", "navigatorCompatibilityMode": "trident" })"sv);
-
- auto settings = WebView::Settings::create(settings_path());
- EXPECT(!settings.user_agent_preset().has_value());
- EXPECT_EQ(settings.navigator_compatibility_mode(), Web::NavigatorCompatibilityMode::Chrome);
- remove_settings_file();
-}