Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Merged PR 22033: [release/3.1] MSRC 68590 - newlines in domain literals
Browse files Browse the repository at this point in the history
block embedded CRLF by default.

Cherry picked from !21713

Cherry-picked from commit `a3434781`.
  • Loading branch information
Tomas Weinfurt authored and vseanreesermsft committed Mar 30, 2022
2 parents a18dd8b + 4f6b8ec commit d1a0c03
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace System.ComponentModel.DataAnnotations
AllowMultiple = false)]
public sealed class EmailAddressAttribute : DataTypeAttribute
{
private static readonly char[] s_newLines = new char[] { '\r', '\n' };
private static bool s_allowFullDomainLiterals =
AppContext.TryGetSwitch("System.Net.AllowFullDomainLiterals", out bool enable) ? enable : false;

public EmailAddressAttribute()
: base(DataType.EmailAddress)
{
Expand All @@ -28,6 +32,11 @@ public override bool IsValid(object value)
return false;
}

if (!s_allowFullDomainLiterals && valueAsString.IndexOfAny(s_newLines) >= 0)
{
return false;
}

// only return true if there is only 1 '@' character
// and it is neither the first nor the last character
int index = valueAsString.IndexOf('@');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected override IEnumerable<TestCase> InvalidValues()
yield return new TestCase(new EmailAddressAttribute(), "someName");
yield return new TestCase(new EmailAddressAttribute(), "someName@");
yield return new TestCase(new EmailAddressAttribute(), "someName@[email protected]");
yield return new TestCase(new EmailAddressAttribute(), "someName@[\r\n\tsomeDomain]");
}

[Fact]
Expand Down
10 changes: 10 additions & 0 deletions src/System.Net.Mail/src/System/Net/Mail/MailAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ namespace System.Net.Mail
//
public partial class MailAddress
{
private static readonly char[] s_newLines = new char[] { '\r', '\n' };
private static bool s_allowFullDomainLiterals =
AppContext.TryGetSwitch("System.Net.AllowFullDomainLiterals", out bool enable) ? enable : false;

// These components form an e-mail address when assembled as follows:
// "EncodedDisplayname" <userName@host>
private readonly Encoding _displayNameEncoding;
Expand Down Expand Up @@ -152,6 +156,12 @@ private string GetHost(bool allowUnicode)
throw new SmtpException(SR.Format(SR.SmtpInvalidHostName, Address), argEx);
}
}

if (!s_allowFullDomainLiterals && domain.IndexOfAny(s_newLines) >= 0)
{
throw new SmtpException(SR.Format(SR.SmtpInvalidHostName, Address));
}

return domain;
}

Expand Down

0 comments on commit d1a0c03

Please sign in to comment.