From a8d9695324a3e294384d1ca89b7db55baf229315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 29 Aug 2011 13:14:55 +0200 Subject: [PATCH] Simplify regexp and ensure it runs against UTF8 chars, closes #1235 --- CHANGELOG.rdoc | 4 ++++ lib/devise.rb | 9 ++++----- lib/devise/email.rb | 23 ----------------------- lib/generators/templates/devise.rb | 6 ++++-- test/models/validatable_test.rb | 5 +++-- 5 files changed, 15 insertions(+), 32 deletions(-) delete mode 100644 lib/devise/email.rb diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index c0e347b6..29c7fb13 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,5 +1,9 @@ * bug fix * Properly deprecate setup_mail + * Fix encoding issues with email regexp + +* deprecations + * Loosened the used email regexp to simply assert the existent of "@". If someone relies on a more strict regexp, they may use https://github.com/SixArm/sixarm_ruby_email_address_validation == 1.4.2 diff --git a/lib/devise.rb b/lib/devise.rb index ab464008..e1122053 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -11,7 +11,6 @@ module Devise autoload :PathChecker, 'devise/path_checker' autoload :Schema, 'devise/schema' autoload :TestHelpers, 'devise/test_helpers' - autoload :Email, 'devise/email' module Controllers autoload :Helpers, 'devise/controllers/helpers' @@ -105,11 +104,11 @@ module Devise mattr_accessor :http_authentication_realm @@http_authentication_realm = "Application" - # Email regex used to validate email formats. Based on RFC 822 and - # retrieved from Sixarm email validation gem - # (https://github.com/SixArm/sixarm_ruby_email_address_validation). + # Email regex used to validate email formats. It simply asserts that + # an one (and only one) @ exists in the given string. This is mainly + # to give user feedback and not to assert the e-mail validity. mattr_accessor :email_regexp - @@email_regexp = Devise::Email::EXACT_PATTERN + @@email_regexp = /\A[^@]+@[^@]+\z/ # Range validation for password length mattr_accessor :password_length diff --git a/lib/devise/email.rb b/lib/devise/email.rb deleted file mode 100644 index 520aac77..00000000 --- a/lib/devise/email.rb +++ /dev/null @@ -1,23 +0,0 @@ -# This e-mail validation regexes were retrieved from SixArm Ruby -# e-mail validation gem (https://github.com/SixArm/sixarm_ruby_email_address_validation) -# As said on https://github.com/SixArm/sixarm_ruby_email_address_validation/blob/master/LICENSE.txt, -# we added it using Ruby license terms. - -module Devise - module Email - QTEXT = Regexp.new '[^\\x0d\\x22\\x5c\\x80-\\xff]', nil, 'n' - DTEXT = Regexp.new '[^\\x0d\\x5b-\\x5d\\x80-\\xff]', nil, 'n' - ATOM = Regexp.new '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+', nil, 'n' - QUOTED_PAIR = Regexp.new '\\x5c[\\x00-\\x7f]', nil, 'n' - DOMAIN_LITERAL = Regexp.new "\\x5b(?:#{DTEXT}|#{QUOTED_PAIR})*\\x5d", nil, 'n' - QUOTED_STRING = Regexp.new "\\x22(?:#{QTEXT}|#{QUOTED_PAIR})*\\x22", nil, 'n' - DOMAIN_REF = ATOM - SUB_DOMAIN = "(?:#{DOMAIN_REF}|#{DOMAIN_LITERAL})" - WORD = "(?:#{ATOM}|#{QUOTED_STRING})" - DOMAIN = "#{SUB_DOMAIN}(?:\\x2e#{SUB_DOMAIN})*" - LOCAL_PART = "#{WORD}(?:\\x2e#{WORD})*" - SPEC = "#{LOCAL_PART}\\x40#{DOMAIN}" - PATTERN = Regexp.new "#{SPEC}", nil, 'n' - EXACT_PATTERN = Regexp.new "\\A#{SPEC}\\z", nil, 'n' - end -end \ No newline at end of file diff --git a/lib/generators/templates/devise.rb b/lib/generators/templates/devise.rb index c93dbd40..292dc949 100644 --- a/lib/generators/templates/devise.rb +++ b/lib/generators/templates/devise.rb @@ -105,8 +105,10 @@ Devise.setup do |config| # Range for password length. Default is 6..128. # config.password_length = 6..128 - # Regex to use to validate the email address - # config.email_regexp = /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i + # Email regex used to validate email formats. It simply asserts that + # an one (and only one) @ exists in the given string. This is mainly + # to give user feedback and not to assert the e-mail validity. + # config.email_regexp = /\A[^@]+@[^@]+\z/ # ==> Configuration for :timeoutable # The time you want to timeout the user session without activity. After this diff --git a/test/models/validatable_test.rb b/test/models/validatable_test.rb index df044bc1..668d25ba 100644 --- a/test/models/validatable_test.rb +++ b/test/models/validatable_test.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 require 'test_helper' class ValidatableTest < ActiveSupport::TestCase @@ -28,7 +29,7 @@ class ValidatableTest < ActiveSupport::TestCase assert user.invalid? assert_not_equal 'is invalid', user.errors[:email].join - %w(invalid_email_format 123 $$$ \(\) ).each do |email| + %w{invalid_email_format 123 $$$ () ☃}.each do |email| user.email = email assert user.invalid?, 'should be invalid with email ' << email assert_equal 'is invalid', user.errors[:email].join @@ -39,7 +40,7 @@ class ValidatableTest < ActiveSupport::TestCase end test 'should accept valid emails' do - %w(a.b.c@example.com test_mail@gmail.com any@any.net email@test.br 123@mail.test).each do |email| + %w(a.b.c@example.com test_mail@gmail.com any@any.net email@test.br 123@mail.test 1☃3@mail.test).each do |email| user = new_user(:email => email) assert user.valid?, 'should be valid with email ' << email assert_blank user.errors[:email]