1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:48:12 +00:00

Browser+Ladybird+LibWeb: Prevent infinite growth of content filters

We never clear content filters on either end of the Browser-WebContent
IPC connection. So when the filters change, we re-append all filters to
the Vector holding them. This incidentally makes it impossible to remove
a filter.

Change both sides to clear their filter lists when receiving a new set
of filters.
This commit is contained in:
Timothy Flynn 2023-04-21 07:36:40 -04:00 committed by Andreas Kling
parent b0ffb15e13
commit 76ae60da15
5 changed files with 33 additions and 14 deletions

View file

@ -121,15 +121,24 @@ static ErrorOr<void> load_content_filters()
file_or_error = Core::File::open(DeprecatedString::formatted("{}/res/ladybird/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read); file_or_error = Core::File::open(DeprecatedString::formatted("{}/res/ladybird/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
if (file_or_error.is_error()) if (file_or_error.is_error())
return file_or_error.release_error(); return file_or_error.release_error();
auto file = file_or_error.release_value(); auto file = file_or_error.release_value();
auto ad_filter_list = TRY(Core::BufferedFile::create(move(file))); auto ad_filter_list = TRY(Core::BufferedFile::create(move(file)));
auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
Vector<DeprecatedString> patterns;
while (TRY(ad_filter_list->can_read_line())) { while (TRY(ad_filter_list->can_read_line())) {
auto line = TRY(ad_filter_list->read_line(buffer)); auto line = TRY(ad_filter_list->read_line(buffer));
if (!line.is_empty()) { if (line.is_empty())
Web::ContentFilter::the().add_pattern(line); continue;
}
TRY(patterns.try_append(line));
} }
auto& content_filter = Web::ContentFilter::the();
TRY(content_filter.set_patterns(patterns));
return {}; return {};
} }

View file

@ -50,6 +50,9 @@ static ErrorOr<void> load_content_filters()
auto file = TRY(Core::File::open(DeprecatedString::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::File::OpenMode::Read)); auto file = TRY(Core::File::open(DeprecatedString::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::File::OpenMode::Read));
auto ad_filter_list = TRY(Core::BufferedFile::create(move(file))); auto ad_filter_list = TRY(Core::BufferedFile::create(move(file)));
auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
Browser::g_content_filters.clear_with_capacity();
while (TRY(ad_filter_list->can_read_line())) { while (TRY(ad_filter_list->can_read_line())) {
auto line = TRY(ad_filter_list->read_line(buffer)); auto line = TRY(ad_filter_list->read_line(buffer));
if (!line.is_empty()) if (!line.is_empty())

View file

@ -33,15 +33,23 @@ bool ContentFilter::is_filtered(const AK::URL& url) const
return false; return false;
} }
void ContentFilter::add_pattern(DeprecatedString const& pattern) ErrorOr<void> ContentFilter::set_patterns(ReadonlySpan<DeprecatedString> patterns)
{ {
StringBuilder builder; m_patterns.clear_with_capacity();
if (!pattern.starts_with('*'))
builder.append('*'); for (auto const& pattern : patterns) {
builder.append(pattern); StringBuilder builder;
if (!pattern.ends_with('*'))
builder.append('*'); if (!pattern.starts_with('*'))
m_patterns.empend(builder.to_deprecated_string()); TRY(builder.try_append('*'));
TRY(builder.try_append(pattern));
if (!pattern.ends_with('*'))
TRY(builder.try_append('*'));
TRY(m_patterns.try_empend(builder.to_deprecated_string()));
}
return {};
} }
} }

View file

@ -16,7 +16,7 @@ public:
static ContentFilter& the(); static ContentFilter& the();
bool is_filtered(const AK::URL&) const; bool is_filtered(const AK::URL&) const;
void add_pattern(DeprecatedString const&); ErrorOr<void> set_patterns(ReadonlySpan<DeprecatedString>);
private: private:
ContentFilter(); ContentFilter();

View file

@ -636,8 +636,7 @@ Messages::WebContentServer::DumpLayoutTreeResponse ConnectionFromClient::dump_la
void ConnectionFromClient::set_content_filters(Vector<DeprecatedString> const& filters) void ConnectionFromClient::set_content_filters(Vector<DeprecatedString> const& filters)
{ {
for (auto& filter : filters) Web::ContentFilter::the().set_patterns(filters).release_value_but_fixme_should_propagate_errors();
Web::ContentFilter::the().add_pattern(filter);
} }
void ConnectionFromClient::set_autoplay_allowed_on_all_websites() void ConnectionFromClient::set_autoplay_allowed_on_all_websites()