From cc684803bac964114319a39d52fd12fc84864a02 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 8 Nov 2009 13:51:53 +0000 Subject: [PATCH] Ignore emails received from the application emission address to avoid hell cycles (#4139). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3022 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/mail_handler.rb | 12 +++++++++--- .../ticket_from_emission_address.eml | 19 +++++++++++++++++++ test/unit/mail_handler_test.rb | 7 +++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/mail_handler/ticket_from_emission_address.eml diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb index 8686c6b01..23a51d83a 100644 --- a/app/models/mail_handler.rb +++ b/app/models/mail_handler.rb @@ -41,7 +41,13 @@ class MailHandler < ActionMailer::Base # Returns the created object (eg. an issue, a message) or false def receive(email) @email = email - @user = User.find_by_mail(email.from.to_a.first.to_s.strip) + sender_email = email.from.to_a.first.to_s.strip + # Ignore emails received from the application emission address to avoid hell cycles + if sender_email.downcase == Setting.mail_from.to_s.strip.downcase + logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" if logger && logger.info + return false + end + @user = User.find_by_mail(sender_email) if @user && !@user.active? logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" if logger && logger.info return false @@ -57,12 +63,12 @@ class MailHandler < ActionMailer::Base logger.info "MailHandler: [#{@user.login}] account created" if logger && logger.info Mailer.deliver_account_information(@user, @user.password) else - logger.error "MailHandler: could not create account for [#{email.from.first}]" if logger && logger.error + logger.error "MailHandler: could not create account for [#{sender_email}]" if logger && logger.error return false end else # Default behaviour, emails from unknown users are ignored - logger.info "MailHandler: ignoring email from unknown user [#{email.from.first}]" if logger && logger.info + logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]" if logger && logger.info return false end end diff --git a/test/fixtures/mail_handler/ticket_from_emission_address.eml b/test/fixtures/mail_handler/ticket_from_emission_address.eml new file mode 100644 index 000000000..bdb2d21b7 --- /dev/null +++ b/test/fixtures/mail_handler/ticket_from_emission_address.eml @@ -0,0 +1,19 @@ +Return-Path: +Received: from osiris ([127.0.0.1]) + by OSIRIS + with hMailServer ; Sun, 22 Jun 2008 12:28:07 +0200 +Message-ID: <000501c8d452$a95cd7e0$0a00a8c0@osiris> +From: "John Doe" +To: +Subject: Ticket with the Redmine emission address +Date: Sun, 22 Jun 2008 12:28:07 +0200 +MIME-Version: 1.0 +Content-Type: text/plain; + format=flowed; + charset="iso-8859-1"; + reply-type=original +Content-Transfer-Encoding: 7bit + +This is a ticket submitted with the Redmine emission address. +It should be ignored. + diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb index 9b6c28ba3..7e7dfa0c8 100644 --- a/test/unit/mail_handler_test.rb +++ b/test/unit/mail_handler_test.rb @@ -185,6 +185,13 @@ class MailHandlerTest < ActiveSupport::TestCase assert_equal false, submit_email('ticket_without_from_header.eml') end + def test_should_ignore_emails_from_emission_address + Role.anonymous.add_permission!(:add_issues) + assert_no_difference 'User.count' do + assert_equal false, submit_email('ticket_from_emission_address.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create') + end + end + def test_add_issue_should_send_email_notification ActionMailer::Base.deliveries.clear # This email contains: 'Project: onlinestore' -- 2.39.5