Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/lib/rich_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ def initialize(text:)
# Iterate over all images and unlink tracking pixels
css('img[src]').each do |node|
node.unlink if TrackingDetection.tracking_pixel?(node)

# Don't send referrer when requesting images
node.set_attribute('referrerpolicy', 'no-referrer')
end
end

# Iterate over all urls in the document
# If the block returns a new url, the element gets replaced
# rubocop:disable Metrics/AbcSize
def handle_img_urls(&)
def handle_img_urls(&) # rubocop:disable Metrics/AbcSize
css('img[src]').each do |node|
node.set_attribute('src', yield(node['src']))
end
Expand All @@ -31,7 +33,6 @@ def handle_img_urls(&)
node.set_attribute('style', urls_in_styles(node['style'], &))
end
end
# rubocop:enable Metrics/AbcSize

def add_to_head(node_or_string)
at_css('head').add_child(node_or_string)
Expand Down
5 changes: 3 additions & 2 deletions test/components/entry_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class EntryComponentTest < ViewComponent::TestCase
render_inline(EntryComponent.new(entry:))

assert_selector '.entry__iframe'
assert_includes page.find('.entry__iframe')[:srcdoc], '<body><div><img src="https://example.com/image.jpg"></div></body>'
assert_includes page.find('.entry__iframe')[:srcdoc],
'<body><div><img src="https://example.com/image.jpg" referrerpolicy="no-referrer"></div></body>'
end

test 'should replace image src when proxied' do
Expand All @@ -55,7 +56,7 @@ class EntryComponentTest < ViewComponent::TestCase

assert_selector '.entry__iframe'

regex = %r{<img src="/rails/active_storage/blobs/redirect/[A-Za-z\d=-]+/image.jpg">}
regex = %r{<img src="/rails/active_storage/blobs/redirect/[A-Za-z\d=-]+/image.jpg"}

assert_match regex, page.find('.entry__iframe')[:srcdoc]
end
Expand Down
8 changes: 8 additions & 0 deletions test/lib/rich_text_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
require 'test_helper'

class RichTextTest < ActiveSupport::TestCase
test 'should set no-referrer for all images' do
text = RichText.new(
text: '<div><img src="https://example.com/image.jpg" /></div>'
)

assert_includes text.to_html, '<img src="https://example.com/image.jpg" referrerpolicy="no-referrer">'
end

# Handle_img_urls
test 'should detect url is srcset' do
text = RichText.new(
Expand Down