2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-03 23:54:20 +08:00
discourse/app/serializers/post_item_excerpt.rb
Régis Hanol ce9abeb50d
FIX: Handle Nokogiri HTML parser limits in PrettyText.excerpt (#37610)
The /drafts endpoint returns a 500 error when any draft contains HTML
with excessive nesting depth or too many attributes per element.

Nokogiri::HTML5.fragment raises ArgumentError when these limits are
exceeded, and PrettyText.excerpt had no error handling for this. A
previous fix in PostItemExcerpt only caught the tree depth variant,
leaving the attributes limit unhandled, and only protecting one of the
13+ callers.

Rescue ArgumentError around the Nokogiri::HTML5.fragment call in
PrettyText.excerpt and return "" on failure. This is consistent with the
existing blank-input guard and protects all callers at once. The
now-redundant rescue in PostItemExcerpt is removed.

Ref - t/173858
2026-02-06 22:00:54 +01:00

24 lines
425 B
Ruby

# frozen_string_literal: true
module PostItemExcerpt
def self.included(base)
base.attributes(:excerpt, :truncated)
end
def cooked
@cooked ||= object.cooked || PrettyText.cook(object.raw)
end
def excerpt
return nil unless cooked
@excerpt ||= PrettyText.excerpt(cooked, 300, keep_emoji_images: true)
end
def truncated
true
end
def include_truncated?
cooked.length > 300
end
end