diff options
| author | Daisuke Aritomo <osyoyu@osyoyu.com> | 2025-11-04 14:34:31 +0900 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2025-11-04 06:45:16 +0000 |
| commit | 397bb12778dd8940572e3fc4156085f1ca57f056 (patch) | |
| tree | 3f1bbdd424a46024ad3a9cf6b0121183c5bbd50e | |
| parent | 447809658afa851b6523397103c2fce74c4d5562 (diff) | |
[ruby/uri] Re-allow consecutive, leading and trailing dots in EMAIL_REGEXP
Effectively reverts commit https://github.com/ruby/uri/commit/788274b180d6 and
https://github.com/ruby/uri/commit/0abac721d8fe.
EMAIL_REGEXP was mostly drawn from WHATWG HTML LS. This spec states that
it intentionally violates RFC 5322 to provide a practical regex for
validation.
> This requirement is a willful violation of RFC 5322, which defines a
> syntax for email addresses that is simultaneously too strict (before the
> "@" character), too vague (after the "@" character), and too lax
> (allowing comments, whitespace characters, and quoted strings in manners
> unfamiliar to most users) to be of practical use here.
The allowing of consecutive dot s(`a..a@`) and leading/trailing dots
(`.a@`, `a.@`) is not the only derivation from RFC 5322. If a truly RFC
5322-compliant regexp is needed, tt should be organized under a
different name, since too much departure from the original EMAIL_REGEXP
must be introduced.
https://github.com/ruby/uri/commit/c551d7020b
| -rw-r--r-- | lib/uri/mailto.rb | 6 | ||||
| -rw-r--r-- | test/uri/test_mailto.rb | 26 |
2 files changed, 11 insertions, 21 deletions
diff --git a/lib/uri/mailto.rb b/lib/uri/mailto.rb index f747b79ec7..cb8024f301 100644 --- a/lib/uri/mailto.rb +++ b/lib/uri/mailto.rb @@ -52,11 +52,7 @@ module URI HEADER_REGEXP = /\A(?<hfield>(?:%\h\h|[!$'-.0-;@-Z_a-z~])*=(?:%\h\h|[!$'-.0-;@-Z_a-z~])*)(?:&\g<hfield>)*\z/ # practical regexp for email address # https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address - EMAIL_REGEXP = %r[\A#{ - atext = %q[(?:[a-zA-Z0-9!\#$%&'*+\/=?^_`{|}~-]+)] - }(?:\.#{atext})*@#{ - label = %q[(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)] - }(?:\.#{label})*\z] + EMAIL_REGEXP = /\A[a-zA-Z0-9.!\#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\z/ # :startdoc: # diff --git a/test/uri/test_mailto.rb b/test/uri/test_mailto.rb index 59bb5ded09..6cd3352978 100644 --- a/test/uri/test_mailto.rb +++ b/test/uri/test_mailto.rb @@ -145,29 +145,23 @@ class URI::TestMailTo < Test::Unit::TestCase u.to = 'a@valid.com' assert_equal(u.to, 'a@valid.com') - # Invalid emails - assert_raise(URI::InvalidComponentError) do - u.to = '#1@mail.com' - end + # Intentionally allowed violations of RFC 5322 + u.to = 'a..a@valid.com' + assert_equal(u.to, 'a..a@valid.com') - assert_raise(URI::InvalidComponentError) do - u.to = '@invalid.email' - end + u.to = 'hello.@valid.com' + assert_equal(u.to, 'hello.@valid.com') - assert_raise(URI::InvalidComponentError) do - u.to = '.hello@invalid.email' - end - - assert_raise(URI::InvalidComponentError) do - u.to = 'hello.@invalid.email' - end + u.to = '.hello@valid.com' + assert_equal(u.to, '.hello@valid.com') + # Invalid emails assert_raise(URI::InvalidComponentError) do - u.to = 'n.@invalid.email' + u.to = '#1@mail.com' end assert_raise(URI::InvalidComponentError) do - u.to = 'n..t@invalid.email' + u.to = '@invalid.email' end # Invalid host emails |
