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:
@@ -18,6 +18,8 @@ class AutoLinkScrubber < Loofah::Scrubber
|
|||||||
TRAILING_PUNCTUATION = %(.?,:!;"'<>)
|
TRAILING_PUNCTUATION = %(.?,:!;"'<>)
|
||||||
TRAILING_PUNCTUATION_REGEXP = /[#{Regexp.escape(TRAILING_PUNCTUATION)}]+\z/
|
TRAILING_PUNCTUATION_REGEXP = /[#{Regexp.escape(TRAILING_PUNCTUATION)}]+\z/
|
||||||
|
|
||||||
|
MAX_TEXT_NODE_LENGTH = 10_000
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@direction = :top_down
|
@direction = :top_down
|
||||||
@regexp_timeout_reported = false
|
@regexp_timeout_reported = false
|
||||||
@@ -56,6 +58,8 @@ class AutoLinkScrubber < Loofah::Scrubber
|
|||||||
end
|
end
|
||||||
|
|
||||||
def find_links(text)
|
def find_links(text)
|
||||||
|
return [] if text.length > MAX_TEXT_NODE_LENGTH
|
||||||
|
|
||||||
links = []
|
links = []
|
||||||
|
|
||||||
text.scan(AUTOLINK_REGEXP) do
|
text.scan(AUTOLINK_REGEXP) do
|
||||||
|
|||||||
@@ -125,6 +125,17 @@ class HtmlHelperTest < ActionView::TestCase
|
|||||||
end
|
end
|
||||||
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
|
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 \
|
||||||
|
|||||||
Reference in New Issue
Block a user