Skip to content

Commit d631696

Browse files
authored
Merge pull request rails#52093 from p8/actiontext/fix-remote-image-preview
Only sanitize `content` attribute when present in attachments
2 parents 860f23c + 52c21f9 commit d631696

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

actiontext/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
* Only sanitize `content` attribute when present in attachments.
2+
3+
*Petrik de Heus*
14

25
Please check [7-2-stable](https://github.com/rails/rails/blob/7-2-stable/actiontext/CHANGELOG.md) for previous changes.

actiontext/lib/action_text/content.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ 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"])
100+
if node.key? "content"
101+
node["content"] = sanitize_content_attachment(node["content"])
102+
end
101103
block.call(attachment_for_node(node, **options))
102104
end
103105
self.class.new(content, canonicalize: false)

actiontext/test/unit/content_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ class ActionText::ContentTest < ActiveSupport::TestCase
158158
ActionText::ContentHelper.allowed_attributes = old_attrs
159159
end
160160

161+
test "sanitizes attachment markup for Trix" do
162+
html = '<action-text-attachment content="<img src=\&quot;.\&quot; onerror=alert>"></action-text-attachment>'
163+
trix_html = '<figure data-trix-attachment="{&quot;content&quot;:&quot;<img src=\\&quot;\\\\%22.\\\\%22\\&quot;>&quot;}"></figure>'
164+
assert_equal trix_html, content_from_html(html).to_trix_html.strip
165+
end
166+
167+
test "does not add missing content attribute" do
168+
html = '<action-text-attachment sgid="123"></action-text-attachment>'
169+
trix_html = '<figure data-trix-attachment="{&quot;sgid&quot;:&quot;123&quot;}"></figure>'
170+
assert_equal trix_html, content_from_html(html).to_trix_html.strip
171+
end
172+
161173
test "renders with layout when in a new thread" do
162174
html = "<h1>Hello world</h1>"
163175
rendered = nil

0 commit comments

Comments
 (0)