Skip to content

Commit c782629

Browse files
committed
Don't include the domain name in exception message text before its syntax is validated
The domain name might have unsafe characters. In any case, the user probably knows what they entered and doesn't need it repeated back to them.
1 parent 75c2136 commit c782629

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

email_validator/syntax.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def validate_email_domain_part(domain, test_environment=False, globally_delivera
128128
try:
129129
domain = idna.uts46_remap(domain, std3_rules=False, transitional=False)
130130
except idna.IDNAError as e:
131-
raise EmailSyntaxError("The domain name %s contains invalid characters (%s)." % (domain, str(e)))
131+
raise EmailSyntaxError("The part after the @-sign contains invalid characters ({}).".format(str(e)))
132132

133133
# Now we can perform basic checks on the use of periods (since equivalent
134134
# symbols have been mapped to periods). These checks are needed because the
@@ -166,7 +166,7 @@ def validate_email_domain_part(domain, test_environment=False, globally_delivera
166166
# one the user supplied. Also I'm not sure if the length check applies
167167
# to the internationalized form, the IDNA ASCII form, or even both!
168168
raise EmailSyntaxError("The email address is too long after the @-sign.")
169-
raise EmailSyntaxError("The domain name %s contains invalid characters (%s)." % (domain, str(e)))
169+
raise EmailSyntaxError("The part after the @-sign contains invalid characters (%s)." % str(e))
170170

171171
# Check the syntax of the string returned by idna.encode.
172172
# It should never fail.
@@ -186,7 +186,7 @@ def validate_email_domain_part(domain, test_environment=False, globally_delivera
186186
for label in ascii_domain.split("."):
187187
if len(label) > DNS_LABEL_LENGTH_LIMIT:
188188
reason = get_length_reason(label, limit=DNS_LABEL_LENGTH_LIMIT)
189-
raise EmailSyntaxError("The part of the email address \"{}\" is too long {}.".format(label, reason))
189+
raise EmailSyntaxError("On either side of the @-sign, periods cannot be separated by so many characters {}.".format(reason))
190190

191191
if globally_deliverable:
192192
# All publicly deliverable addresses have domain named with at least
@@ -223,7 +223,7 @@ def validate_email_domain_part(domain, test_environment=False, globally_delivera
223223
try:
224224
domain_i18n = idna.decode(ascii_domain.encode('ascii'))
225225
except idna.IDNAError as e:
226-
raise EmailSyntaxError("The domain name %s is not valid IDNA (%s)." % (ascii_domain, str(e)))
226+
raise EmailSyntaxError("The part after the @-sign is not valid IDNA ({}).".format(str(e)))
227227

228228
# Return the IDNA ASCII-encoded form of the domain, which is how it
229229
# would be transmitted on the wire (except when used with SMTPUTF8

tests/test_main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,22 +218,22 @@ def test_email_valid(email_input, output):
218218
('my@..twodots.com', 'An email address cannot have a period immediately after the @-sign.'),
219219
('my@twodots..com', 'An email address cannot have two periods in a row.'),
220220
('my@baddash.-.com',
221-
'The domain name baddash.-.com contains invalid characters (Label must not start or end with a hyphen).'),
221+
'The part after the @-sign contains invalid characters (Label must not start or end with a hyphen).'),
222222
('my@baddash.-a.com',
223-
'The domain name baddash.-a.com contains invalid characters (Label must not start or end with a hyphen).'),
223+
'The part after the @-sign contains invalid characters (Label must not start or end with a hyphen).'),
224224
('my@baddash.b-.com',
225-
'The domain name baddash.b-.com contains invalid characters (Label must not start or end with a hyphen).'),
225+
'The part after the @-sign contains invalid characters (Label must not start or end with a hyphen).'),
226226
('my@example.com\n',
227-
'The domain name example.com\n contains invalid characters (Codepoint U+000A at position 4 of '
227+
'The part after the @-sign contains invalid characters (Codepoint U+000A at position 4 of '
228228
'\'com\\n\' not allowed).'),
229229
('my@example\n.com',
230-
'The domain name example\n.com contains invalid characters (Codepoint U+000A at position 8 of '
230+
'The part after the @-sign contains invalid characters (Codepoint U+000A at position 8 of '
231231
'\'example\\n\' not allowed).'),
232232
('.leadingdot@domain.com', 'The email address contains invalid characters before the @-sign: FULL STOP.'),
233233
('..twodots@domain.com', 'The email address contains invalid characters before the @-sign: FULL STOP.'),
234234
('twodots..here@domain.com', 'The email address contains invalid characters before the @-sign: FULL STOP.'),
235235
('me@⒈wouldbeinvalid.com',
236-
"The domain name ⒈wouldbeinvalid.com contains invalid characters (Codepoint U+2488 not allowed "
236+
"The part after the @-sign contains invalid characters (Codepoint U+2488 not allowed "
237237
"at position 1 in '⒈wouldbeinvalid.com')."),
238238
('@example.com', 'There must be something before the @-sign.'),
239239
('\nmy@example.com', 'The email address contains invalid characters before the @-sign: \'\\n\'.'),

0 commit comments

Comments
 (0)