FIX: Don't attach images that aren't rendered in the digest e-mail (#31525)

When secure uploads are enabled, we need to send images that are rendered in the digest e-mail as attachments. Before this change, we would indiscriminately attach all images in the relevant topic's first post, whether they were rendered the e-mail body or not.

This change fixes that by only attaching images that are referenced in the e-mail body.
This commit is contained in:
Ted Johansson
2025-02-27 11:41:17 +08:00
committed by GitHub
parent 38de3d7bd1
commit 39f4485939
2 changed files with 21 additions and 10 deletions

View File

@ -367,12 +367,13 @@ module Email
post.uploads.each do |original_upload|
optimized_1X = original_upload.optimized_images.first
# only attach images in digests
next if is_digest && !FileHelper.is_supported_image?(original_upload.original_filename)
if FileHelper.is_supported_image?(original_upload.original_filename) &&
!should_attach_image?(original_upload, optimized_1X)
next
if FileHelper.is_supported_image?(original_upload.original_filename)
next if !should_attach_image?(original_upload, optimized_1X)
# Don't attach images that aren't rendered in the e-mail.
next if is_digest && !@message.html_part.body.include?(original_upload.sha1)
else
# Only attach images in digests.
next if is_digest
end
attached_upload = optimized_1X || original_upload

View File

@ -247,6 +247,7 @@ RSpec.describe Email::Sender do
it "should remove the right headers" do
email_sender.send
expect(message.header["X-Discourse-Topic-Id"]).not_to be_present
expect(message.header["X-Discourse-Topic-Ids"]).not_to be_present
expect(message.header["X-Discourse-Post-Id"]).not_to be_present
expect(message.header["X-Discourse-Reply-Key"]).not_to be_present
end
@ -700,9 +701,18 @@ RSpec.describe Email::Sender do
Jobs::PullHotlinkedImages.any_instance.expects(:execute)
# Crafted so that the second image is not in the excerpt.
raw = <<~MD
IMAGE #1
#{UploadMarkdown.new(@secure_image).image_markdown}
> 11:15, restate my assumptions:
>
> 1. Mathematics is the language of nature.
> 2. Everything around us can be represented and understood through numbers.
> 3. If you graph these numbers, patterns emerge.
>
> Therefore: There are patterns everywhere in nature.
IMAGE #2
#{UploadMarkdown.new(@secure_image_2).image_markdown}
@ -738,13 +748,13 @@ RSpec.describe Email::Sender do
"multipart/mixed; boundary=\"#{summary.body.boundary}\"",
)
expect(summary.attachments.map(&:filename)).to contain_exactly(
*[@secure_image, @secure_image_2, @secure_image_3].map(&:original_filename),
*[@secure_image, @secure_image_3].map(&:original_filename),
)
expect(summary.attachments.size).to eq(3)
expect(summary.attachments.size).to eq(2)
expect(summary.to_s.scan("Content-Type: text/html;").length).to eq(1)
expect(summary.to_s.scan("Content-Type: text/plain;").length).to eq(1)
expect(summary.to_s.scan(/cid:[\w\-@.]+/).length).to eq(3)
expect(summary.to_s.scan(/cid:[\w\-@.]+/).uniq.length).to eq(3)
expect(summary.to_s.scan(/cid:[\w\-@.]+/).length).to eq(2)
expect(summary.to_s.scan(/cid:[\w\-@.]+/).uniq.length).to eq(2)
end
it "does not attach images that are not marked as secure, in the case of a non-secure upload copied to a PM" do