diff --git a/lib/auto_link_scrubber.rb b/lib/auto_link_scrubber.rb index 5ac9f2f3d..557a168c2 100644 --- a/lib/auto_link_scrubber.rb +++ b/lib/auto_link_scrubber.rb @@ -71,6 +71,9 @@ class AutoLinkScrubber < Loofah::Scrubber end links + rescue Regexp::TimeoutError => error + Sentry.capture_exception error if Fizzy.saas? + [] end def clean_url(url) diff --git a/test/helpers/html_helper_test.rb b/test/helpers/html_helper_test.rb index 0fd6c268a..f19d80003 100644 --- a/test/helpers/html_helper_test.rb +++ b/test/helpers/html_helper_test.rb @@ -104,6 +104,27 @@ class HtmlHelperTest < ActionView::TestCase format_html(%(

Contact us at support@example.com

)) end + test "gracefully handle regexp timeout by skipping auto-linking" do + input = "

Check this: https://example.com

" + + 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 %(

Check this: https://example.com

), 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 %w[ figcaption pre code ].each do |element| assert_equal_html \