This repository was archived by the owner on Jan 23, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +20
-0
lines changed
System.ComponentModel.Annotations
src/System/ComponentModel/DataAnnotations
tests/System/ComponentModel/DataAnnotations
System.Net.Mail/src/System/Net/Mail Expand file tree Collapse file tree 3 files changed +20
-0
lines changed Original file line number Diff line number Diff 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 ( '@' ) ;
Original file line number Diff line number Diff 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@a@b.com" ) ;
38+ yield return new TestCase ( new EmailAddressAttribute ( ) , "someName@[\r \n \t someDomain]" ) ;
3839 }
3940
4041 [ Fact ]
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments