FIX: Gracefully handle inline images in emails (#12855)

FIX: Gracefully handle inline images in emails (#12855)

diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb
index bd32867..b580e7e 100644
--- a/lib/email/receiver.rb
+++ b/lib/email/receiver.rb
@@ -1124,8 +1124,8 @@ module Email
                     raw = raw.sub(match, replacement)
                   end
                 end
-              elsif raw[/\[image:.*?\d+[^\]]*\]/i]
-                raw.sub!(/\[image:.*?\d+[^\]]*\]/i, UploadMarkdown.new(upload).to_markdown)
+              elsif raw[/\[image:[^\]]*\]/i]
+                raw.sub!(/\[image:[^\]]*\]/i, UploadMarkdown.new(upload).to_markdown)
               else
                 raw << "\n\n#{UploadMarkdown.new(upload).to_markdown}\n\n"
               end
diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb
index b3b5a08..fcdc094 100644
--- a/spec/components/email/receiver_spec.rb
+++ b/spec/components/email/receiver_spec.rb
@@ -585,6 +585,19 @@ describe Email::Receiver do
       MD
     end
 
+    it "gracefully handles malformed images in HTML part" do
+      expect { process(:inline_image_2) }.to change { topic.posts.count }
+
+      post = topic.posts.last
+      upload = post.uploads.last
+
+      expect(post.raw).to eq(<<~MD.chomp)
+      [image:#{'0' * 5000}
+
+      ![#{upload.original_filename}|#{upload.width}x#{upload.height}](#{upload.short_url})
+      MD
+    end
+
     it "supports attached images in signature" do
       SiteSetting.incoming_email_prefer_html = true
       SiteSetting.always_show_trimmed_content = true
diff --git a/spec/fixtures/emails/inline_image_2.eml b/spec/fixtures/emails/inline_image_2.eml
new file mode 100644
index 0000000..cba266d
--- /dev/null
+++ b/spec/fixtures/emails/inline_image_2.eml
@@ -0,0 +1,26 @@
+Return-Path: <discourse@bar.com>
+From: Foo Bar <discourse@bar.com>
+To: reply+4f97315cc828096c9cb34c6f1a0d6fe8@bar.com
+Date: Fri, 15 Jan 2016 00:12:43 +0100
+Message-ID: <28@foo.bar.mail>
+Mime-Version: 1.0
+Content-Type: multipart/related; boundary="0000000000006d045f05c0e384eb"
+
+--0000000000006d045f05c0e384eb
+Content-Type: multipart/alternative; boundary="0000000000006d045b05c0e384ea"
+
+--0000000000006d045b05c0e384ea
+Content-Type: text/html; charset="UTF-8"
+
+[image:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+
+--0000000000006d045b05c0e384ea--
+--0000000000006d045f05c0e384eb
+Content-Type: image/png; name="poc.png"
+Content-Disposition: attachment; filename="poc.png"
+Content-Transfer-Encoding: base64
+X-Attachment-Id: ii_poc0
+Content-ID: <ii_poc0>
+
+iVBORw0KGgoAAAANSUhEUgAABm4AAAHTCAIAAAChr3DwAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAg
+--0000000000006d045f05c0e384eb--

GitHub sha: e6329d30

This commit appears in #12855 which was approved by eviltrout and ZogStriP. It was merged by nbianca.