Skip to content

Commit

Permalink
fix: Change email validation regex [PT-187054507]
Browse files Browse the repository at this point in the history
Removed whitelist of TLDs and replaced with a check for at least two letters.
  • Loading branch information
dougmartin committed Feb 15, 2024
1 parent 10d4be2 commit a634603
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion rails/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def self.name_regex

email_name_regex = '[\w\.%\+\-\']+'.freeze
domain_head_regex = '(?:[A-Z0-9\-]+\.)+'.freeze
domain_tld_regex = '(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)'.freeze
domain_tld_regex = '(?:[A-Z]{2,})'.freeze
email_regex = /\A#{email_name_regex}@#{domain_head_regex}#{domain_tld_regex}\z/i
bad_email_message = "should look like an email address.".freeze

Expand Down
4 changes: 2 additions & 2 deletions rails/spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
['foo@bar.com', 'foo@newskool-tld.museum', 'foo@twoletter-tld.de', 'foo@nonexistant-tld.qq',
'r@a.wk', '1234567890-234567890-234567890-234567890-234567890-234567890-234567890-234567890-234567890@gmail.com',
'hello.-_there@funnychar.com', 'uucp%addr@gmail.com', 'hello+routing-str@gmail.com',
'domain@can.haz.many.sub.doma.in', 'student.name@university.edu'
'domain@can.haz.many.sub.doma.in', 'student.name@university.edu', 'foo@anytld.xxx', 'foo@longtld.abcdefg', 'teacher@some.school'
].each do |email_str|
it "'#{email_str}'" do
expect do
Expand All @@ -103,7 +103,7 @@
end

describe 'disallows illegitimate emails' do
['!!@nobadchars.com', 'foo@no-rep-dots..com', 'foo@badtld.xxx', 'foo@toolongtld.abcdefg',
['!!@nobadchars.com', 'foo@no-rep-dots..com', 'needdomain@', 'needtld@example', '@.com', '@example.com', 'oneletterltd@example.x',
'Iñtërnâtiônàlizætiøn@hasnt.happened.to.email', 'need.domain.and.tld@de',
'r@.wk', '1234567890-234567890-234567890-234567890-234567890-234567890-234567890-234567890-234567890@gmail2.com',
# these are technically allowed but not seen in practice:
Expand Down

0 comments on commit a634603

Please sign in to comment.