Skip to content

Truly independent web browser

LibWeb: Treat SVG filters with more than 256 primitives as errors

Andreas Kling630819ce4b5d
diff --git a/Libraries/LibWeb/Painting/SvgPaintResources.cpp b/Libraries/LibWeb/Painting/SvgPaintResources.cpp
index 7a241badf5c..d680664dd69 100644
--- a/Libraries/LibWeb/Painting/SvgPaintResources.cpp
+++ b/Libraries/LibWeb/Painting/SvgPaintResources.cpp
@@ -22,6 +22,16 @@ bool push_svg_filter_reference(Layout::BegunRead const& read, DOM::Document& doc
auto filter_element = resolve_svg_filter_reference({ .pointer = url_value }, layout_node);
if (!filter_element)
return false;
+
+ // INTEROP: Other engines treat a filter with more than a few hundred primitives as an error, which also keeps a
+ // chain of thousands of primitives from overflowing the stack of the painter that applies them one by one.
+ static constexpr size_t max_primitives_per_filter = 256;
+ size_t primitive_count = 0;
+ for (auto const* child = filter_element->first_child(); child; child = child->next_sibling()) {
+ if (is<DOM::Element>(*child) && ++primitive_count > max_primitives_per_filter)
+ return false;
+ }
+
auto dest_size = Gfx::enclosing_int_rect(absolute_rect(layout_node).to_type<float>()).size();
filter_element->push_primitives(sink, dest_size);
return true;
diff --git a/Tests/LibWeb/Crash/SVG/filter-with-many-primitives.html b/Tests/LibWeb/Crash/SVG/filter-with-many-primitives.html
new file mode 100644
index 00000000000..0a40f5e7f7d
--- /dev/null
+++ b/Tests/LibWeb/Crash/SVG/filter-with-many-primitives.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<!-- Thousands of chained filter primitives must not overflow the stack of whatever process paints the filter. -->
+<div style="filter: url(#filter)">test</div>
+<svg width="100" height="100">
+ <filter id="filter">
+ <feMorphology operator="dilate" radius="1" />
+ </filter>
+</svg>
+<script>
+ filter.innerHTML = filter.innerHTML.repeat(10000);
+</script>