Skip to content

Commit 56c61c0

Browse files
committed
fix: replace slow regex attribute check with Loofah method
which uses the Crass parser
1 parent 71b5aca commit 56c61c0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/rails/html/scrubbers.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,11 @@ def scrub_attribute(node, attr_node)
146146
attr_node.remove
147147
end
148148
end
149+
149150
if Loofah::HTML5::SafeList::SVG_ATTR_VAL_ALLOWS_REF.include?(attr_name)
150-
attr_node.value = attr_node.value.gsub(/url\s*\(\s*[^#\s][^)]+?\)/m, ' ') if attr_node.value
151+
Loofah::HTML5::Scrub.scrub_attribute_that_allows_local_ref(attr_node)
151152
end
153+
152154
if Loofah::HTML5::SafeList::SVG_ALLOW_LOCAL_HREF.include?(node.name) && attr_name == 'xlink:href' && attr_node.value =~ /^\s*[^#\s].*/m
153155
attr_node.remove
154156
end

test/sanitizer_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,16 @@ def test_disallow_the_dangerous_safelist_combination_of_select_and_style
606606
refute_includes(sanitized, "style")
607607
end
608608

609+
def test_scrubbing_svg_attr_values_that_allow_ref
610+
input = %Q(<div fill="yellow url(http://bad.com/) #fff">hey</div>)
611+
expected = %Q(<div fill="yellow #fff">hey</div>)
612+
actual = scope_allowed_attributes %w(fill) do
613+
safe_list_sanitize(input)
614+
end
615+
616+
assert_equal(expected, actual)
617+
end
618+
609619
protected
610620

611621
def xpath_sanitize(input, options = {})

0 commit comments

Comments
 (0)