Skip to content

Commit 397bb12

Browse files
osyoyumatzbot
authored andcommitted
[ruby/uri] Re-allow consecutive, leading and trailing dots in EMAIL_REGEXP
Effectively reverts commit ruby/uri@788274b180d6 and ruby/uri@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. ruby/uri@c551d7020b
1 parent 4478096 commit 397bb12

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

lib/uri/mailto.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ class MailTo < Generic
5252
HEADER_REGEXP = /\A(?<hfield>(?:%\h\h|[!$'-.0-;@-Z_a-z~])*=(?:%\h\h|[!$'-.0-;@-Z_a-z~])*)(?:&\g<hfield>)*\z/
5353
# practical regexp for email address
5454
# https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
55-
EMAIL_REGEXP = %r[\A#{
56-
atext = %q[(?:[a-zA-Z0-9!\#$%&'*+\/=?^_`{|}~-]+)]
57-
}(?:\.#{atext})*@#{
58-
label = %q[(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)]
59-
}(?:\.#{label})*\z]
55+
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/
6056
# :startdoc:
6157

6258
#

test/uri/test_mailto.rb

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,29 +145,23 @@ def test_check_to
145145
u.to = 'a@valid.com'
146146
assert_equal(u.to, 'a@valid.com')
147147

148-
# Invalid emails
149-
assert_raise(URI::InvalidComponentError) do
150-
u.to = '#1@mail.com'
151-
end
148+
# Intentionally allowed violations of RFC 5322
149+
u.to = 'a..a@valid.com'
150+
assert_equal(u.to, 'a..a@valid.com')
152151

153-
assert_raise(URI::InvalidComponentError) do
154-
u.to = '@invalid.email'
155-
end
152+
u.to = 'hello.@valid.com'
153+
assert_equal(u.to, 'hello.@valid.com')
156154

157-
assert_raise(URI::InvalidComponentError) do
158-
u.to = '.hello@invalid.email'
159-
end
160-
161-
assert_raise(URI::InvalidComponentError) do
162-
u.to = 'hello.@invalid.email'
163-
end
155+
u.to = '.hello@valid.com'
156+
assert_equal(u.to, '.hello@valid.com')
164157

158+
# Invalid emails
165159
assert_raise(URI::InvalidComponentError) do
166-
u.to = 'n.@invalid.email'
160+
u.to = '#1@mail.com'
167161
end
168162

169163
assert_raise(URI::InvalidComponentError) do
170-
u.to = 'n..t@invalid.email'
164+
u.to = '@invalid.email'
171165
end
172166

173167
# Invalid host emails

0 commit comments

Comments
 (0)