Simplify regexp and ensure it runs against UTF8 chars, closes #1235

This commit is contained in:
José Valim 2011-08-29 13:14:55 +02:00
parent 4f76e6f577
commit a8d9695324
5 changed files with 15 additions and 32 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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]