-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged PR 21497: [release/6.0] MSRC 68590 - newlines in domain literals
This add validation for embedded newlines in email addresses. Based on https://dev.azure.com/dnceng/internal/_git/dotnet-runtime/pullrequest/20738 There is opt-in System.Net.Mail.EnableFullDomainLiterals switch to allow previous behavior
- Loading branch information
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ protected override IEnumerable<TestCase> InvalidValues() | |
yield return new TestCase(new EmailAddressAttribute(), 0); | ||
yield return new TestCase(new EmailAddressAttribute(), ""); | ||
yield return new TestCase(new EmailAddressAttribute(), " \r \t \n" ); | ||
yield return new TestCase(new EmailAddressAttribute(), "someName@[\r\n\tsomeDomain]"); | ||
yield return new TestCase(new EmailAddressAttribute(), "@someDomain.com"); | ||
yield return new TestCase(new EmailAddressAttribute(), "@[email protected]"); | ||
yield return new TestCase(new EmailAddressAttribute(), "someName"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,15 @@ | |
// (C) 2006 John Luke | ||
// | ||
|
||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Net.NetworkInformation; | ||
using System.Net.Sockets; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.DotNet.RemoteExecutor; | ||
using Systen.Net.Mail.Tests; | ||
using Xunit; | ||
|
||
|
@@ -523,5 +527,60 @@ public async Task SendMail_SendQUITOnDispose(bool asyncSend) | |
quitReceived.Wait(TimeSpan.FromSeconds(30)); | ||
Assert.True(quitMessageReceived, "QUIT message not received"); | ||
} | ||
|
||
[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] | ||
[InlineData("foo@[\r\n bar]")] | ||
[InlineData("foo@[bar\r\n ]")] | ||
[InlineData("foo@[bar\r\n baz]")] | ||
public void MultiLineDomainLiterals_Enabled_Success(string input) | ||
{ | ||
RemoteExecutor.Invoke(static (string @input) => | ||
{ | ||
AppContext.SetSwitch("System.Net.AllowFullDomainLiterals", true); | ||
|
||
var address = new MailAddress(@input); | ||
|
||
// Using address with new line breaks the protocol so we cannot easily use LoopbackSmtpServer | ||
// Instead we call internal method that does the extra validation. | ||
string? host = (string?)typeof(MailAddress).InvokeMember("GetAddress", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, address, new object[] { true }); | ||
Assert.Equal(input, host); | ||
}, input).Dispose(); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(SendMail_MultiLineDomainLiterals_Data))] | ||
public async Task SendMail_MultiLineDomainLiterals_Disabled_Throws(string from, string to, bool asyncSend) | ||
{ | ||
using var server = new LoopbackSmtpServer(); | ||
|
||
using SmtpClient client = server.CreateClient(); | ||
client.Credentials = new NetworkCredential("Foo", "Bar"); | ||
|
||
using var msg = new MailMessage(@from, @to, "subject", "body"); | ||
|
||
await Assert.ThrowsAsync<SmtpException>(async () => | ||
{ | ||
if (asyncSend) | ||
{ | ||
await client.SendMailAsync(msg).WaitAsync(TimeSpan.FromSeconds(30)); | ||
} | ||
else | ||
{ | ||
client.Send(msg); | ||
} | ||
}); | ||
} | ||
|
||
public static IEnumerable<object[]> SendMail_MultiLineDomainLiterals_Data() | ||
{ | ||
foreach (bool async in new[] { true, false }) | ||
{ | ||
foreach (string address in new[] { "foo@[\r\n bar]", "foo@[bar\r\n ]", "foo@[bar\r\n baz]" }) | ||
{ | ||
yield return new object[] { address, "[email protected]", async }; | ||
yield return new object[] { "[email protected]", address, async }; | ||
} | ||
} | ||
} | ||
} | ||
} |