Skip auto-linking in very large text nodes

A large comment body was causing Regexp::TimeoutError in production.
The URI regexp isn't catastrophically backtracking — it's linear — but
with a long enough string it exceeds the 1s Regexp.timeout set by
Rails.

Skip scanning text nodes over 10KB, which is well beyond any
reasonable content for auto-linking and still keeps us many orders of
magnitued under the timeout on an unloaded machine.

Fixes FIZZY-Q4

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mike Dalessio
2026-03-20 15:52:39 -04:00
parent 95a45aa651
commit 87c659cd67
2 changed files with 15 additions and 0 deletions
+4
View File
@@ -18,6 +18,8 @@ class AutoLinkScrubber < Loofah::Scrubber
TRAILING_PUNCTUATION = %(.?,:!;"'<>)
TRAILING_PUNCTUATION_REGEXP = /[#{Regexp.escape(TRAILING_PUNCTUATION)}]+\z/
MAX_TEXT_NODE_LENGTH = 10_000
def initialize
@direction = :top_down
@regexp_timeout_reported = false
@@ -56,6 +58,8 @@ class AutoLinkScrubber < Loofah::Scrubber
end
def find_links(text)
return [] if text.length > MAX_TEXT_NODE_LENGTH
links = []
text.scan(AUTOLINK_REGEXP) do
+11
View File
@@ -125,6 +125,17 @@ class HtmlHelperTest < ActionView::TestCase
end
end
test "skip auto-linking in very large text nodes" do
url = "https://example.com"
large_text = "x" * 5_000 + " #{url} " + "y" * 5_000
input = "<p>#{large_text}</p>"
result = format_html(input)
assert_no_match(/<a/, result)
assert_includes result, url
end
test "don't autolink content in excluded elements" do
%w[ figcaption pre code ].each do |element|
assert_equal_html \