Commit 05e4af5b4c7709ab08194d109ddec8e19f44758d

Authored by Jeroen van Baarsen
1 parent dba98240

Better check on the validity of emails

At this moment it was possible to enter emails like:
mailto:info@example.com. This was causing some issue in the frontend,
since those links became html mailto: links.

Fixes: #3516
Gemfile
... ... @@ -52,6 +52,9 @@ gem "grape", "~> 0.6.1"
52 52 gem "grape-entity", "~> 0.3.0"
53 53 gem 'rack-cors', require: 'rack/cors'
54 54  
  55 +# Email validation
  56 +gem "email_validator", "~> 1.4.0", :require => 'email_validator/strict'
  57 +
55 58 # Format dates and times
56 59 # based on human-friendly examples
57 60 gem "stamp"
... ...
Gemfile.lock
... ... @@ -114,6 +114,8 @@ GEM
114 114 email_spec (1.5.0)
115 115 launchy (~> 2.1)
116 116 mail (~> 2.2)
  117 + email_validator (1.4.0)
  118 + activemodel
117 119 enumerize (0.7.0)
118 120 activesupport (>= 3.2)
119 121 equalizer (0.0.8)
... ... @@ -567,6 +569,7 @@ DEPENDENCIES
567 569 devise (= 3.0.4)
568 570 devise-async (= 0.8.0)
569 571 email_spec
  572 + email_validator (~> 1.4.0)
570 573 enumerize
571 574 factory_girl_rails
572 575 ffaker
... ...
app/models/user.rb
... ... @@ -103,7 +103,7 @@ class User < ActiveRecord::Base
103 103 # Validations
104 104 #
105 105 validates :name, presence: true
106   - validates :email, presence: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/ }, uniqueness: true
  106 + validates :email, presence: true, email: {strict_mode: true}, uniqueness: true
107 107 validates :bio, length: { maximum: 255 }, allow_blank: true
108 108 validates :extern_uid, allow_blank: true, uniqueness: {scope: :provider}
109 109 validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0}
... ...
spec/models/user_spec.rb
... ... @@ -74,6 +74,27 @@ describe User do
74 74 it { should_not allow_value(-1).for(:projects_limit) }
75 75  
76 76 it { should ensure_length_of(:bio).is_within(0..255) }
  77 +
  78 + describe 'email' do
  79 + it 'accepts info@example.com' do
  80 + user = build(:user, email: 'info@example.com')
  81 + expect(user).to be_valid
  82 + end
  83 + it 'accepts info+test@example.com' do
  84 + user = build(:user, email: 'info+test@example.com')
  85 + expect(user).to be_valid
  86 + end
  87 +
  88 + it 'rejects test@test@example.com' do
  89 + user = build(:user, email: 'test@test@example.com')
  90 + expect(user).to be_invalid
  91 + end
  92 +
  93 + it 'rejects mailto:test@example.com' do
  94 + user = build(:user, email: 'mailto:test@example.com')
  95 + expect(user).to be_invalid
  96 + end
  97 + end
77 98 end
78 99  
79 100 describe "Respond to" do
... ...