Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d1a0c03

Browse files
Tomas Weinfurtvseanreesermsft
authored andcommitted
Merged PR 22033: [release/3.1] MSRC 68590 - newlines in domain literals
block embedded CRLF by default. Cherry picked from !21713 Cherry-picked from commit `a3434781`.
2 parents a18dd8b + 4f6b8ec commit d1a0c03

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/EmailAddressAttribute.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ namespace System.ComponentModel.DataAnnotations
88
AllowMultiple = false)]
99
public sealed class EmailAddressAttribute : DataTypeAttribute
1010
{
11+
private static readonly char[] s_newLines = new char[] { '\r', '\n' };
12+
private static bool s_allowFullDomainLiterals =
13+
AppContext.TryGetSwitch("System.Net.AllowFullDomainLiterals", out bool enable) ? enable : false;
14+
1115
public EmailAddressAttribute()
1216
: base(DataType.EmailAddress)
1317
{
@@ -28,6 +32,11 @@ public override bool IsValid(object value)
2832
return false;
2933
}
3034

35+
if (!s_allowFullDomainLiterals && valueAsString.IndexOfAny(s_newLines) >= 0)
36+
{
37+
return false;
38+
}
39+
3140
// only return true if there is only 1 '@' character
3241
// and it is neither the first nor the last character
3342
int index = valueAsString.IndexOf('@');

src/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/EmailAddressAttributeTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected override IEnumerable<TestCase> InvalidValues()
3535
yield return new TestCase(new EmailAddressAttribute(), "someName");
3636
yield return new TestCase(new EmailAddressAttribute(), "someName@");
3737
yield return new TestCase(new EmailAddressAttribute(), "someName@[email protected]");
38+
yield return new TestCase(new EmailAddressAttribute(), "someName@[\r\n\tsomeDomain]");
3839
}
3940

4041
[Fact]

src/System.Net.Mail/src/System/Net/Mail/MailAddress.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ namespace System.Net.Mail
1515
//
1616
public partial class MailAddress
1717
{
18+
private static readonly char[] s_newLines = new char[] { '\r', '\n' };
19+
private static bool s_allowFullDomainLiterals =
20+
AppContext.TryGetSwitch("System.Net.AllowFullDomainLiterals", out bool enable) ? enable : false;
21+
1822
// These components form an e-mail address when assembled as follows:
1923
// "EncodedDisplayname" <userName@host>
2024
private readonly Encoding _displayNameEncoding;
@@ -152,6 +156,12 @@ private string GetHost(bool allowUnicode)
152156
throw new SmtpException(SR.Format(SR.SmtpInvalidHostName, Address), argEx);
153157
}
154158
}
159+
160+
if (!s_allowFullDomainLiterals && domain.IndexOfAny(s_newLines) >= 0)
161+
{
162+
throw new SmtpException(SR.Format(SR.SmtpInvalidHostName, Address));
163+
}
164+
155165
return domain;
156166
}
157167

0 commit comments

Comments
 (0)