Patch #27025 » 0001-Optional-regex-for-filtering-attachments-on-incoming.patch
app/models/mail_handler.rb | ||
---|---|---|
309 | 309 |
def accept_attachment?(attachment) |
310 | 310 |
@excluded ||= Setting.mail_handler_excluded_filenames.to_s.split(',').map(&:strip).reject(&:blank?) |
311 | 311 |
@excluded.each do |pattern| |
312 |
if Setting.mail_handler_enable_regex_excluded_filenames? |
|
313 |
regexp = %r{\A#{pattern}\z}i |
|
314 |
else |
|
312 | 315 |
regexp = %r{\A#{Regexp.escape(pattern).gsub("\\*", ".*")}\z}i |
316 |
end |
|
313 | 317 |
if attachment.filename.to_s =~ regexp |
314 | 318 |
logger.info "MailHandler: ignoring attachment #{attachment.filename} matching #{pattern}" |
315 | 319 |
return false |
app/models/setting.rb | ||
---|---|---|
143 | 143 |
def self.validate_all_from_params(settings) |
144 | 144 |
messages = [] |
145 | 145 | |
146 |
if settings.key?(:mail_handler_body_delimiters) || settings.key?(:mail_handler_enable_regex_delimiters) |
|
147 |
regexp = Setting.mail_handler_enable_regex_delimiters? |
|
148 |
if settings.key?(:mail_handler_enable_regex_delimiters) |
|
149 |
regexp = settings[:mail_handler_enable_regex_delimiters].to_s != '0' |
|
146 |
[[:mail_handler_enable_regex_delimiters, :mail_handler_body_delimiters, /[\r\n]+/], |
|
147 |
[:mail_handler_enable_regex_excluded_filenames, :mail_handler_excluded_filenames, /\s*,\s*/] |
|
148 |
].each do |enable_regex, regex_field, delimiter| |
|
149 | ||
150 |
if settings.key?(regex_field) || settings.key?(enable_regex) |
|
151 |
regexp = Setting.send("#{enable_regex}?") |
|
152 |
if settings.key?(enable_regex) |
|
153 |
regexp = settings[enable_regex].to_s != '0' |
|
150 | 154 |
end |
151 | 155 |
if regexp |
152 |
settings[:mail_handler_body_delimiters].to_s.split(/[\r\n]+/).each do |delimiter|
|
|
156 |
settings[regex_field].to_s.split(delimiter).each do |value|
|
|
153 | 157 |
begin |
154 |
Regexp.new(delimiter)
|
|
158 |
Regexp.new(value)
|
|
155 | 159 |
rescue RegexpError => e |
156 |
messages << [:mail_handler_body_delimiters, "#{l('activerecord.errors.messages.not_a_regexp')} (#{e.message})"]
|
|
160 |
messages << [regex_field, "#{l('activerecord.errors.messages.not_a_regexp')} (#{e.message})"]
|
|
157 | 161 |
end |
158 | 162 |
end |
159 | 163 |
end |
160 | 164 |
end |
165 |
end |
|
161 | 166 | |
162 | 167 |
messages |
163 | 168 |
end |
app/views/settings/_mail_handler.html.erb | ||
---|---|---|
5 | 5 |
<%= setting_text_area :mail_handler_body_delimiters, :rows => 5 %> |
6 | 6 |
<label class="block"> |
7 | 7 |
<%= setting_check_box :mail_handler_enable_regex_delimiters, :label => false %> |
8 |
<%= l(:setting_mail_handler_enable_regex_delimiters) %>
|
|
8 |
<%= l(:setting_mail_handler_enable_regex) %> |
|
9 | 9 |
</label> |
10 | 10 |
<em class="info"><%= l(:text_line_separated) %></em> |
11 | 11 |
</p> |
12 | 12 |
<p> |
13 | 13 |
<%= setting_text_field :mail_handler_excluded_filenames, :size => 60 %> |
14 |
<label class="block"> |
|
15 |
<%= setting_check_box :mail_handler_enable_regex_excluded_filenames, :label => false %> |
|
16 |
<%= l(:setting_mail_handler_enable_regex) %> |
|
17 |
</label> |
|
14 | 18 |
<em class="info"><%= l(:text_comma_separated) %> |
15 | 19 |
<%= l(:label_example) %>: smime.p7s, *.vcf</em> |
16 | 20 |
</p> |
config/locales/en.yml | ||
---|---|---|
415 | 415 |
setting_display_subprojects_issues: Display subprojects issues on main projects by default |
416 | 416 |
setting_enabled_scm: Enabled SCM |
417 | 417 |
setting_mail_handler_body_delimiters: "Truncate emails after one of these lines" |
418 |
setting_mail_handler_enable_regex_delimiters: "Enable regular expressions"
|
|
418 |
setting_mail_handler_enable_regex: "Enable regular expressions" |
|
419 | 419 |
setting_mail_handler_api_enabled: Enable WS for incoming emails |
420 | 420 |
setting_mail_handler_api_key: Incoming email WS API key |
421 | 421 |
setting_sys_api_key: Repository management WS API key |
config/settings.yml | ||
---|---|---|
187 | 187 |
default: '' |
188 | 188 |
mail_handler_enable_regex_delimiters: |
189 | 189 |
default: 0 |
190 |
mail_handler_enable_regex_excluded_filenames: |
|
191 |
default: 0 |
|
190 | 192 |
mail_handler_excluded_filenames: |
191 | 193 |
default: '' |
192 | 194 |
mail_handler_api_enabled: |
test/unit/mail_handler_test.rb | ||
---|---|---|
1045 | 1045 |
end |
1046 | 1046 |
end |
1047 | 1047 | |
1048 |
def test_attachments_that_match_mail_handler_excluded_filenames_by_regex_should_be_ignored |
|
1049 |
with_settings :mail_handler_excluded_filenames => '.+\.vcf,(pa|nut)ella\.jpg', |
|
1050 |
:mail_handler_enable_regex_excluded_filenames => 1 do |
|
1051 |
issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'}) |
|
1052 |
assert issue.is_a?(Issue) |
|
1053 |
assert !issue.new_record? |
|
1054 |
assert_equal 0, issue.reload.attachments.size |
|
1055 |
end |
|
1056 |
end |
|
1057 | ||
1048 | 1058 |
def test_attachments_that_do_not_match_mail_handler_excluded_filenames_should_be_attached |
1049 | 1059 |
with_settings :mail_handler_excluded_filenames => '*.vcf, *.gif' do |
1050 | 1060 |
issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'}) |