Skip to content

Commit

Permalink
Merge pull request #52093 from p8/actiontext/fix-remote-image-preview
Browse files Browse the repository at this point in the history
Only sanitize `content` attribute when present in attachments
  • Loading branch information
rafaelfranca committed Jun 12, 2024
1 parent 0e76928 commit 2b05f76
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions actiontext/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Only sanitize `content` attribute when present in attachments.

*Petrik de Heus*


## Rails 7.1.3.4 (June 04, 2024) ##

* Sanitize ActionText HTML ContentAttachment in Trix edit view
Expand Down
4 changes: 3 additions & 1 deletion actiontext/lib/action_text/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ def append_attachables(attachables)

def render_attachments(**options, &block)
content = fragment.replace(ActionText::Attachment.tag_name) do |node|
node["content"] = sanitize_content_attachment(node["content"])
if node.key? "content"
node["content"] = sanitize_content_attachment(node["content"])
end
block.call(attachment_for_node(node, **options))
end
self.class.new(content, canonicalize: false)
Expand Down
12 changes: 12 additions & 0 deletions actiontext/test/unit/content_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ class ActionText::ContentTest < ActiveSupport::TestCase
ActionText::ContentHelper.allowed_attributes = old_attrs
end

test "sanitizes attachment markup for Trix" do
html = '<action-text-attachment content="<img src=\&quot;.\&quot; onerror=alert>"></action-text-attachment>'
trix_html = '<figure data-trix-attachment="{&quot;content&quot;:&quot;<img src=\\&quot;\\\\%22.\\\\%22\\&quot;>&quot;}"></figure>'
assert_equal trix_html, content_from_html(html).to_trix_html.strip
end

test "does not add missing content attribute" do
html = '<action-text-attachment sgid="123"></action-text-attachment>'
trix_html = '<figure data-trix-attachment="{&quot;sgid&quot;:&quot;123&quot;}"></figure>'
assert_equal trix_html, content_from_html(html).to_trix_html.strip
end

test "renders with layout when in a new thread" do
html = "<h1>Hello world</h1>"
rendered = nil
Expand Down

0 comments on commit 2b05f76

Please sign in to comment.