From b70b3e867c49ee6166dcd9b9e660be905c58bcfb Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Tue, 3 Nov 2020 13:53:15 -0300 Subject: [PATCH] FIX: Safely skip secure_media steps when it's not enabled (#11110) * FIX: Safely skip secure_media steps when it's not enabled * DEV: Secure media tests should enable secure media --- lib/email/styles.rb | 2 +- lib/pretty_text.rb | 2 +- spec/components/email/styles_spec.rb | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/email/styles.rb b/lib/email/styles.rb index 4b53d53b496..562b4982a74 100644 --- a/lib/email/styles.rb +++ b/lib/email/styles.rb @@ -278,7 +278,7 @@ module Email def to_html # needs to be before class + id strip because we need to style redacted # media and also not double-redact already redacted from lower levels - replace_secure_media_urls + replace_secure_media_urls if SiteSetting.secure_media? strip_classes_and_ids replace_relative_urls diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index c98ea50e766..ba25ed4921a 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -432,7 +432,7 @@ module PrettyText # images by themselves or inside a onebox doc.css('img[src]').each do |img| - url = if img.parent.classes.include?("aspect-image") + url = if img.parent.classes.include?("aspect-image") && img.attributes["srcset"].present? # we are using the first image from the srcset here so we get the # optimized image instead of the original, because an optimized diff --git a/spec/components/email/styles_spec.rb b/spec/components/email/styles_spec.rb index 475364b5859..84a1e3e204b 100644 --- a/spec/components/email/styles_spec.rb +++ b/spec/components/email/styles_spec.rb @@ -188,6 +188,11 @@ describe Email::Styles do end context "replace_secure_media_urls" do + before do + setup_s3 + SiteSetting.secure_media = true + end + let(:attachments) { { 'testimage.png' => stub(url: 'email/test.png') } } it "replaces secure media within a link with a placeholder" do frag = html_fragment("") @@ -208,6 +213,11 @@ describe Email::Styles do end context "inline_secure_images" do + before do + setup_s3 + SiteSetting.secure_media = true + end + let(:attachments) { { 'testimage.png' => stub(url: 'cid:email/test.png') } } fab!(:upload) { Fabricate(:upload, original_filename: 'testimage.png', secure: true, sha1: '123456') } let(:html) { "" }