Truly independent web browser
LibWeb+WebContent+LibWebView: Scope content blocking to pages
Timothy Flynn0c185fb0c643
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex ed283da3e43..12c765584f9 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -974,7 +974,7 @@ void Document::visit_edges(Cell::Visitor& visitor)Utf16String const& Document::content_blocker_style_sheet(){- if (is_decoded_svg()) {+ if (is_decoded_svg() || !page().content_blocking_enabled()) {if (!m_content_blocker_style_sheet.has_value())m_content_blocker_style_sheet = Utf16String {};return m_content_blocker_style_sheet.value();@@ -1019,7 +1019,7 @@ void Document::invalidate_content_blocker_style_sheet()bool Document::content_blocker_style_sheet_may_need_refresh_for_class_or_id(Utf16FlyString const* id, ReadonlySpan<Utf16FlyString> class_names){- if (is_decoded_svg())+ if (is_decoded_svg() || !page().content_blocking_enabled())return false;if (!m_content_blocker_style_sheet.has_value())diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex 50f0d6769e5..e57c74e41af 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -283,7 +283,7 @@ static void invalidate_content_blocker_style_if_needed(Element& element){if (!element.is_connected())return;- if (!ContentBlocker::the().filtering_enabled() || !ContentBlocker::the().has_cosmetic_rules())+ if (!element.document().page().content_blocking_enabled() || !ContentBlocker::the().has_cosmetic_rules())return;auto const& id = element.id();diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cppindex d03ea95072e..3a009234374 100644--- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp@@ -565,7 +565,7 @@ void HTMLLinkElement::fetch_and_process_linked_dns_prefetch_resource()// FIXME: This should go through Fetch: https://fetch.spec.whatwg.org/#resolve-an-originif (!ResourceLoader::is_initialized())return;- ResourceLoader::the().prefetch_dns(url.value(), document().fallback_base_url());+ ResourceLoader::the().prefetch_dns(document().page(), url.value(), document().fallback_base_url());}// https://html.spec.whatwg.org/multipage/links.html#link-type-preconnect:fetch-and-process-the-linked-resource-2@@ -821,8 +821,10 @@ void HTMLLinkElement::preconnect(LinkProcessingOptions const& options)// FIXME: This should go through Fetch: https://fetch.spec.whatwg.org/#concept-connection-obtainif (!ResourceLoader::is_initialized())return;++ GC::Ptr<Page> page = options.document ? &options.document->page() : nullptr;auto source_url = options.document ? options.document->fallback_base_url() : options.environment->api_base_url();- ResourceLoader::the().preconnect(*url, source_url);+ ResourceLoader::the().preconnect(page, *url, source_url);}// https://html.spec.whatwg.org/multipage/links.html#match-preload-typediff --git a/Libraries/LibWeb/Loader/ContentBlocker.cpp b/Libraries/LibWeb/Loader/ContentBlocker.cppindex f182faa9054..b8fae59b073 100644--- a/Libraries/LibWeb/Loader/ContentBlocker.cpp+++ b/Libraries/LibWeb/Loader/ContentBlocker.cpp@@ -187,7 +187,7 @@ bool ContentBlocker::is_filtered(URL::URL const& url, URL::URL const& source_urlbool ContentBlocker::is_filtered(URL::URL const& url, URL::URL const& source_url, ResourceType resource_type) const{- if (!filtering_enabled() || !m_engine)+ if (!m_engine)return false;if (url.scheme() == "data"sv)@@ -215,7 +215,7 @@ Utf16String ContentBlocker::cosmetic_style_sheet_for_url(URL::URL const& url) coUtf16String ContentBlocker::cosmetic_style_sheet_for_url(URL::URL const& url, ReadonlySpan<Utf16FlyString> classes, ReadonlySpan<Utf16FlyString> ids) const{- if (!filtering_enabled() || !m_engine || !m_has_cosmetic_rules)+ if (!m_engine || !m_has_cosmetic_rules)return {};auto url_string = serialized_url(url);@@ -242,7 +242,7 @@ Utf16String ContentBlocker::cosmetic_style_sheet_for_url(URL::URL const& url, Rebool ContentBlocker::has_generic_cosmetic_selectors_for_url(URL::URL const& url, ReadonlySpan<Utf16FlyString> classes, ReadonlySpan<Utf16FlyString> ids) const{- if (!filtering_enabled() || !m_engine || !m_has_cosmetic_rules)+ if (!m_engine || !m_has_cosmetic_rules)return false;auto url_string = serialized_url(url);diff --git a/Libraries/LibWeb/Loader/ContentBlocker.h b/Libraries/LibWeb/Loader/ContentBlocker.hindex 729d890857c..be3aa176401 100644--- a/Libraries/LibWeb/Loader/ContentBlocker.h+++ b/Libraries/LibWeb/Loader/ContentBlocker.h@@ -45,8 +45,6 @@ public:bool has_rules() const { return m_engine != nullptr; }bool has_cosmetic_rules() const { return m_has_cosmetic_rules; }- bool filtering_enabled() const { return m_filtering_enabled; }- void set_filtering_enabled(bool const enabled) { m_filtering_enabled = enabled; }bool is_filtered(URL::URL const&) const;bool is_filtered(URL::URL const&, URL::URL const& source_url, ResourceType) const;@@ -65,7 +63,6 @@ private:ContentBlocker();~ContentBlocker();- bool m_filtering_enabled { true };bool m_has_cosmetic_rules { false };void* m_engine { nullptr };};diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cppindex 0d3a0433208..dfda48a5683 100644--- a/Libraries/LibWeb/Loader/ResourceLoader.cpp+++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp@@ -73,14 +73,16 @@ void ResourceLoader::set_client(NonnullRefPtr<Requests::RequestClient> request_c};}-void ResourceLoader::prefetch_dns(URL::URL const& url, URL::URL const& source_url)+void ResourceLoader::prefetch_dns(GC::Ptr<Page> page, URL::URL const& url, URL::URL const& source_url){if (url.scheme().is_one_of("file"sv, "data"sv))return;- if (ContentBlocker::the().is_filtered(url, source_url, ContentBlocker::ResourceType::Other)) {- dbgln("ResourceLoader: Refusing to prefetch DNS for '{}': \033[31;1mURL was filtered\033[0m", url);- return;+ if (!page || page->content_blocking_enabled()) {+ if (ContentBlocker::the().is_filtered(url, source_url, ContentBlocker::ResourceType::Other)) {+ dbgln("ResourceLoader: Refusing to prefetch DNS for '{}': \033[31;1mURL was filtered\033[0m", url);+ return;+ }}// FIXME: We could put this request in a queue until the client connection is re-established.@@ -88,14 +90,16 @@ void ResourceLoader::prefetch_dns(URL::URL const& url, URL::URL const& source_urm_request_client->ensure_connection(url, RequestServer::CacheLevel::ResolveOnly);}-void ResourceLoader::preconnect(URL::URL const& url, URL::URL const& source_url)+void ResourceLoader::preconnect(GC::Ptr<Page> page, URL::URL const& url, URL::URL const& source_url){if (url.scheme().is_one_of("file"sv, "data"sv))return;- if (ContentBlocker::the().is_filtered(url, source_url, ContentBlocker::ResourceType::Other)) {- dbgln("ResourceLoader: Refusing to pre-connect to '{}': \033[31;1mURL was filtered\033[0m", url);- return;+ if (!page || page->content_blocking_enabled()) {+ if (ContentBlocker::the().is_filtered(url, source_url, ContentBlocker::ResourceType::Other)) {+ dbgln("ResourceLoader: Refusing to pre-connect to '{}': \033[31;1mURL was filtered\033[0m", url);+ return;+ }}// FIXME: We could put this request in a queue until the client connection is re-established.@@ -186,10 +190,12 @@ static bool should_block_request(LoadRequest const& request)return true;}- auto source_url = request.source_url().value_or(url);- if (ContentBlocker::the().is_filtered(url, source_url, request.destination(), request.initiator_type(), request.request_mode())) {- log_filtered_request(request);- return true;+ if (auto page = request.page(); !page || page->content_blocking_enabled()) {+ auto source_url = request.source_url().value_or(url);+ if (ContentBlocker::the().is_filtered(url, source_url, request.destination(), request.initiator_type(), request.request_mode())) {+ log_filtered_request(request);+ return true;+ }}return false;diff --git a/Libraries/LibWeb/Loader/ResourceLoader.h b/Libraries/LibWeb/Loader/ResourceLoader.hindex 08f625db45b..1832df8da64 100644--- a/Libraries/LibWeb/Loader/ResourceLoader.h+++ b/Libraries/LibWeb/Loader/ResourceLoader.h@@ -44,8 +44,8 @@ public:RefPtr<Requests::RequestClient>& request_client() { return m_request_client; }- void prefetch_dns(URL::URL const&, URL::URL const& source_url);- void preconnect(URL::URL const&, URL::URL const& source_url);+ void prefetch_dns(GC::Ptr<Page>, URL::URL const&, URL::URL const& source_url);+ void preconnect(GC::Ptr<Page>, URL::URL const&, URL::URL const& source_url);static bool is_known_hsts_host(Page&, String const& host);diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cppindex b53ded832e3..cf94eb0baaf 100644--- a/Libraries/LibWeb/Page/Page.cpp+++ b/Libraries/LibWeb/Page/Page.cpp@@ -1712,18 +1712,6 @@ void Page::set_user_style(Utf16String source)invalidate_user_style();}-void Page::set_content_blocking_enabled(bool enabled)-{- auto& blocker = ContentBlocker::the();- if (blocker.filtering_enabled() == enabled)- return;-- auto has_cosmetic_rules = blocker.has_cosmetic_rules();- blocker.set_filtering_enabled(enabled);- if (has_cosmetic_rules)- invalidate_user_style();-}-void Page::invalidate_user_style(){if (!has_top_level_traversable())@@ -1762,6 +1750,16 @@ void Page::invalidate_style_for_preference_change()}}+void Page::set_content_blocking_enabled(bool enabled)+{+ if (m_content_blocking_enabled == enabled)+ return;+ m_content_blocking_enabled = enabled;++ if (ContentBlocker::the().has_cosmetic_rules())+ invalidate_user_style();+}+Vector<GC::Root<DOM::Document>> Page::documents_in_active_window() const{if (!has_local_traversable())diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.hindex 9aa2a313be1..3201dc248c2 100644--- a/Libraries/LibWeb/Page/Page.h+++ b/Libraries/LibWeb/Page/Page.h@@ -393,10 +393,12 @@ public:Optional<Utf16String> const& user_style() const { return m_user_style_sheet_source; }void set_user_style(Utf16String source);- void set_content_blocking_enabled(bool);void invalidate_user_style();void invalidate_style_for_preference_change();+ bool content_blocking_enabled() const { return m_content_blocking_enabled; }+ void set_content_blocking_enabled(bool);+bool pdf_viewer_supported() const { return m_pdf_viewer_supported; }void clear_selection();@@ -564,6 +566,8 @@ private:Optional<Utf16String> m_user_style_sheet_source;+ bool m_content_blocking_enabled { true };+// https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-supported// Each user agent has a PDF viewer supported boolean, whose value is implementation-defined (and might vary// according to user preferences).diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex d48c96771e9..fbd8f327735 100644--- a/Libraries/LibWebView/Application.cpp+++ b/Libraries/LibWebView/Application.cpp@@ -2718,7 +2718,7 @@ void Application::apply_view_options(Badge<ViewImplementation>, ViewImplementati// Test mode supplies its own filter lists independently of profile preferences.auto content_blocking_enabled = m_browser_options.enable_content_blocker == EnableContentBlocker::Yes&& (m_web_content_options.is_test_mode == IsTestMode::Yes || m_settings->content_blocker_enabled());- page.async_debug_request("content-blocking"sv, content_blocking_enabled ? "on"sv : "off"sv);+ page.async_set_content_blocking_enabled(content_blocking_enabled);if (m_content_blocker_list_buffer.has_value())page.client().async_set_content_blockers(*m_content_blocker_list_buffer);@@ -2764,30 +2764,30 @@ void Application::background_networking_settings_changed(Badge<ApplicationSettinvoid Application::content_blocker_settings_changed(Badge<ApplicationSettingsObserver>){- if (m_web_content_options.is_test_mode == IsTestMode::Yes)- return;-- auto enabled = m_browser_options.enable_content_blocker == EnableContentBlocker::Yes && m_settings->content_blocker_enabled();- ViewImplementation::for_each_view([enabled](ViewImplementation& view) {- view.debug_request("content-blocking"sv, enabled ? "on"sv : "off"sv);- return IterationDecision::Continue;- });-apply_content_blocker_settings();}void Application::apply_content_blocker_settings(){- if (m_web_content_options.is_test_mode == IsTestMode::Yes)- return;- rebuild_content_blocker_list_paths();- if (auto result = load_content_blocker_lists(); result.is_error()) {- warnln("Unable to apply content blocker settings: {}", result.error());- return;+ bool lists_loaded = false;+ if (m_web_content_options.is_test_mode == IsTestMode::No) {+ rebuild_content_blocker_list_paths();+ if (auto result = load_content_blocker_lists(); result.is_error())+ warnln("Unable to apply content blocker settings: {}", result.error());+ else+ lists_loaded = true;}+ auto enabled = m_browser_options.enable_content_blocker == EnableContentBlocker::Yes+ && (m_web_content_options.is_test_mode == IsTestMode::Yes || m_settings->content_blocker_enabled());+WebContentClient::for_each_client([&](WebContentClient& client) {- client.async_set_content_blockers(*m_content_blocker_list_buffer);+ client.for_each_page([&](WebContentPage& page) {+ client.async_set_content_blocking_enabled(page.id(), enabled);+ return IterationDecision::Continue;+ });+ if (lists_loaded)+ client.async_set_content_blockers(*m_content_blocker_list_buffer);return IterationDecision::Continue;});}diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cppindex 1e7af4c0b63..6a79d01835f 100644--- a/Services/WebContent/ConnectionFromClient.cpp+++ b/Services/WebContent/ConnectionFromClient.cpp@@ -1156,11 +1156,6 @@ void ConnectionFromClient::debug_request(Web::PageId page_id, ByteString requestWeb::ResourceLoader::the().set_navigator_compatibility_mode(compatibility_mode);return;}-- if (request == "content-blocking") {- page->page().set_content_blocking_enabled(argument == "on");- return;- }}void ConnectionFromClient::get_source(Web::PageId page_id)@@ -3078,6 +3073,12 @@ void ConnectionFromClient::update_input_method_state(Web::PageId page_id)async_did_update_input_method_state(page_id, caret_rect, is_enabled, cursor_position, anchor_position, move(text_before_cursor), move(text_after_cursor));}+void ConnectionFromClient::set_content_blocking_enabled(Web::PageId page_id, bool enabled)+{+ if (auto page = this->page(page_id); page.has_value())+ page->page().set_content_blocking_enabled(enabled);+}+void ConnectionFromClient::set_content_blockers(Core::AnonymousBuffer patterns_buffer){auto& blocker = Web::ContentBlocker::the();diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.hindex f300cb98d54..5c6c912ded6 100644--- a/Services/WebContent/ConnectionFromClient.h+++ b/Services/WebContent/ConnectionFromClient.h@@ -230,7 +230,9 @@ private:virtual void clone_dom_node(Web::PageId page_id, Web::UniqueNodeID node_id) override;virtual void remove_dom_node(Web::PageId page_id, Web::UniqueNodeID node_id) override;+ virtual void set_content_blocking_enabled(Web::PageId page_id, bool enabled) override;virtual void set_content_blockers(Core::AnonymousBuffer patterns) override;+virtual void set_autoplay_settings(Web::PageId page_id, Web::HTML::AutoplayPolicy policy, Vector<Utf16String> allowlist) override;virtual void set_preferred_color_scheme(Web::PageId page_id, Web::CSS::PreferredColorScheme) override;virtual void set_preferred_contrast(Web::PageId page_id, Web::CSS::PreferredContrast) override;diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipcindex 7f095d4cc3f..b7a8bb75f33 100644--- a/Services/WebContent/WebContentServer.ipc+++ b/Services/WebContent/WebContentServer.ipc@@ -238,7 +238,9 @@ endpoint WebContentServer routed by Web::PageId page_idfind_in_page_previous_match(Web::PageId page_id) =|find_in_page_end(Web::PageId page_id) =|+ set_content_blocking_enabled(Web::PageId page_id, bool enabled) =|set_content_blockers(Core::AnonymousBuffer patterns) =|+set_autoplay_settings(Web::PageId page_id, Web::HTML::AutoplayPolicy policy, Vector<Utf16String> allowlist) =|set_preferred_color_scheme(Web::PageId page_id, Web::CSS::PreferredColorScheme color_scheme) =|set_preferred_contrast(Web::PageId page_id, Web::CSS::PreferredContrast contrast) =|diff --git a/Tests/LibWeb/TestContentBlocker.cpp b/Tests/LibWeb/TestContentBlocker.cppindex b90e4418823..e2ee06c967d 100644--- a/Tests/LibWeb/TestContentBlocker.cpp+++ b/Tests/LibWeb/TestContentBlocker.cpp@@ -16,7 +16,6 @@ static ContentBlocker& make_blocker(Vector<String> rules){auto& blocker = ContentBlocker::the();MUST(blocker.set_patterns(rules));- blocker.set_filtering_enabled(true);return blocker;}@@ -101,27 +100,6 @@ TEST_CASE(empty_rules_clear_previous_rules)EXPECT(!blocker.is_filtered(url("https://ads.example.com/script.js"sv), source_url, ContentBlocker::ResourceType::Script));}-TEST_CASE(disable_filtering)-{- Vector<String> rules = {- { "||example.com^"_string },- { "##.ad"_string }- };-- auto& blocker = make_blocker(move(rules));- blocker.set_filtering_enabled(false);- Vector<Utf16FlyString> classes = { "ad"_utf16_fly_string };- Vector<Utf16FlyString> ids;-- EXPECT(!blocker.is_filtered(url("https://example.com"sv)));- EXPECT(!blocker.is_filtered(url("http://example.com/ads"sv)));- EXPECT(blocker.cosmetic_style_sheet_for_url(url("https://example.com"sv), classes.span(), ids.span()).is_empty());-- blocker.set_filtering_enabled(true);- EXPECT(blocker.is_filtered(url("https://example.com"sv)));- EXPECT(!blocker.cosmetic_style_sheet_for_url(url("https://example.com"sv), classes.span(), ids.span()).is_empty());-}-TEST_CASE(fetch_metadata_maps_to_resource_type){using Request = Fetch::Infrastructure::Request;diff --git a/Tests/LibWeb/TestInlineStyle.cpp b/Tests/LibWeb/TestInlineStyle.cppindex add3c303656..a8f3bca29b1 100644--- a/Tests/LibWeb/TestInlineStyle.cpp+++ b/Tests/LibWeb/TestInlineStyle.cpp@@ -16,6 +16,7 @@#include <LibWeb/HTML/HTMLDocument.h>#include <LibWeb/HTML/HTMLElement.h>#include <LibWeb/HTML/LocalTraversableNavigable.h>+#include <LibWeb/Loader/ContentBlocker.h>#include <LibWeb/Namespace.h>#include <LibWeb/Page/Page.h>#include <LibWeb/Platform/FontPlugin.h>@@ -64,12 +65,21 @@ public:GC_DEFINE_ALLOCATOR(TestPageClient);+static void install_font_plugin()+{+ static bool installed = [] {+ static Web::Platform::FontPlugin font_plugin(false);+ Web::Platform::FontPlugin::install(font_plugin);+ return true;+ }();+ VERIFY(installed);+}+}TEST_CASE(replacing_custom_property_only_inline_blocks_updates_dependent_style){- static Web::Platform::FontPlugin font_plugin(false);- Web::Platform::FontPlugin::install(font_plugin);+ install_font_plugin();auto principal_realm = Web::Bindings::create_a_principal_javascript_realm();VERIFY(principal_realm.ptr());auto& vm = Web::Bindings::main_thread_vm();@@ -100,3 +110,44 @@ TEST_CASE(replacing_custom_property_only_inline_blocks_updates_dependent_style)replace_block("--paint: blue"_utf16, "rgb(0, 0, 255)"_utf16);replace_block(""_utf16, "rgb(0, 0, 0)"_utf16);}++TEST_CASE(content_blocking_policy_isolated_between_pages)+{+ install_font_plugin();+ auto principal_realm = Web::Bindings::create_a_principal_javascript_realm();+ VERIFY(principal_realm.ptr());+ auto& vm = Web::Bindings::main_thread_vm();+ auto make_page = [&] {+ auto client = vm.heap().allocate<TestPageClient>();+ auto page = Web::Page::create(client);+ client->m_page = page.ptr();+ auto traversable = Web::HTML::LocalTraversableNavigable::create_a_new_top_level_traversable(page, nullptr, {});+ page->set_top_level_traversable(traversable);+ auto& document = *traversable->active_document();+ if (!document.body())+ MUST(document.populate_with_html_head_and_body());+ auto element = MUST(Web::DOM::create_element(document, "div"_utf16_fly_string, Web::Namespace::HTML));+ element->set_attribute("class"_fly_string, "advertisement"_utf16);+ MUST(document.body()->append_child(element));+ return page;+ };+ auto first_page = make_page();+ auto second_page = make_page();+ auto& blocker = Web::ContentBlocker::the();+ Vector<String> rules { "##.advertisement"_string, "||ads.example.com^"_string };+ MUST(blocker.set_patterns(rules));+ auto& first_document = *static_cast<Web::HTML::LocalNavigable&>(*first_page->top_level_traversable()).active_document();+ auto& second_document = *static_cast<Web::HTML::LocalNavigable&>(*second_page->top_level_traversable()).active_document();+ EXPECT(!first_document.content_blocker_style_sheet().is_empty());+ EXPECT(!second_document.content_blocker_style_sheet().is_empty());+ first_page->set_content_blocking_enabled(false);+ EXPECT(!first_page->content_blocking_enabled());+ EXPECT(second_page->content_blocking_enabled());+ EXPECT(first_document.content_blocker_style_sheet().is_empty());+ EXPECT(!second_document.content_blocker_style_sheet().is_empty());+ EXPECT(blocker.has_rules());+ first_page->set_content_blocking_enabled(true);+ EXPECT(!first_document.content_blocker_style_sheet().is_empty());+ EXPECT(!second_document.content_blocker_style_sheet().is_empty());+ MUST(blocker.set_patterns({}));+}