Skip to content

Commit e215bf3

Browse files
fresh-eggstenderlove
authored andcommitted
Sanitize ActionText HTML ContentAttachment in Trix edit view
[CVE-2024-32464] Instances of ActionText::Attachable::ContentAttachment included within a rich_text_area tag could potentially contain unsanitized HTML. This could lead to a potential cross site scripting issue within the Trix editor. This change enforces existing sanitization routines on ActionText::Attachable::ContentAttachment attachments.
1 parent 35858f1 commit e215bf3

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

actiontext/app/helpers/action_text/content_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ def render_action_text_content(content)
1616
sanitize_action_text_content(render_action_text_attachments(content))
1717
end
1818

19+
def sanitize_content_attachment(content_attachment)
20+
sanitizer.sanitize(
21+
content_attachment,
22+
tags: sanitizer_allowed_tags,
23+
attributes: sanitizer_allowed_attributes,
24+
scrubber: scrubber,
25+
)
26+
end
27+
1928
def sanitize_action_text_content(content)
2029
sanitizer.sanitize(
2130
content.to_html,

actiontext/lib/action_text/content.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module ActionText
2222
# body.to_s # => "<h1>Funny times!</h1>"
2323
# body.to_plain_text # => "Funny times!"
2424
class Content
25-
include Rendering, Serialization
25+
include Rendering, Serialization, ContentHelper
2626

2727
attr_reader :fragment
2828

@@ -97,6 +97,7 @@ def append_attachables(attachables)
9797

9898
def render_attachments(**options, &block)
9999
content = fragment.replace(ActionText::Attachment.tag_name) do |node|
100+
node["content"] = sanitize_content_attachment(node["content"])
100101
block.call(attachment_for_node(node, **options))
101102
end
102103
self.class.new(content, canonicalize: false)

actiontext/test/unit/attachment_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ class ActionText::AttachmentTest < ActiveSupport::TestCase
7979
end
8080
end
8181

82+
test "sanitizes HTML content attachment" do
83+
attachment = attachment_from_html('<action-text-attachment content-type="text/html" content="<img src=\&quot;.\&quot; onerror=alert>"></action-text-attachment>')
84+
attachable = attachment.attachable
85+
86+
ActionText::Content.with_renderer MessagesController.renderer do
87+
assert_equal "<img src=\"\\%22.\\%22\">", attachable.to_html.strip
88+
end
89+
end
90+
8291
test "defaults trix partial to model partial" do
8392
attachable = Page.create! title: "Homepage"
8493
assert_equal "pages/page", attachable.to_trix_content_attachment_partial_path

0 commit comments

Comments
 (0)