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 @@
['[email protected]', '[email protected]', '[email protected]', '[email protected]',
'[email protected]', '1234567890-234567890-234567890-234567890-234567890-234567890-234567890-234567890-234567890@gmail.com',
'[email protected]', 'uucp%[email protected]', '[email protected]',
'[email protected]', '[email protected]'
'[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'
].each do |email_str|
it "'#{email_str}'" do
expect do
Expand All @@ -103,7 +103,7 @@
end

describe 'disallows illegitimate emails' do
['[email protected]', '[email protected]', '[email protected]', '[email protected]',
['[email protected]', '[email protected]', 'needdomain@', 'needtld@example', '@.com', '@example.com', '[email protected]',
'Iñtërnâtiônàlizætiø[email protected]', 'need.domain.and.tld@de',
'[email protected]', '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.