From 46e1a08742c36cc51f9114311bc566fec432c9f1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 21 May 2026 16:36:25 +0200 Subject: [PATCH] LibWeb: Rename ContentFilter to ContentBlocker Rename the local content blocking implementation and its tests from ContentFilter to ContentBlocker while keeping the existing substring matcher backend and behavior. Update the WebContent IPC method, WebView option names, debug toggle, and default config file name to use content blocker terminology. --- ...Filters.txt => BrowserContentBlockers.txt} | 0 Libraries/LibWeb/CMakeLists.txt | 2 +- .../{ContentFilter.cpp => ContentBlocker.cpp} | 18 +-- .../{ContentFilter.h => ContentBlocker.h} | 8 +- Libraries/LibWeb/Loader/ResourceLoader.cpp | 8 +- Libraries/LibWebView/Application.cpp | 14 +- Libraries/LibWebView/Application.h | 2 +- Libraries/LibWebView/Menu.h | 2 +- Libraries/LibWebView/Options.h | 4 +- Services/WebContent/ConnectionFromClient.cpp | 10 +- Services/WebContent/ConnectionFromClient.h | 2 +- Services/WebContent/WebContentServer.ipc | 2 +- Services/WebContent/main.cpp | 24 ++-- Tests/LibWeb/CMakeLists.txt | 4 +- Tests/LibWeb/TestContentBlocker.cpp | 126 ++++++++++++++++++ Tests/LibWeb/TestContentFilter.cpp | 126 ------------------ Tests/LibWeb/test-web/TestWebView.cpp | 4 +- Tests/LibWeb/test-web/TestWebView.h | 2 +- Tests/LibWeb/test-web/main.cpp | 2 +- UI/Android/src/main/cpp/WebContentService.cpp | 24 ++-- UI/cmake/ResourceFiles.cmake | 2 +- 21 files changed, 193 insertions(+), 193 deletions(-) rename Base/res/ladybird/default-config/{BrowserContentFilters.txt => BrowserContentBlockers.txt} (100%) rename Libraries/LibWeb/Loader/{ContentFilter.cpp => ContentBlocker.cpp} (92%) rename Libraries/LibWeb/Loader/{ContentFilter.h => ContentBlocker.h} (91%) create mode 100644 Tests/LibWeb/TestContentBlocker.cpp delete mode 100644 Tests/LibWeb/TestContentFilter.cpp diff --git a/Base/res/ladybird/default-config/BrowserContentFilters.txt b/Base/res/ladybird/default-config/BrowserContentBlockers.txt similarity index 100% rename from Base/res/ladybird/default-config/BrowserContentFilters.txt rename to Base/res/ladybird/default-config/BrowserContentBlockers.txt diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 54b3c05fc6..2d931265b0 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -807,7 +807,7 @@ set(SOURCES Layout/TreeBuilder.cpp Layout/VideoBox.cpp Layout/Viewport.cpp - Loader/ContentFilter.cpp + Loader/ContentBlocker.cpp Loader/FileRequest.cpp Loader/GeneratedPagesLoader.cpp Loader/ProxyMappings.cpp diff --git a/Libraries/LibWeb/Loader/ContentFilter.cpp b/Libraries/LibWeb/Loader/ContentBlocker.cpp similarity index 92% rename from Libraries/LibWeb/Loader/ContentFilter.cpp rename to Libraries/LibWeb/Loader/ContentBlocker.cpp index c7f3136da4..96fe39520d 100644 --- a/Libraries/LibWeb/Loader/ContentFilter.cpp +++ b/Libraries/LibWeb/Loader/ContentBlocker.cpp @@ -9,21 +9,21 @@ #include #include #include -#include +#include namespace Web { -ContentFilter& ContentFilter::the() +ContentBlocker& ContentBlocker::the() { - static ContentFilter filter; - return filter; + static ContentBlocker blocker; + return blocker; } -ContentFilter::ContentFilter() = default; +ContentBlocker::ContentBlocker() = default; -ContentFilter::~ContentFilter() = default; +ContentBlocker::~ContentBlocker() = default; -bool ContentFilter::is_filtered(URL::URL const& url) const +bool ContentBlocker::is_filtered(URL::URL const& url) const { if (!filtering_enabled()) return false; @@ -33,14 +33,14 @@ bool ContentFilter::is_filtered(URL::URL const& url) const return contains(url.to_string()); } -bool ContentFilter::contains(StringView text) const +bool ContentBlocker::contains(StringView text) const { if (!m_matcher) return false; return m_matcher->contains(text); } -ErrorOr ContentFilter::set_patterns(ReadonlySpan patterns) +ErrorOr ContentBlocker::set_patterns(ReadonlySpan patterns) { m_matcher = make(patterns); return {}; diff --git a/Libraries/LibWeb/Loader/ContentFilter.h b/Libraries/LibWeb/Loader/ContentBlocker.h similarity index 91% rename from Libraries/LibWeb/Loader/ContentFilter.h rename to Libraries/LibWeb/Loader/ContentBlocker.h index dcb151c250..3a5abad6c5 100644 --- a/Libraries/LibWeb/Loader/ContentFilter.h +++ b/Libraries/LibWeb/Loader/ContentBlocker.h @@ -37,9 +37,9 @@ private: Vector m_transitions; }; -class WEB_API ContentFilter { +class WEB_API ContentBlocker { public: - static ContentFilter& the(); + static ContentBlocker& the(); bool filtering_enabled() const { return m_filtering_enabled; } void set_filtering_enabled(bool const enabled) { m_filtering_enabled = enabled; } @@ -48,8 +48,8 @@ public: ErrorOr set_patterns(ReadonlySpan); private: - ContentFilter(); - ~ContentFilter(); + ContentBlocker(); + ~ContentBlocker(); bool contains(StringView text) const; diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cpp index d091f58faa..16f65130e0 100644 --- a/Libraries/LibWeb/Loader/ResourceLoader.cpp +++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include #include @@ -77,7 +77,7 @@ void ResourceLoader::prefetch_dns(URL::URL const& url) if (url.scheme().is_one_of("file"sv, "data"sv)) return; - if (ContentFilter::the().is_filtered(url)) { + if (ContentBlocker::the().is_filtered(url)) { dbgln("ResourceLoader: Refusing to prefetch DNS for '{}': \033[31;1mURL was filtered\033[0m", url); return; } @@ -92,7 +92,7 @@ void ResourceLoader::preconnect(URL::URL const& url) if (url.scheme().is_one_of("file"sv, "data"sv)) return; - if (ContentFilter::the().is_filtered(url)) { + if (ContentBlocker::the().is_filtered(url)) { dbgln("ResourceLoader: Refusing to pre-connect to '{}': \033[31;1mURL was filtered\033[0m", url); return; } @@ -189,7 +189,7 @@ static bool should_block_request(LoadRequest const& request) return true; } - if (ContentFilter::the().is_filtered(url)) { + if (ContentBlocker::the().is_filtered(url)) { log_filtered_request(request); return true; } diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp index 7eee3235d2..79a24f6387 100644 --- a/Libraries/LibWebView/Application.cpp +++ b/Libraries/LibWebView/Application.cpp @@ -160,7 +160,7 @@ ErrorOr Application::initialize(Main::Arguments const& arguments) bool enable_idl_tracing = false; bool disable_http_memory_cache = false; bool disable_http_disk_cache = false; - bool disable_content_filter = false; + bool disable_content_blocker = false; Optional resource_substitution_map_path; bool enable_autoplay = false; bool expose_experimental_interfaces = false; @@ -233,7 +233,7 @@ ErrorOr Application::initialize(Main::Arguments const& arguments) args_parser.add_option(enable_idl_tracing, "Enable IDL tracing", "enable-idl-tracing"); args_parser.add_option(disable_http_memory_cache, "Disable HTTP memory cache", "disable-http-memory-cache"); args_parser.add_option(disable_http_disk_cache, "Disable HTTP disk cache", "disable-http-disk-cache"); - args_parser.add_option(disable_content_filter, "Disable content filter", "disable-content-filter"); + args_parser.add_option(disable_content_blocker, "Disable content blocker", "disable-content-blocker"); args_parser.add_option(enable_autoplay, "Enable multimedia autoplay", "enable-autoplay"); args_parser.add_option(expose_experimental_interfaces, "Expose experimental IDL interfaces", "expose-experimental-interfaces"); args_parser.add_option(expose_internals_object, "Expose internals object", "expose-internals-object"); @@ -330,7 +330,7 @@ ErrorOr Application::initialize(Main::Arguments const& arguments) : DNSSettings(DNSOverUDP(dns_server_address.release_value(), *dns_server_port, validate_dnssec_locally)) } : OptionalNone()), .devtools_port = devtools_port, - .enable_content_filter = disable_content_filter ? EnableContentFilter::No : EnableContentFilter::Yes, + .enable_content_blocker = disable_content_blocker ? EnableContentBlocker::No : EnableContentBlocker::Yes, }; if (screenshot_delay.has_value()) @@ -1224,9 +1224,9 @@ void Application::initialize_actions() m_enable_scripting_action->set_checked(m_browser_options.disable_scripting == WebView::DisableScripting::No); m_debug_menu->add_action(*m_enable_scripting_action); - m_enable_content_filtering_action = Action::create_checkable("Enable Content Filtering"sv, ActionID::EnableContentFiltering, check(m_enable_content_filtering_action, "content-filtering"sv)); - m_enable_content_filtering_action->set_checked(m_browser_options.enable_content_filter == WebView::EnableContentFilter::Yes); - m_debug_menu->add_action(*m_enable_content_filtering_action); + m_enable_content_blocking_action = Action::create_checkable("Enable Content Blocking"sv, ActionID::EnableContentBlocking, check(m_enable_content_blocking_action, "content-blocking"sv)); + m_enable_content_blocking_action->set_checked(m_browser_options.enable_content_blocker == WebView::EnableContentBlocker::Yes); + m_debug_menu->add_action(*m_enable_content_blocking_action); m_block_pop_ups_action = Action::create_checkable("Block Pop-ups"sv, ActionID::BlockPopUps, check(m_block_pop_ups_action, "block-pop-ups"sv)); m_block_pop_ups_action->set_checked(m_browser_options.allow_popups == AllowPopups::No); @@ -1241,7 +1241,7 @@ void Application::apply_view_options(Badge, ViewImplementati view.debug_request("set-line-box-borders"sv, m_show_line_box_borders_action->checked() ? "on"sv : "off"sv); view.debug_request("scripting"sv, m_enable_scripting_action->checked() ? "on"sv : "off"sv); - view.debug_request("content-filtering"sv, m_enable_content_filtering_action->checked() ? "on"sv : "off"sv); + view.debug_request("content-blocking"sv, m_enable_content_blocking_action->checked() ? "on"sv : "off"sv); view.debug_request("block-pop-ups"sv, m_block_pop_ups_action->checked() ? "on"sv : "off"sv); view.debug_request("spoof-user-agent"sv, m_user_agent_string); view.debug_request("navigator-compatibility-mode"sv, m_navigator_compatibility_mode); diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h index 60ef6ebc77..c81543bfea 100644 --- a/Libraries/LibWebView/Application.h +++ b/Libraries/LibWebView/Application.h @@ -325,7 +325,7 @@ private: RefPtr m_debug_menu; RefPtr m_show_line_box_borders_action; RefPtr m_enable_scripting_action; - RefPtr m_enable_content_filtering_action; + RefPtr m_enable_content_blocking_action; RefPtr m_block_pop_ups_action; StringView m_user_agent_string; StringView m_navigator_compatibility_mode; diff --git a/Libraries/LibWebView/Menu.h b/Libraries/LibWebView/Menu.h index 7d3299ccc3..ab6397cdb6 100644 --- a/Libraries/LibWebView/Menu.h +++ b/Libraries/LibWebView/Menu.h @@ -103,7 +103,7 @@ enum class ActionID { SpoofUserAgent, NavigatorCompatibilityMode, EnableScripting, - EnableContentFiltering, + EnableContentBlocking, BlockPopUps, }; diff --git a/Libraries/LibWebView/Options.h b/Libraries/LibWebView/Options.h index 4762d5b3ef..12577eca27 100644 --- a/Libraries/LibWebView/Options.h +++ b/Libraries/LibWebView/Options.h @@ -69,7 +69,7 @@ using DNSSettings = Variant; constexpr inline u16 default_devtools_port = 6000; -enum class EnableContentFilter { +enum class EnableContentBlocker { No, Yes, }; @@ -91,7 +91,7 @@ struct BrowserOptions { Optional webdriver_endpoint {}; Optional dns_settings {}; Optional devtools_port; - EnableContentFilter enable_content_filter { EnableContentFilter::Yes }; + EnableContentBlocker enable_content_blocker { EnableContentBlocker::Yes }; }; enum class HTTPDiskCacheMode { diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp index ccdcfe1c84..879efe9e93 100644 --- a/Services/WebContent/ConnectionFromClient.cpp +++ b/Services/WebContent/ConnectionFromClient.cpp @@ -49,7 +49,7 @@ #include #include #include -#include +#include #include #include #include @@ -457,8 +457,8 @@ void ConnectionFromClient::debug_request(u64 page_id, ByteString request, ByteSt return; } - if (request == "content-filtering") { - Web::ContentFilter::the().set_filtering_enabled(argument == "on"); + if (request == "content-blocking") { + Web::ContentBlocker::the().set_filtering_enabled(argument == "on"); return; } } @@ -1144,9 +1144,9 @@ void ConnectionFromClient::paste(u64 page_id, Utf16String text) page->page().focused_navigable().paste(text); } -void ConnectionFromClient::set_content_filters(u64, Vector filters) +void ConnectionFromClient::set_content_blockers(u64, Vector patterns) { - Web::ContentFilter::the().set_patterns(filters).release_value_but_fixme_should_propagate_errors(); + Web::ContentBlocker::the().set_patterns(patterns).release_value_but_fixme_should_propagate_errors(); } void ConnectionFromClient::set_autoplay_allowed_on_all_websites(u64) diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.h index 089abae299..bfdff71b8e 100644 --- a/Services/WebContent/ConnectionFromClient.h +++ b/Services/WebContent/ConnectionFromClient.h @@ -107,7 +107,7 @@ private: virtual void clone_dom_node(u64 page_id, Web::UniqueNodeID node_id) override; virtual void remove_dom_node(u64 page_id, Web::UniqueNodeID node_id) override; - virtual void set_content_filters(u64 page_id, Vector) override; + virtual void set_content_blockers(u64 page_id, Vector patterns) override; virtual void set_autoplay_allowed_on_all_websites(u64 page_id) override; virtual void set_autoplay_allowlist(u64 page_id, Vector allowlist) override; virtual void set_proxy_mappings(u64 page_id, Vector, HashMap) override; diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipc index e5493e9470..99f78fd4a5 100644 --- a/Services/WebContent/WebContentServer.ipc +++ b/Services/WebContent/WebContentServer.ipc @@ -97,7 +97,7 @@ endpoint WebContentServer find_in_page_next_match(u64 page_id) =| find_in_page_previous_match(u64 page_id) =| - set_content_filters(u64 page_id, Vector filters) =| + set_content_blockers(u64 page_id, Vector patterns) =| set_autoplay_allowed_on_all_websites(u64 page_id) =| set_autoplay_allowlist(u64 page_id, Vector allowlist) =| set_proxy_mappings(u64 page_id, Vector proxies, HashMap mappings) =| diff --git a/Services/WebContent/main.cpp b/Services/WebContent/main.cpp index b7d65d0dc4..4508795aba 100644 --- a/Services/WebContent/main.cpp +++ b/Services/WebContent/main.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include #include @@ -100,7 +100,7 @@ static void install_crash_signal_handlers() } #endif -static ErrorOr load_content_filters(StringView config_path); +static ErrorOr load_content_blockers(StringView config_path); static ErrorOr connect_to_resource_loader(GC::Heap& heap, IPC::TransportHandle const& handle); static ErrorOr connect_to_image_decoder(IPC::TransportHandle const& handle); @@ -251,9 +251,9 @@ ErrorOr ladybird_main(Main::Arguments arguments) Web::WebIDL::set_enable_idl_tracing(true); } - auto maybe_content_filter_error = load_content_filters(config_path); - if (maybe_content_filter_error.is_error()) - dbgln("Failed to load content filters: {}", maybe_content_filter_error.error()); + auto maybe_content_blocker_error = load_content_blockers(config_path); + if (maybe_content_blocker_error.is_error()) + dbgln("Failed to load content blockers: {}", maybe_content_blocker_error.error()); #if defined(AK_OS_MACOS) auto browser_port = TRY(Core::MachPort::look_up_from_bootstrap_server(ByteString { mach_server_name })); @@ -277,17 +277,17 @@ ErrorOr ladybird_main(Main::Arguments arguments) return event_loop.exec(); } -static ErrorOr load_content_filters(StringView config_path) +static ErrorOr load_content_blockers(StringView config_path) { auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); - auto file = TRY(Core::File::open(ByteString::formatted("{}/BrowserContentFilters.txt", config_path), Core::File::OpenMode::Read)); - auto ad_filter_list = TRY(Core::InputBufferedFile::create(move(file))); + auto file = TRY(Core::File::open(ByteString::formatted("{}/BrowserContentBlockers.txt", config_path), Core::File::OpenMode::Read)); + auto content_blocker_list = TRY(Core::InputBufferedFile::create(move(file))); Vector patterns; - while (TRY(ad_filter_list->can_read_line())) { - auto line = TRY(ad_filter_list->read_line(buffer)); + while (TRY(content_blocker_list->can_read_line())) { + auto line = TRY(content_blocker_list->read_line(buffer)); if (line.is_empty()) continue; @@ -295,8 +295,8 @@ static ErrorOr load_content_filters(StringView config_path) TRY(patterns.try_append(move(pattern))); } - auto& content_filter = Web::ContentFilter::the(); - TRY(content_filter.set_patterns(patterns)); + auto& content_blocker = Web::ContentBlocker::the(); + TRY(content_blocker.set_patterns(patterns)); return {}; } diff --git a/Tests/LibWeb/CMakeLists.txt b/Tests/LibWeb/CMakeLists.txt index c57cb9611a..7d4c8eafc2 100644 --- a/Tests/LibWeb/CMakeLists.txt +++ b/Tests/LibWeb/CMakeLists.txt @@ -1,6 +1,6 @@ set(TEST_SOURCES TestCSSIDSpeed.cpp - TestContentFilter.cpp + TestContentBlocker.cpp TestControlMessageQueue.cpp TestCSSInheritedProperty.cpp TestCSSPixels.cpp @@ -24,7 +24,7 @@ endforeach() ladybird_utility(css-tokenizer SOURCES css-tokenizer.cpp LIBS LibFileSystem LibMain LibWeb) -target_link_libraries(TestContentFilter PRIVATE LibURL) +target_link_libraries(TestContentBlocker PRIVATE LibURL) target_link_libraries(TestControlMessageQueue PRIVATE LibSync) target_link_libraries(TestFetchURL PRIVATE LibURL) target_link_libraries(TestSourceHighlighter PRIVATE LibURL LibWebView) diff --git a/Tests/LibWeb/TestContentBlocker.cpp b/Tests/LibWeb/TestContentBlocker.cpp new file mode 100644 index 0000000000..6b5767ef2a --- /dev/null +++ b/Tests/LibWeb/TestContentBlocker.cpp @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2025, Tim Ledbetter + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +namespace Web { + +static ContentBlocker& make_blocker(Vector patterns) +{ + auto& blocker = ContentBlocker::the(); + MUST(blocker.set_patterns(patterns)); + return blocker; +} + +static URL::URL url(StringView string) +{ + auto result = URL::Parser::basic_parse(string); + EXPECT(result.has_value()); + return result.release_value(); +} + +TEST_CASE(empty_pattern_list) +{ + auto& blocker = make_blocker({}); + + EXPECT(!blocker.is_filtered(url("https://anything.com"sv))); + EXPECT(!blocker.is_filtered(url("data:text/plain,hi"sv))); +} + +TEST_CASE(basic_blocking) +{ + Vector patterns = { + "ads."_string, + "?banner"_string, + "tracker"_string + }; + + auto& blocker = make_blocker(move(patterns)); + + EXPECT(blocker.is_filtered(url("https://example.com/ads.js"sv))); + EXPECT(blocker.is_filtered(url("http://site.com/page.html?banner=true"sv))); + EXPECT(blocker.is_filtered(url("https://tracker.example.org/ping"sv))); + EXPECT(!blocker.is_filtered(url("https://ds.example.com/page.html"sv))); +} + +TEST_CASE(data_urls_exempt) +{ + Vector patterns = { + { "data:"_string }, + { "evil.com"_string } + }; + + auto& blocker = make_blocker(move(patterns)); + + EXPECT(!blocker.is_filtered(url("data:text/plain,hello"sv))); + EXPECT(!blocker.is_filtered(url("data:image/png;base64,abc123"sv))); + EXPECT(blocker.is_filtered(url("https://evil.com/script.js"sv))); +} + +TEST_CASE(disable_filtering) +{ + Vector patterns = { + { "example.com"_string } + }; + + auto& blocker = make_blocker(move(patterns)); + blocker.set_filtering_enabled(false); + + EXPECT(!blocker.is_filtered(url("https://example.com"sv))); + EXPECT(!blocker.is_filtered(url("http://example.com/ads"sv))); + + blocker.set_filtering_enabled(true); + EXPECT(blocker.is_filtered(url("https://example.com"sv))); +} + +TEST_CASE(substring_matches) +{ + Vector patterns = { + { "ads"_string }, + { "ad/"_string } + }; + + auto& blocker = make_blocker(move(patterns)); + + EXPECT(blocker.is_filtered(url("https://site.com/ads/banner.jpg"sv))); + EXPECT(blocker.is_filtered(url("http://marketing.com/ad/page"sv))); + EXPECT(!blocker.is_filtered(url("https://site.com/content/article.html"sv))); + EXPECT(!blocker.is_filtered(url("http://advancedtech.com/home"sv))); +} + +TEST_CASE(file_scheme_can_be_filtered) +{ + Vector patterns = { + { "secret"_string }, + { ".txt"_string } + }; + + auto& blocker = make_blocker(move(patterns)); + + EXPECT(blocker.is_filtered(url("file:///home/user/secret.txt"sv))); + EXPECT(!blocker.is_filtered(url("file:///home/user/document.pdf"sv))); +} + +TEST_CASE(query_parameters_and_fragments) +{ + Vector patterns = { + { "#ad="_string }, + { "?ad="_string }, + { "#sponsored"_string } + }; + + auto& blocker = make_blocker(move(patterns)); + + EXPECT(blocker.is_filtered(url("https://site.com/page?ad=123"sv))); + EXPECT(blocker.is_filtered(url("https://site.com/page#ad=456"sv))); + EXPECT(blocker.is_filtered(url("https://site.com/page?ref=home&ad=1#sponsored"sv))); + EXPECT(!blocker.is_filtered(url("https://site.com/page?ref=home"sv))); +} + +} diff --git a/Tests/LibWeb/TestContentFilter.cpp b/Tests/LibWeb/TestContentFilter.cpp deleted file mode 100644 index d864ff71a4..0000000000 --- a/Tests/LibWeb/TestContentFilter.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (c) 2025, Tim Ledbetter - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include -#include -#include - -namespace Web { - -static ContentFilter& make_filter(Vector patterns) -{ - auto& filter = ContentFilter::the(); - MUST(filter.set_patterns(patterns)); - return filter; -} - -static URL::URL url(StringView string) -{ - auto result = URL::Parser::basic_parse(string); - EXPECT(result.has_value()); - return result.release_value(); -} - -TEST_CASE(empty_pattern_list) -{ - auto& filter = make_filter({}); - - EXPECT(!filter.is_filtered(url("https://anything.com"sv))); - EXPECT(!filter.is_filtered(url("data:text/plain,hi"sv))); -} - -TEST_CASE(basic_blocking) -{ - Vector patterns = { - "ads."_string, - "?banner"_string, - "tracker"_string - }; - - auto& filter = make_filter(move(patterns)); - - EXPECT(filter.is_filtered(url("https://example.com/ads.js"sv))); - EXPECT(filter.is_filtered(url("http://site.com/page.html?banner=true"sv))); - EXPECT(filter.is_filtered(url("https://tracker.example.org/ping"sv))); - EXPECT(!filter.is_filtered(url("https://ds.example.com/page.html"sv))); -} - -TEST_CASE(data_urls_exempt) -{ - Vector patterns = { - { "data:"_string }, - { "evil.com"_string } - }; - - auto& filter = make_filter(move(patterns)); - - EXPECT(!filter.is_filtered(url("data:text/plain,hello"sv))); - EXPECT(!filter.is_filtered(url("data:image/png;base64,abc123"sv))); - EXPECT(filter.is_filtered(url("https://evil.com/script.js"sv))); -} - -TEST_CASE(disable_filtering) -{ - Vector patterns = { - { "example.com"_string } - }; - - auto& filter = make_filter(move(patterns)); - filter.set_filtering_enabled(false); - - EXPECT(!filter.is_filtered(url("https://example.com"sv))); - EXPECT(!filter.is_filtered(url("http://example.com/ads"sv))); - - filter.set_filtering_enabled(true); - EXPECT(filter.is_filtered(url("https://example.com"sv))); -} - -TEST_CASE(substring_matches) -{ - Vector patterns = { - { "ads"_string }, - { "ad/"_string } - }; - - auto& filter = make_filter(move(patterns)); - - EXPECT(filter.is_filtered(url("https://site.com/ads/banner.jpg"sv))); - EXPECT(filter.is_filtered(url("http://marketing.com/ad/page"sv))); - EXPECT(!filter.is_filtered(url("https://site.com/content/article.html"sv))); - EXPECT(!filter.is_filtered(url("http://advancedtech.com/home"sv))); -} - -TEST_CASE(file_scheme_can_be_filtered) -{ - Vector patterns = { - { "secret"_string }, - { ".txt"_string } - }; - - auto& filter = make_filter(move(patterns)); - - EXPECT(filter.is_filtered(url("file:///home/user/secret.txt"sv))); - EXPECT(!filter.is_filtered(url("file:///home/user/document.pdf"sv))); -} - -TEST_CASE(query_parameters_and_fragments) -{ - Vector patterns = { - { "#ad="_string }, - { "?ad="_string }, - { "#sponsored"_string } - }; - - auto& filter = make_filter(move(patterns)); - - EXPECT(filter.is_filtered(url("https://site.com/page?ad=123"sv))); - EXPECT(filter.is_filtered(url("https://site.com/page#ad=456"sv))); - EXPECT(filter.is_filtered(url("https://site.com/page?ref=home&ad=1#sponsored"sv))); - EXPECT(!filter.is_filtered(url("https://site.com/page?ref=home"sv))); -} - -} diff --git a/Tests/LibWeb/test-web/TestWebView.cpp b/Tests/LibWeb/test-web/TestWebView.cpp index 4ae1a48984..ccaa969617 100644 --- a/Tests/LibWeb/test-web/TestWebView.cpp +++ b/Tests/LibWeb/test-web/TestWebView.cpp @@ -25,9 +25,9 @@ TestWebView::TestWebView(Core::AnonymousBuffer theme, Web::DevicePixelSize viewp { } -void TestWebView::clear_content_filters() +void TestWebView::clear_content_blockers() { - client().async_set_content_filters(m_client_state.page_index, {}); + client().async_set_content_blockers(m_client_state.page_index, {}); } pid_t TestWebView::web_content_pid() const diff --git a/Tests/LibWeb/test-web/TestWebView.h b/Tests/LibWeb/test-web/TestWebView.h index 7c5d6b747a..a91661b982 100644 --- a/Tests/LibWeb/test-web/TestWebView.h +++ b/Tests/LibWeb/test-web/TestWebView.h @@ -22,7 +22,7 @@ class TestWebView final : public WebView::HeadlessWebView { public: static NonnullOwnPtr create(Core::AnonymousBuffer theme, Web::DevicePixelSize window_size); - void clear_content_filters(); + void clear_content_blockers(); pid_t web_content_pid() const; NonnullRefPtr>> take_screenshot(); diff --git a/Tests/LibWeb/test-web/main.cpp b/Tests/LibWeb/test-web/main.cpp index d886872821..9507989b8c 100644 --- a/Tests/LibWeb/test-web/main.cpp +++ b/Tests/LibWeb/test-web/main.cpp @@ -1291,7 +1291,7 @@ static ErrorOr run_tests(Core::AnonymousBuffer const& theme, Web::DevicePix for (auto [view_id, view] : enumerate(views)) { set_ui_callbacks_for_tests(*view, test_run_capture); - view->clear_content_filters(); + view->clear_content_blockers(); auto cleanup_test = [&, view = view.ptr()](size_t test_index, TestResult test_result) { view->on_load_finish = {}; diff --git a/UI/Android/src/main/cpp/WebContentService.cpp b/UI/Android/src/main/cpp/WebContentService.cpp index eac7484a61..a6c2de8e30 100644 --- a/UI/Android/src/main/cpp/WebContentService.cpp +++ b/UI/Android/src/main/cpp/WebContentService.cpp @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include #include @@ -40,7 +40,7 @@ static ErrorOr> bind_image_decoder_ser return bind_service(&bind_image_decoder_java); } -static ErrorOr load_content_filters(); +static ErrorOr load_content_blockers(); static ErrorOr load_autoplay_allowlist(); @@ -68,9 +68,9 @@ ErrorOr service_main(int ipc_socket) // in order to make it work. For now, it's better to just disable it. WebView::disable_site_isolation(); - auto maybe_content_filter_error = load_content_filters(); - if (maybe_content_filter_error.is_error()) - dbgln("Failed to load content filters: {}", maybe_content_filter_error.error()); + auto maybe_content_blocker_error = load_content_blockers(); + if (maybe_content_blocker_error.is_error()) + dbgln("Failed to load content blockers: {}", maybe_content_blocker_error.error()); auto maybe_autoplay_allowlist_error = load_autoplay_allowlist(); if (maybe_autoplay_allowlist_error.is_error()) @@ -102,20 +102,20 @@ ErrorOr> bind_service(void (*bind_method)(int)) return new_client; } -static ErrorOr load_content_filters() +static ErrorOr load_content_blockers() { - auto file_or_error = Core::File::open(ByteString::formatted("{}/res/ladybird/default-config/BrowserContentFilters.txt", WebView::s_ladybird_resource_root), Core::File::OpenMode::Read); + auto file_or_error = Core::File::open(ByteString::formatted("{}/res/ladybird/default-config/BrowserContentBlockers.txt", WebView::s_ladybird_resource_root), Core::File::OpenMode::Read); if (file_or_error.is_error()) return file_or_error.release_error(); auto file = file_or_error.release_value(); - auto ad_filter_list = TRY(Core::InputBufferedFile::create(move(file))); + auto content_blocker_list = TRY(Core::InputBufferedFile::create(move(file))); auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); Vector patterns; - while (TRY(ad_filter_list->can_read_line())) { - auto line = TRY(ad_filter_list->read_line(buffer)); + while (TRY(content_blocker_list->can_read_line())) { + auto line = TRY(content_blocker_list->read_line(buffer)); if (line.is_empty()) continue; @@ -123,8 +123,8 @@ static ErrorOr load_content_filters() TRY(patterns.try_append(move(pattern))); } - auto& content_filter = Web::ContentFilter::the(); - TRY(content_filter.set_patterns(patterns)); + auto& content_blocker = Web::ContentBlocker::the(); + TRY(content_blocker.set_patterns(patterns)); return {}; } diff --git a/UI/cmake/ResourceFiles.cmake b/UI/cmake/ResourceFiles.cmake index 11e56fb899..32c5f716b9 100644 --- a/UI/cmake/ResourceFiles.cmake +++ b/UI/cmake/ResourceFiles.cmake @@ -105,7 +105,7 @@ set(THEMES list(TRANSFORM THEMES PREPEND "${LADYBIRD_SOURCE_DIR}/Base/res/themes/") set(CONFIG_RESOURCES - BrowserContentFilters.txt + BrowserContentBlockers.txt ) list(TRANSFORM CONFIG_RESOURCES PREPEND "${LADYBIRD_SOURCE_DIR}/Base/res/ladybird/default-config/")