Skip to content
Open
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
6 changes: 5 additions & 1 deletion lib/postal/message_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ def generate

def parse_parts(parts)
parts.each do |part|
case part.content_type
# Match on the bare MIME type rather than the full Content-Type header
# value: multipart/related containers commonly carry the RFC 2387
# type="text/html" parameter, which would otherwise match the
# /text\/html/ branch and destroy the container.
case part.mime_type
when /text\/html/
part.body = parse(part.body.decoded.dup, :html)
part.content_transfer_encoding = nil
Expand Down
52 changes: 52 additions & 0 deletions spec/lib/postal/message_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,56 @@
expect(parser.new_body).to match(/^Hello world! https:\/\/click\.#{message.domain.name}/)
expect(parser.tracked_links).to eq 1
end

it "should not treat a multipart/related container with a type parameter as an HTML part" do
message = create_plain_text_message(server, "Hello world!", "test@example.com")
create(:track_domain, server: server, domain: message.domain)

raw = <<~MESSAGE.gsub("\n", "\r\n")
From: test@#{message.domain.name}
To: test@example.com
Subject: Inline image test
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="alt-boundary"

--alt-boundary
Content-Type: text/plain; charset=utf-8

Hello world!
--alt-boundary
Content-Type: multipart/related;
boundary="rel-boundary";
type="text/html"

--rel-boundary
Content-Type: text/html; charset=utf-8

<html><body><p>Hello world!</p><img src="cid:img1"></body></html>
--rel-boundary
Content-Type: image/png; name=chart.png
Content-ID: <img1>
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=chart.png

iVBORw0KGgo=
--rel-boundary--

--alt-boundary--
MESSAGE
allow(message).to receive(:raw_message).and_return(raw)

parser = Postal::MessageParser.new(message)

parsed = Mail.new("#{parser.new_headers}\r\n\r\n#{parser.new_body}")
related = parsed.parts.detect { |p| p.mime_type == "multipart/related" }
expect(related).to_not be_nil
expect(related.parts.map(&:mime_type)).to match_array(["text/html", "image/png"])

html_part = related.parts.detect { |p| p.mime_type == "text/html" }
expect(html_part.body.decoded).to include("img/#{server.token}/#{message.token}")

image_part = related.parts.detect { |p| p.mime_type == "image/png" }
expect(image_part.content_id).to eq "<img1>"
end
end