Rescue regexp timeout

This commit is contained in:
Zacharias Dyna Knudsen
2026-03-20 11:55:00 +01:00
parent 6538cf80a9
commit 7399ec5bd9
2 changed files with 24 additions and 0 deletions
+3
View File
@@ -71,6 +71,9 @@ class AutoLinkScrubber < Loofah::Scrubber
end end
links links
rescue Regexp::TimeoutError => error
Sentry.capture_exception error if Fizzy.saas?
[]
end end
def clean_url(url) def clean_url(url)
+21
View File
@@ -104,6 +104,27 @@ class HtmlHelperTest < ActionView::TestCase
format_html(%(<p>Contact us at <a href="mailto:support@example.com">support@example.com</a></p>)) format_html(%(<p>Contact us at <a href="mailto:support@example.com">support@example.com</a></p>))
end end
test "gracefully handle regexp timeout by skipping auto-linking" do
input = "<p>Check this: https://example.com</p>"
String.class_eval do
alias_method :original_scan, :scan
define_method(:scan) do |*args, &block|
if args.first == AutoLinkScrubber::AUTOLINK_REGEXP
raise Regexp::TimeoutError
end
original_scan(*args, &block)
end
end
assert_equal_html %(<p>Check this: https://example.com</p>), format_html(input)
ensure
String.class_eval do
alias_method :scan, :original_scan
remove_method :original_scan
end
end
test "don't autolink content in excluded elements" do test "don't autolink content in excluded elements" do
%w[ figcaption pre code ].each do |element| %w[ figcaption pre code ].each do |element|
assert_equal_html \ assert_equal_html \