Browse Source

Fixed issue #2254 - Unable to process spam email `"ERROR: #<Exceptions::UnprocessableEntity: Invalid email>"`.

Martin Edenhofer 6 years ago
parent
commit
e31d2751cf

+ 3 - 1
app/models/channel/filter/identify_sender.rb

@@ -156,7 +156,7 @@ module Channel::Filter::IdentifySender
   end
 
   def self.populate_attributes!(attrs, **extras)
-    if attrs[:email].match?(/\S\s+\S/)
+    if attrs[:email].match?(/\S\s+\S/) || attrs[:email].match?(/^<|>$/)
       attrs[:preferences] = { mail_delivery_failed: true,
                               mail_delivery_failed_reason: 'invalid email',
                               mail_delivery_failed_data: Time.zone.now }
@@ -190,6 +190,8 @@ module Channel::Filter::IdentifySender
     string.downcase
           .strip
           .delete('"')
+          .delete(' ')             # see https://github.com/zammad/zammad/issues/2254
+          .sub(/^<|>$/, '')        # see https://github.com/zammad/zammad/issues/2254
           .sub(/\A'(.*)'\z/, '\1') # see https://github.com/zammad/zammad/issues/2154
           .gsub(/\s/, '')          # see https://github.com/zammad/zammad/issues/2198
   end

+ 20 - 0
spec/models/channel/email_parser_spec.rb

@@ -71,6 +71,26 @@ RSpec.describe Channel::EmailParser, type: :model do
       end
     end
 
+    # see https://github.com/zammad/zammad/issues/2254
+    context 'when sender address contains > (#2254)' do
+      let(:mail_file) { Rails.root.join('test', 'data', 'mail', 'mail076.box') }
+      let(:sender_email) { 'millionslotteryspaintransfer@example.com' }
+
+      it 'removes them before creating a new user' do
+        expect { described_class.new.process({}, raw_mail) }
+          .to change { User.where(email: sender_email).count }.to(1)
+      end
+
+      it 'marks new user email as invalid' do
+        described_class.new.process({}, raw_mail)
+
+        expect(User.find_by(email: sender_email).preferences)
+          .to include('mail_delivery_failed' => true)
+          .and include('mail_delivery_failed_reason' => 'invalid email')
+          .and include('mail_delivery_failed_data' => a_kind_of(ActiveSupport::TimeWithZone))
+      end
+    end
+
     # see https://github.com/zammad/zammad/issues/2224
     context 'when header specifies Windows-1258 charset (#2224)' do
       let(:mail_file) { Rails.root.join('test', 'data', 'mail', 'mail072.box') }

+ 7 - 0
test/data/mail/mail076.box

@@ -0,0 +1,7 @@
+From: "Millions Lottery Spain transfer"@example.com>
+To: info@example.de
+Subject: [SPAM] (19.9) Your Winning Notification!!!
+Date: 24 Sep 2018 10:22:54 -0700
+Message-ID: <20180924102254.B52A3EAFDF0F87BA@example.com>
+
+test spam

+ 9 - 0
test/data/mail/mail076.yml

@@ -0,0 +1,9 @@
+--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
+from: '"Millions Lottery Spain transfer"@example.com>'
+from_email: '"Millions Lottery Spain transfer"@example.com>'
+from_display_name: Millions Lottery Spain transfer@example.com>
+to: info@example.de
+subject: "[SPAM] (19.9) Your Winning Notification!!!"
+body: test spam
+content_type: text/plain
+attachments: []

+ 21 - 0
test/unit/email_process_test.rb

@@ -3115,6 +3115,27 @@ Content-Type: text/html; charset=us-ascii; format=flowed
           ],
         },
       },
+      { # See https://github.com/zammad/zammad/issues/2254
+        data: File.read(Rails.root.join('test', 'data', 'mail', 'mail076.box')),
+        success: true,
+        result: {
+          1 => {
+            from: '"Millions Lottery Spain transfer"@example.com>',
+            sender: 'Customer',
+            type: 'email',
+          },
+        },
+        verify: {
+          users: [
+            {
+              firstname: 'Millions',
+              lastname: 'Lottery Spain transfer@example.com>',
+              fullname: 'Millions Lottery Spain transfer@example.com>',
+              email: 'millionslotteryspaintransfer@example.com',
+            },
+          ],
+        },
+      },
     ]
     assert_process(files)
   end