fix(mailbox): render inline images without Content-Disposition (#11949)

## Description
This pull request addresses issue #11948, where inline images embedded
in emails (such as those sent from Outlook) are not rendered correctly
if the Content-Disposition header is missing.

The solution ensures that images referenced via cid: in the HTML body
are correctly identified and rewritten using url_for.

Fixes #11948

## Type of change
Bug fix (non-breaking change which fixes an issue)

## How Has This Been Tested?
Added test: detects image inline attachment by cid reference when
Content-Disposition is missing

## Checklist:

- [X] My code follows the style guidelines of this project
- [X] I have performed a self-review of my code
- [X] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [X] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules

---------

Co-authored-by: Pranav <pranav@chatwoot.com>
Co-authored-by: Sojan Jose <sojan@pepalo.com>
Co-authored-by: Sony Mathew <sony@chatwoot.com>
Co-authored-by: Sony Mathew <2040199+sony-mathew@users.noreply.github.com>
This commit is contained in:
Wendell Gasparoni
2026-05-06 10:26:31 -03:00
committed by GitHub
parent d43a87c9dc
commit 42bba748cf
4 changed files with 190 additions and 12 deletions

View File

@@ -77,4 +77,60 @@ RSpec.describe MailboxHelper do
expect(text_content).to include(Rails.application.routes.url_helpers.url_for(mail_attachment[:blob]))
end
end
describe '#body_references_cid?' do
let(:helper_instance) { mailbox_helper_obj.new(conversation, processed_mail) }
it 'detects percent-encoded CID references in HTML content' do
helper_instance.instance_variable_set(:@html_content, '<img src="cid:image001.jpg%40test">')
expect(helper_instance.send(:body_references_cid?, 'image001.jpg@test')).to be true
end
end
describe '#upload_inline_image' do
let(:mail_attachment) do
{
original: OpenStruct.new(cid: 'image001.jpg@test'),
blob: get_blob_for('spec/assets/avatar.png', 'image/png')
}
end
let(:helper_instance) { mailbox_helper_obj.new(conversation, processed_mail) }
it 'replaces percent-encoded CID references in HTML content' do
allow(Rails.application.routes.url_helpers).to receive(:url_for).and_return('/fake-image-url')
helper_instance.instance_variable_set(:@html_content, '<img src="cid:image001.jpg%40test">')
helper_instance.send(:upload_inline_image, mail_attachment)
html_content = helper_instance.instance_variable_get(:@html_content)
expect(html_content).to include('/fake-image-url"')
expect(html_content).not_to include('cid:')
end
end
describe '#add_attachments_to_message' do
let(:mail) { create_inbound_email_from_fixture('cid_inline_images_without_disposition.eml').mail }
let(:processed_mail) { MailPresenter.new(mail) }
let(:conversation) { create(:conversation) }
let(:helper_instance) { mailbox_helper_obj.new(conversation, processed_mail) }
before do
helper_instance.send(:create_message)
end
it 'detects inline image attachment by cid reference when Content-Disposition is missing' do
allow(Rails.application.routes.url_helpers).to receive(:url_for).and_return('/fake-image-url')
helper_instance.send(:add_attachments_to_message)
message = conversation.messages[0]
expect(message.attachments.count).to eq(0)
html_content = message.content_attributes[:email][:html_content][:full]
expect(html_content).to include('/fake-image-url"')
expect(html_content).not_to include('cid:')
end
end
end