Commit c02112b9 authored by maeda's avatar maeda
Browse files

Optional regex for filtering attachments on incoming emails (#27025).

Patch by Jan Schulz-Hofen.


git-svn-id: https://svn.redmine.org/redmine/trunk@17225 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent 972764f2
......@@ -310,7 +310,11 @@ class MailHandler < ActionMailer::Base
def accept_attachment?(attachment)
@excluded ||= Setting.mail_handler_excluded_filenames.to_s.split(',').map(&:strip).reject(&:blank?)
@excluded.each do |pattern|
if Setting.mail_handler_enable_regex_excluded_filenames?
regexp = %r{\A#{pattern}\z}i
else
regexp = %r{\A#{Regexp.escape(pattern).gsub("\\*", ".*")}\z}i
end
if attachment.filename.to_s =~ regexp
logger.info "MailHandler: ignoring attachment #{attachment.filename} matching #{pattern}"
return false
......
......@@ -143,21 +143,26 @@ class Setting < ActiveRecord::Base
def self.validate_all_from_params(settings)
messages = []
if settings.key?(:mail_handler_body_delimiters) || settings.key?(:mail_handler_enable_regex_delimiters)
regexp = Setting.mail_handler_enable_regex_delimiters?
if settings.key?(:mail_handler_enable_regex_delimiters)
regexp = settings[:mail_handler_enable_regex_delimiters].to_s != '0'
[[:mail_handler_enable_regex_delimiters, :mail_handler_body_delimiters, /[\r\n]+/],
[:mail_handler_enable_regex_excluded_filenames, :mail_handler_excluded_filenames, /\s*,\s*/]
].each do |enable_regex, regex_field, delimiter|
if settings.key?(regex_field) || settings.key?(enable_regex)
regexp = Setting.send("#{enable_regex}?")
if settings.key?(enable_regex)
regexp = settings[enable_regex].to_s != '0'
end
if regexp
settings[:mail_handler_body_delimiters].to_s.split(/[\r\n]+/).each do |delimiter|
settings[regex_field].to_s.split(delimiter).each do |value|
begin
Regexp.new(delimiter)
Regexp.new(value)
rescue RegexpError => e
messages << [:mail_handler_body_delimiters, "#{l('activerecord.errors.messages.not_a_regexp')} (#{e.message})"]
messages << [regex_field, "#{l('activerecord.errors.messages.not_a_regexp')} (#{e.message})"]
end
end
end
end
end
messages
end
......
......@@ -5,12 +5,16 @@
<%= setting_text_area :mail_handler_body_delimiters, :rows => 5 %>
<label class="block">
<%= setting_check_box :mail_handler_enable_regex_delimiters, :label => false %>
<%= l(:setting_mail_handler_enable_regex_delimiters) %>
<%= l(:setting_mail_handler_enable_regex) %>
</label>
<em class="info"><%= l(:text_line_separated) %></em>
</p>
<p>
<%= setting_text_field :mail_handler_excluded_filenames, :size => 60 %>
<label class="block">
<%= setting_check_box :mail_handler_enable_regex_excluded_filenames, :label => false %>
<%= l(:setting_mail_handler_enable_regex) %>
</label>
<em class="info"><%= l(:text_comma_separated) %>
<%= l(:label_example) %>: smime.p7s, *.vcf</em>
</p>
......
......@@ -414,7 +414,7 @@ en:
setting_display_subprojects_issues: Display subprojects issues on main projects by default
setting_enabled_scm: Enabled SCM
setting_mail_handler_body_delimiters: "Truncate emails after one of these lines"
setting_mail_handler_enable_regex_delimiters: "Enable regular expressions"
setting_mail_handler_enable_regex: "Enable regular expressions"
setting_mail_handler_api_enabled: Enable WS for incoming emails
setting_mail_handler_api_key: Incoming email WS API key
setting_sys_api_key: Repository management WS API key
......
......@@ -185,6 +185,8 @@ mail_handler_body_delimiters:
default: ''
mail_handler_enable_regex_delimiters:
default: 0
mail_handler_enable_regex_excluded_filenames:
default: 0
mail_handler_excluded_filenames:
default: ''
mail_handler_api_enabled:
......
......@@ -1053,6 +1053,16 @@ class MailHandlerTest < ActiveSupport::TestCase
end
end
def test_attachments_that_match_mail_handler_excluded_filenames_by_regex_should_be_ignored
with_settings :mail_handler_excluded_filenames => '.+\.vcf,(pa|nut)ella\.jpg',
:mail_handler_enable_regex_excluded_filenames => 1 do
issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
assert issue.is_a?(Issue)
assert !issue.new_record?
assert_equal 0, issue.reload.attachments.size
end
end
def test_attachments_that_do_not_match_mail_handler_excluded_filenames_should_be_attached
with_settings :mail_handler_excluded_filenames => '*.vcf, *.gif' do
issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment