feature-31154.patch

Mizuki ISHIKAWA, 2019-07-09 06:56

Download (2.33 KB)

View differences:

app/models/email_address.rb
20 20
class EmailAddress < ActiveRecord::Base
21 21
  include Redmine::SafeAttributes
22 22

  
23
  EMAIL_REGEXP = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
24

  
23 25
  belongs_to :user
24 26

  
25 27
  after_update :destroy_tokens
......
30 32
  after_destroy_commit :deliver_security_notification_destroy
31 33

  
32 34
  validates_presence_of :address
33
  validates_format_of :address, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :allow_blank => true
35
  validates_format_of :address, :with => EMAIL_REGEXP, :allow_blank => true
34 36
  validates_length_of :address, :maximum => User::MAIL_LENGTH_LIMIT, :allow_nil => true
35 37
  validates_uniqueness_of :address, :case_sensitive => false,
36 38
    :if => Proc.new {|email| email.address_changed? && email.address.present?}
app/models/setting.rb
166 166
      end
167 167
    end
168 168

  
169
    if settings.key?(:mail_from)
170
      begin
171
        mail_from = Mail::Address.new(settings[:mail_from])
172
        raise unless mail_from.address =~ EmailAddress::EMAIL_REGEXP
173
      rescue
174
        messages << [:mail_from, l('activerecord.errors.messages.invalid')]
175
      end
176
    end
169 177
    messages
170 178
  end
171 179

  
test/unit/setting_test.rb
132 132
    Setting.where(:name => 'commit_update_keywords').delete_all
133 133
    Setting.clear_cache
134 134
  end
135

  
136
  def test_mail_from_format_should_be_validated
137
    with_settings :default_language => 'en' do
138
      ['[Redmine app] <redmine@example.net>', 'redmine'].each do |invalid_mail_from|
139
        errors = Setting.set_all_from_params({:mail_from => invalid_mail_from})
140
        assert_includes errors, [:mail_from, 'is invalid']
141
      end
142

  
143
      ['Redmine app <redmine@example.net>', 'redmine@example.net', '<redmine@example.net>'].each do |valid_mail_from|
144
        errors = Setting.set_all_from_params({:mail_from => valid_mail_from})
145
        assert_nil errors
146
      end
147
    end
148
  end
135 149
end